How can I change the default text editor from gedit to vim?
Solution 1
You don't say how you are clicking on this file, so I will explain a way to do it in Gnome from Nautilus. It should work in other cases, I believe.
First, to get Vim (or any app) into the "open with other application" list, you need to create a .desktop file in ~/.local/share/applications
with a line in it like this: Exec=<command> %f
. I just made one called vim.desktop with these lines:
[Desktop Entry]
Categories=;
Comment=Edit file in Vim
Exec=vim %f
GenericName=Text Editor
Hidden=false
Icon=vim
Name=Vim
Terminal=true
Type=Application
Version=1.0
This made "Vim" appear in the list of possible apps when I right-clicked in Nautilus, and opened the file as expected.
Let me know if there are any problems.
Solution 2
Open defaults.list
sudo -H gedit /etc/gnome/defaults.list`
Replace
text/plain=gedit.desktop
with
text/plain=gvim.desktop
Save and close.
For more details and screenshot check this answer: How do I stop gedit from opening anything?
Solution 3
Save this as ~/.local/share/applications/vim.desktop
:
[Desktop Entry]
Name=Vim Text Editor
Comment=Edit text files
Exec=vim %F
Terminal=true
Type=Application
Icon=vim
Categories=Utility;TextEditor;
StartupNotify=true
MimeType=text/plain;
Then in Nautilus right-click on a text file, choose "Properties" and go to "Open with". If vim isn't shown here click "Show other applications". Select vim and click "Set as default".
Solution 4
The problem with the vim.desktop
approach is that each time it will open a new terminal window, instead of using an existing vim instance. This is probably not what you want, but I don't think there's any way around it.
The next best thing is using gvim
, which is a GUI app, instead of a terminal app.
Solution 5
Pure command line based method:
echo "[Desktop Entry]
Name=Vim Text Editor
Comment=Edit text files
Exec=vim %F
Terminal=true
Type=Application
Icon=vim
Categories=Utility;TextEditor;
StartupNotify=true
MimeType=text/plain;" > ~/.local/share/applications/vim.desktop
cp /etc/gnome/defaults.list ~/defaults.list.bak # backup
sudo sed -i "s/=gedit\.desktop/=vim\.desktop/" /etc/gnome/defaults.list
mkdir ~/.icons
wget -O ~/.icons/vim.png http://en.xn--icne-wqa.com/images/icones/1/4/vim.png # if you want an icon
Related videos on Youtube
philipballew
I am Ubuntu user, and Ubuntu Member. I do many things in the Ubuntu community. I do what I can and sometimes more. My spectiulties are: Networking and Networking hardware Troubleshooting Unity and Unity Customization (If you break Unity, I can fix it for you) LXDE Getting Ubuntu to work for you A full detailed documentaion of my work with ubuntu can be found here
Updated on September 18, 2022Comments
-
philipballew over 1 year
How can I change the default text editor from gedit to vim? When I click on a text file, I want it to open in vim under the terminal instead of gedit.
-
Deepak Verma over 12 yearsJust saw another answer when I posted this. Looks like I wasted time, but perhaps it will help anyway, somehow.
-
Deepak Verma over 12 yearsI think by default gvim will also open a new instance. There is a way around this, I think by using a gvim switch. I used to have it open in a new tab with the existing gvim before I reinstalled Ubuntu.
-
Deepak Verma over 12 yearsGlad I could help. Thanks for letting me know. I learned something new, too, as I didn't know the answer, but decided it would be nice to know (and an exercise for my brain). :)
-
Deepak Verma over 12 yearsBTW, from a different thread, if you want to use vim automatically instead of gedit for all types of files. I think you could edit /etc/gnome/defaults.list, and replace gedit with vim, to use the vim desktop file. I haven't tested this, though.
-
bioShark about 12 years+1 Excellent tip. Not necessarely for vim, but in general to add new applications to Open with...
-
voithos about 12 yearsIs
gvim
a Gnome app? I think the 'g' stands for 'graphical', not 'Gnome'. -
user1036005 about 12 years@voithos: Indeed, the 'g' doesn't stand for 'Gnome'. Corrected.
-
brunch875 over 9 yearsExcellent answer! I'd like to add to this to open it with sudo vim /etc/gnome/defaults.list and then use the command %s_gedit_gvim_g
-
Darael over 8 yearsIn general, it's better to set
EDITOR
and usesudoedit
than to run the editor undersudo
-
Noumenon over 4 yearsIf you want vim to be the default choice for a file extension, not just another choice, add that extension to the
Categories=
.