39

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.

Jorge Castro
  • 71,754
philipballew
  • 2,439

5 Answers5

43

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.

Marty Fried
  • 18,466
  • Just saw another answer when I posted this. Looks like I wasted time, but perhaps it will help anyway, somehow. – Marty Fried Jan 22 '12 at 22:18
  • Glad 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). :) – Marty Fried Jan 24 '12 at 19:22
  • BTW, 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. – Marty Fried Jan 29 '12 at 21:21
  • 1
    +1 Excellent tip. Not necessarely for vim, but in general to add new applications to Open with... – bioShark Feb 01 '12 at 21:01
  • If you want vim to be the default choice for a file extension, not just another choice, add that extension to the Categories=. – Noumenon Dec 06 '19 at 00:02
23

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?

Achu
  • 21,237
  • 2
    Excellent 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 – brunch875 Aug 27 '14 at 11:02
  • In general, it's better to set EDITOR and use sudoedit than to run the editor under sudo – Darael Nov 20 '15 at 14:56
  • Note that in Ubuntu 23.04 the correct path to the defaults.list file is /usr/share/applications/defaults.list. Modification of /etc/gnome/defaults.list does not have any effect. Also, if you want to replace the default Text Editor with Gedit and vice versa, their desktop entries are org.gnome.gedit.desktop and /etc/gnome/defaults.list respectively. – Kyselejsyreček Apr 20 '23 at 06:54
12

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".

3

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.

scribu
  • 1,351
  • I 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. – Marty Fried Jan 23 '12 at 01:42
  • Is gvim a Gnome app? I think the 'g' stands for 'graphical', not 'Gnome'. – voithos Feb 13 '12 at 06:51
  • @voithos: Indeed, the 'g' doesn't stand for 'Gnome'. Corrected. – scribu Feb 13 '12 at 08:01
2

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
texasflood
  • 457
  • 2
  • 6
  • 18