Bash – Open files in existing Gvim in multiple (new) tabs

aliasbashtabsUbuntuvim

I have put some aliases in my .bashrc to open a group of project files in gvim, each in their own tab:

gvim -p <list of file names using absolute paths>

This is all well and good, except there are several groups of files I might want to move between at any given time (my current project uses Ruby on Rails, so that explains that). What would be really awesome is if I could append the new tabs to an existing instance of gvim. In my last position I worked on Vista; I got around this by opening a bunch of empty tabs in gvim, which allowed me to right-click on a filename and choose "Open in existing No-Name gvim." Now I use Ubuntu and there's no such thing on the context menu. Is there any way to do this from the command line?

Best Answer

If vim is compiled with the clientserver option, you can do it. Start your vim instance with the following flag:

$ gvim --servername GVIM  # GVIM is the server name. It can be anything.

To open more tabs in this instance, you can run the command:

$ gvim --servername GVIM --remote-tab file1 file2 file3 ...

The clientserver feature in vim is very handy. It's not limited to opening files; it can be used to send any command to vim using the command-line. For example, to close a vim instance remotely, you can use:

$ gvim --servername GVIM --remote-send '<Esc>:wqa<CR>'