TL;DR
$ xdg-mime default vim.desktop <MIMETYPE>
Or edit ~/.config/mimeapps.list
.
MIMETYPE is the output of $ xdg-mime query filetype <interested-file>
If you are using some Desktop Environment (KDE, GNOME, LXQT, etc), you should refer to your DE's documentation.
But there is the XDG standard for setting default applications for specific mime-types.
Mime-type is a way to distinguish one type of file from another, see on Wikipedia, or archwiki. For example, there is text/html
mime-type for *.html
files, and text/plain
for *.txt
files. You can determine mime-type with $xdg-mime query filetype <file_you_interested>
.
Mime-types are used to link applications with files, which these applications should open.
Open ~/.config/mimeapps.list
wich looks something like this on my machine:
[Default Applications]
x-scheme-handler/http=firefox.desktop
...
inode/directory=org.gnome.Nautilus.desktop
[Added Associations]
application/x-shellscript=nvim-qt.desktop;
...
application/pdf=firefox.desktop;
Add to [Default Applications]
section these lines
text/plain=vim.desktop
text/markdown=vim.desktop
text/html=vim.desktop
(Add another mime-types, if you need them)
vim.desktop
is the file in /usr/share/applications
with the following content on my machine:
[Desktop Entry]
Name=Vim
TryExec=vim
Exec=vim %F
Terminal=true
Type=Application
Keywords=Text;editor;
Icon=gvim
Categories=Utility;TextEditor;
StartupNotify=false
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
(Really, there is a lot of translations of names and comments, but they don't matter here)
Look at line Terminal=true
.
By the rules in XDG Desktop Entry Specification, this line says that launcher should open the terminal emulator, and then in this terminal window should be opened your app (Exec
line). Fine, yes?
However, there is a five-year-old bug in xdg-open and xdg-open ignore Terminal
key because there is no specification for the default terminal emulator.
( They tried, but in 2020 there is no spec yet. )
So if you not using DE, xdg-open doesn't respect Desktop Entry Specification for you.
The person that filled bug had created a patch (which was ignored, sadly), that looks at $TERMINAL variable and open terminal emulator, so you can patch your /usr/bin/xdg-open (or $ which xdg-open
).
If you don't want to change xdg-open
script yourself, you can use some workarounds:
You can use gvim.desktop
(if you had gvim
in your system) or nvim-qt.desktop
(qt front-end for neovim
).
You can create a file with some name like my-vim.desktop
with the following content:
[Desktop Entry]
Type=Application
Name=MyVim
Exec=<COMMAND TO RUN YOUR TERMINAL> vim %F
Terminal=false
Icon=gvim
Categories=Utility;TextEditor
And place it in ~/.local/share/applications
.
Command to run your terminal you can get from the manual page of the prefered terminal (for example, gnome-terminal -e
).
More details in Desktop Entry Specifications.
- You can somehow read the
xdg-open
source code and tweaks your system that xdg-open
will think you are using DE and call DE-specific tools, but I think it is strange.
Useful links:
XDG-mime manual page - https://linux.die.net/man/1/xdg-mime
Arch wiki about XDG mime types - https://wiki.archlinux.org/index.php/XDG_MIME_Applications
Arch wiki about XDG Desktop entry - https://wiki.archlinux.org/index.php/Desktop_entries#Application_entry
xdg-open issue - https://gitlab.freedesktop.org/xdg/xdg-utils/-/issues/84