How can I change the default text editor from gedit to vim?

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.


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?


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