created 2008 · complexity basic · author MagnusBerg · version 7.0
Here is how to open files in new tabs, instead of new instances of gvim, under Unix. It works even when you mouse click to open files in a file manager.
gvim.desktop desktop file (
/usr/share/applications/ for most GNU/Linux systems) look for the "Exec" line. It will probably be like this:
Exec=gvim -f %F
Change the Exec line to:
Exec=gvim -p --remote-tab-silent %F
You may also need to change the StartupNotify value to "false" if it is currently set "true", or your Vim may not respond properly when the file is already open in a tab.
Note: Be aware that the desktop file is changed when you install a new version of gvim, so you may need to repeat the following change after an update.
You can of course also use an
% alias gvim='gvim -p --remote-tab-silent'
Most GUI programs don't read aliases, but it is useful if you start gvim from the shell.
Automatic desktop identification using shell scriptEdit
If you want to automatically open into the gvim window on the current desktop, use the following bash script:
#!/bin/bash this=$(basename $0); # vim|gvim|rvim etc... desktop=desktop_$(xprop -root -notype _NET_CURRENT_DESKTOP | perl -pe 's/.*?= (\d+)/$1/') if [ $# != 0 ]; then exec /usr/bin/$this --servername $desktop --remote-tab-silent "$@" elif ! vim --serverlist | grep -iq $desktop; then exec /usr/bin/$this --servername $desktop fi
(script originally from https://gist.github.com/966205)
Save this bash script as /usr/local/bin/gvim to override the global Vim installation, which it will call into (assuming your global installation is in /usr/bin).
The script queries the X server for the current desktop, queries Vim for running Vim instances with --serverlist. When you run this script it will open a file in a new tab in an existing gvim window on the current desktop, or open new one if no gvim window exists on the current desktop, using --servername in both cases to specify a name for the new Vim instance which is specific to the current desktop. If no file was given and gvim is already running on the desktop, the script does nothing.