Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open files in existing Gvim in multiple (new) tabs

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?

like image 583
kajaco Avatar asked May 21 '09 19:05

kajaco


People also ask

How do I open multiple tabs in Gvim?

To open more tabs in this instance, you can run the command: $ gvim --servername GVIM --remote-tab file1 file2 file3 ...

How do I open an existing file in Gvim?

Open a new or existing file with vim filename . Type i to switch into insert mode so that you can start editing the file. Enter or modify the text with your file. Once you're done, press the escape key Esc to get out of insert mode and back to command mode.


4 Answers

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>' 
like image 90
Ayman Hourieh Avatar answered Oct 01 '22 12:10

Ayman Hourieh


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.

like image 27
Steve K Avatar answered Oct 01 '22 12:10

Steve K


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

like image 24
matt burns Avatar answered Oct 01 '22 14:10

matt burns


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
like image 21
Jan Valiska Avatar answered Oct 01 '22 12:10

Jan Valiska