45

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?

Mahbub
  • 4,812
  • 1
  • 31
  • 34
kajaco
  • 2,547
  • 3
  • 24
  • 33

6 Answers6

71

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
  • 132,184
  • 23
  • 144
  • 116
  • see vim -h for other remote options – Nathan Fellman Jun 17 '09 at 07:43
  • 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 Jun 24 '09 at 18:20
  • 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 Jun 26 '09 at 22:51
  • 2
    http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instance keeps it to one instance. – kajaco Aug 18 '09 at 19:09
22

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
  • 2,124
  • 16
  • 19
  • 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 Jun 24 '09 at 18:21
  • 1
    Wait, there's more! You can do this with just one command: :tabnew % – K. Norbert Nov 17 '09 at 08:41
  • Given command `:tabnew %`, Gvim opened a new tab containing the buffer (filename) from the previous tab from which I entered the command. Is this the correct behaviour? – Derek Mahar May 12 '11 at 21:09
  • @DerekMahar Yes, % refers to the filepath of the file you're currently working on. You might be able to navigate to another file from that, but I don't know enough about vim to tell you how to do that. – webdesserts Sep 24 '11 at 14:11
  • 3
    You can shorten that to `:tabe .`. If you want to open another file from the same directory, you can try `:tabe %:h` – Ehtesh Choudhury Nov 12 '11 at 14:31
  • To beat a dead horse: also see Tex (open explorer in a new tab), Vex (open explorer in a new vertical window), and Sex (open explorer in in a new horizontal window. Also, gigidy.) – danns87 Jan 31 '14 at 02:58
13

Want your Windows context menu to allow you to open files in a new tab of the currently open gvim window?

Save this as as a file called temp.reg and double-click it to add the settings to your registry. Be sure to modify the path to vim if yours is different.

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim]
[HKEY_CLASSES_ROOT\*\Shell\Open with &Vim\command]
@="\"C:\\Program Files (x86)\\Vim\\vim73\\gvim.exe\" -p --remote-tab-silent \"%1\" \"%*\""

You will now have a context menu like this:

vim context menu

matt burns
  • 24,742
  • 13
  • 105
  • 107
  • The question was asking how to do this on Ubuntu. – kajaco Mar 21 '14 at 15:00
  • 1
    if Ubuntu user is smart enough to pick up the command line form the answer it would work for him as well. (Checked on Arch Linux :)) – Slava Sep 17 '15 at 07:46
6

Linux users may use this kind of script:

#!/bin/bash

ANS=`pgrep -fx "gvim --servername GVIM"`

echo $@

if [[ ! $ANS ]]; then
    gvim --servername GVIM
fi

if [[ $1 ]]; then
    gvim --servername GVIM --remote-tab "${@}"
fi

And then edit gvim.desktop file for using this script:

Exec=/home/user/bin/my_gvim_script.sh %F
Jan Valiska
  • 145
  • 2
  • 6
1

Here is my gvim start-up script. It is an extension of previous answers. It ensures only one gvim instance will run when gvim is called under all circumstances:

  • gvim is called without filename when no gvim instance is running; gvim is started.
  • gvim is called without filename in the presence of a gvim instance; an empty new tab is opened.
  • gvim is called with a filename with or without a gvim instance; a tab is opened showing the file.

This will mimic the standard behaviour of other editors.

#!/bin/bash
exec=/usr/bin/gvim  #the path to gvim
if [ $# -eq 0 ]
  then  # no filename given
    if [ -z $($exec --serverlist) ]
      then  # no filename given and no gvim instance
        $exec -f --servername GVIM > /dev/null 2>&1
      else  # no filename given, but a gvim instance exists
        $exec -f --servername GVIM --remote-send ':tabnew<CR>' > /dev/null 2>&1
    fi
  else  # filenames given
    $exec -f --servername GVIM --remote-tab "$@" > /dev/null 2>&1
fi
Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
1

There is a way:

n*.cpp|tab ba

or if you like to split:

n*.cpp|sba

If you wish to know more:

:help ba

and I don't know what is n , but it would not work without it.

user1021852
  • 146
  • 9