Wikia

Vim Tips Wiki

Changes: Launch files in new tabs under Unix

Edit

Back to page

("tab all" should be unnecessary, and add note from recent vim_use discussion about StartupNotify)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 3: Line 3:
 
|previous=1551
 
|previous=1551
 
|next=1553
 
|next=1553
|created=April 15, 2008
+
|created=2008
 
|complexity=basic
 
|complexity=basic
 
|author=MagnusBerg
 
|author=MagnusBerg
Line 14: Line 14:
   
 
==Desktop entry==
 
==Desktop entry==
In the <tt>gvim.desktop</tt> desktop file (<tt>/usr/share/applications/</tt> for most GNU/Linux systems) look for the "Exec" line. It will probably be like this:
+
In the <code>gvim.desktop</code> desktop file (<code>/usr/share/applications/</code> for most GNU/Linux systems) look for the "Exec" line. It will probably be like this:
 
<pre>Exec=gvim -f %F</pre>
 
<pre>Exec=gvim -f %F</pre>
   
Line 25: Line 25:
   
 
==alias==
 
==alias==
You can of course also use an <tt>alias</tt>:
+
You can of course also use an <code>alias</code>:
 
<pre>% alias gvim 'gvim -p --remote-tab-silent'</pre>
 
<pre>% alias gvim 'gvim -p --remote-tab-silent'</pre>
   

Latest revision as of 06:32, July 13, 2012

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 entryEdit

In the 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.

aliasEdit

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


See alsoEdit

CommentsEdit

Around Wikia's network

Random Wiki