Having files opening in tabs in GVim

2

I'm having trouble with gvim and tabs. Using Ubuntu, there is the gedit editor, which opens files in new tabs, but gvim opens a new window for each file i open. I definitely prefer to have tabs, just like every other application I use. Having 5 gvim windows open simultenously overwhelms me.

So, is there any way to edit .gvimrc to make it open new files in new tabs instead of opening a new gvim window? I definitely prefer a .gvimrc solution over a Nautilus solution.

Tarrasch

Posted 2011-09-12T20:15:01.343

Reputation: 208

Answers

2

Use the client/server feature of vim/gvim.

gvim --remote-tab myfile

This works for both terminal and GUI versions of vim, as long as they are compiled (vim --version) with the +clientserver feature. (Optionally add --servername foo to set the instance name, which defaults to GVIM.)

Unfortunately, you cannot do this from .vimrc. You will have to, either:

  • write a wrapper script, for example, ~/bin/gvim:

    #!/bin/sh
    exec /usr/bin/gvim --remote-tab-silent "$@"
    

    (--remote-tab-silent will cause a new server to be silently started if needed)

  • modify the gvim.desktop file to include the server commands:

    $ mkdir -p ~/.local/share/applications
    $ cp {/usr,~/.local}/share/applications/gvim.desktop
    $ vim -e ~/.local/share/applications/gvim.desktop
    :/^Exec=/ s/gvim/& --remote-tab-silent/
    :wq
    $
    

The wrapper script method will probably cover more cases than editing the .desktop file.

user1686

Posted 2011-09-12T20:15:01.343

Reputation: 283 655

Thanks! I used the wrapper script method and it works perfectly! Using a wrapper script is more portable, as I can clone my dotfiles anywhere and tabbed gvim will then just work. – Tarrasch – 2011-09-13T08:32:38.987

For anyone having problems with the gvim.desktop method, try using this exec line: Exec=bash -c "gvim --remote-tab-silent %f || gvim". This makes the dash icon still work. Also, if you're getting a spinning cursor for a long time after opening gvim, set StartupNotify to true. – nkorth – 2014-03-09T20:15:29.580