The purpose

On my personal working PC, I always wanted to use the latest vim that build from the source. Also, I want to be able to edit a file by clicking the file icon.

To do it, the idea is make a .desktop file in the ~/.local/share/applications folder. Inside the file, write something like,

Exec=bash -c "vim %F"

This is the standard way to create a desktop file and should work in principle. However, it is not enough: the desktop will not start up. Typically, one needs to handle following issues.

Preventing the terminal from close

In the .desktop file, change Exec to the following,

Exec=bash -c "vim %F;exec $SHELL"

The extra $SHELL is used to prevent the terminal from closing. The source is here

Inherit custom environment

In the .desktop file, change Exec to the following,

Exec=bash -c "source ~/.bashrc && vim %F;exec $SHELL"

This is quite obvious if we need the env variables in ~/.bashrc. Something linke the $PATH and $LD_LIBRARY_PATH. However, one needs to take extra care with the .bashrc file.

Typically, following lines appear in the .bashrc file,

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

And in principle, any environmental variable declearation should be before this code-block. Otherwise the environment won’t affect the terminal called by the desktop.

Final .desktop File

[Desktop Entry]
Encoding=UTF-8
Name=Vim (Termial)
Comment=Edit text files in a console using Vim
Exec=bash -c "source ~/.bashrc && vim %F;exec $SHELL"
Terminal=true
Type=Application
Icon=/usr/local/share/logos/vim.svg
Categories=Application;Utility;TextEditor;
MimeType=text/plain;
NoDisplay=true