Set default xdg-open application to terminal program
Solution 1
In either your .bashrc or .zshrc, depending whether you use bash or zsh respectively, export these two environment variables:
export EDITOR=vim
export VISUAL=vim
Adittionally, you might want to associate vim to the mimetype of text files:
xdg-mime default vim.desktop text/plain
Now you'll have to create a vim.desktop file in /usr/share/applications
, which should execute the terminal emulator you want, opening vim.
Solution 2
I have to add a new answer, even if my comment only completes the answer by thiagowfx, because in the comments you cannot indent code.
The content of vim.desktop
can be something like this:
[Desktop Entry]
Name=Vim Text Editor
Comment=Edit text files
Exec=vim
Terminal=true
Type=Application
Icon=terminal
Categories=Utility;TextEditor;
StartupNotify=true
MimeType=text/plain;
I prefer to put it under ~/.local/share/applications
.
Solution 3
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 hadgvim
in your system) ornvim-qt.desktop
(qt front-end forneovim
).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 thatxdg-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
Related videos on Youtube

Evan
I received my Bachelors and Masters of Science in computer science from the University of Victoria. I enjoy working with many areas of computer science, having extensive projects in graphics, networking, and systems, as well as publishing papers in software engineering.
Updated on September 18, 2022Comments
-
Evan 3 months
Is it possible to use terminal vim with xdg-open?
I don't have a GUI text editor because I only use vim through the terminal. (I don't care very much for gvim either.) Is it possible to tell xdg-open to open a terminal, then open vim with the selected file?
Thanks.
-
thiagowfx almost 8 yearsI'm posting a partial answer so people can complete them later. I haven't understood which types of files you want to open with vim. I'm assuming text/plain only.
-