views:

4754

answers:

2

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?

+12  A: 

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>'
Ayman Hourieh
see vim -h for other remote options
Nathan Fellman
I added my -geom option to the initial command to start gvim. I also had to remove the -p option (for tabs) from all the aliases. Then it worked great!
kajaco
Well, it works great except for one thing: if I have more than one instance of gvim, the first being the server one and the second (or more) created from the CL or by clicking on an icon or by selecting a file to open with gvim, then I attempt to open a tab in the server one (I have aliases set up as mentioned), the new tabs _always_ go in the *second* instance, no matter how it was created or which instance was last active. Any ideas?
kajaco
http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instance keeps it to one instance.
kajaco
+2  A: 

From inside of Gvim, type :tabe {file_name}. This opens the named file in a new tab. If you aren't fond of typing long filenames, try this:

:tabnew
:e .

This will open a new, blank tab page and open a file browser. You can mouse click your way around or use the keyboard. Click or hit the enter key on the file you want to open it. Try using the keyboard to position the cursor over the file you want to open and then hit 't'. This opens the selected file in a new tab, keeping the file browser open in the first tab. This might be a fast way to open a bunch of files.

There are a whole lot of things you can do with tab pages that might make life easier. To get to the relevant section in Vim's on line help manual, type :h tabpage.

Steve K
This is also a great trick for opening a file in an existing instance of gvim. I use it for things that I don't need often enough to make an alias for, and when I just want one file rather than a whole slew of them.
kajaco
Wait, there's more! You can do this with just one command::tabnew %
WishCow