Vim Tips Wiki
Tip 1552 Printable Monobook Previous Next

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.

Desktop entry[]

To ensure that changes will be persistent after gVim has been updated or reinstalled, copy the system-wise .desktop file (/usr/share/applications/gvim.desktop for most GNU/Linux systems) into ~/.local/share/applications/ and edit it:

Find this line:

Exec=gvim -f %F

Change it 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.

alias[]

You can of course also use an alias:

% 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 script[]

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.


See also[]

Comments[]