5

On MS Windows using gVim, is it possible to select several files from the explorer and open them in the same instance of gVim in different buffers?

At the moment if I select 3 different files on Windows graphical explorer and press enter they get open in 3 different instances of gVim. What I want is to open them in the same instance.

When I work on linux and I want to edit all the files of a directory with vim I can simply from command line do the folowing:

$ vim ./*.sh

This will open all the files in the same instance of vim in different buffers. I'm looking for a way to do the same but from Windows GUI with gVim.

Note 1: Using MS Windows command line is not an option here neither is using a cygwin console.

Note 2: I know that I can open only one file and then, from gVim open the others but that would be slower than simply openning them all in one time from GUI.

Note 3 (edit): Out of curiosity I just tried to do it on a Debian Jessie with a gVim 7.3 and it worked as I want (one instance - several buffers) out of the box with the same .vimrc as the one of the windows machine (without any .gvimrc file). So I deduce that:

  • It is possible to do what I want.
  • The behavior of gVim depends on the OS running it which is a little bit disturbing.
  • Re: Note 3. From what I rememer, Windows Explorer launches the program 2 times when you open 2 files (it runs gvim file1 and gvim file2). However, the filemanager you used on Debian may invoke gvim only once with all the files in the argument list (gvim file1 file2). The difference in behaviour is not in gVim as such, but in how it is invoked... – Martin Tournoij Jun 25 '15 at 0:26
  • @carpetsmoker that seems right but is it possible to change this behavior? Or is my question not related enough with vim? Maybe I should have asked it on super user? – statox Jun 25 '15 at 2:00
4

By default gVim for Windows adds an option to the Explorer right-click menu that contains an option Edit with single Vim when multiple files are selected and that works as expected, opening them in a single window. That doesn't handle pressing Enter with multiple files selected though.

There is a solution presented on the vim wiki though. In short, you need to modify the registry (using Registry Editor, regedit.exe) so that the appropriate file associations call gvim.exe with the --remote-tab-silent option. Searching for any occurrences of the vim executable with a full path and "%1" as a parameter and changing them to include that parameter before the "%1" should do it.

I just did this and the only occurrence I found was:

HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\edit\command

See below for the key after editing.

gVim registry key

Once the change was made, hitting Enter with multiple files associated with gVim opened them all in the same session.

Note: You almost certainly need Administrator privileges to edit that registry key.

  • If I am not mistaken, this will effectively limit the total number of open Vim instances to just a single one? – Martin Tournoij Jun 25 '15 at 8:50
  • 1
    @Carpetsmoker "Sort of". If you have an existing gVim session, the files opened by this method will be added as new tabs (and if you have multiple sessions, they will be added to the most recently created). But it doesn't prevent you from opening multiple sessions using other methods (eg, running gvim.exe again, right-clicking and editing, etc). – mjturner Jun 25 '15 at 8:56
  • @mjturner: Thank for your solution it is pretty close from what I wanted except that it opens files in different tabs instead of different buffers in one tab only but that's what I'm going to use if I can't find something closer to what I want :) – statox Jun 25 '15 at 9:02
  • 1
    @statox Great! I thought --remote-silent might do what you want, but it doesn't open a new buffer, it replaces the active one. – mjturner Jun 25 '15 at 9:08
  • 3
    @statox You can sent $any command with --remote or --remote-silent. See :help --remote for an example. – Martin Tournoij Jun 25 '15 at 9:40
1

This plugin, gvimt, http://www.vim.org/scripts/script.php?script_id=4214 gives you various options on the Windows right-click/Context menu, the SendTo menu (for those cases where you aren't allowed to edit the registry) and the command line.

It lets you do these things with the files you specify:

"Edit with Vim Tab" (open each in a new tab)

"Edit with Vim VSplit" (vertically split the existing tab for each)

"Edit with Vim Split"

"Edit with Vim VSplit Tab" (open a new tab first, then open all the files you selected vertically split within it)

"Edit with Vim Split Tab"

By default it won't let you do it just by pressing [Enter], but since the actions appear on the Context menu I believe you could change the default action for text files to be whichever you liked. I have not attempted this.


It's based on the methods discussed on the Vim wiki ( http://vim.wikia.com/wiki/Launch_files_in_new_tabs_under_Windows , this is the same link discussed in mjturner's answer ) but addresses some problems I had with those methods:

  • There is no --remote-split parameter so a different approach is needed for opening files in a new split window. --remote-send can be used to send an arbitary command for this, but unlike --remote-tab it will not open a new instance of gvim if one doesn't already exist.

  • If you select multiple files and use the Windows Context menu, each file gets its own invocation of the command. gvim does not handle several simultaneous --remote-tab commands well.

  • The SendTo menu does pass all the selected files to one command, but it is an extra click on the right menu to get there, so it is nice to use the Context menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.