In Midnight Commander, if Options > Configuration > Use internal edit is unset, MC uses my default editor (which I have set to Vim) instead of Mcedit when I press F4. That is good, but it blocks the file-manager until I exit Vim. To workaround that, I have put

 Open=xterm -e vim %f &

in the extension file (~/.config/mc/mc.ext) and then if I press Enter on a selected text file, it is opened in a new terminal with Vim and I can keep using MC to navigate.

However, after some time I end up with various terminals opened, each with a file, and cycling through them with AltTab becomes cumbersome. Is there a way to open every new file in a new tab/buffer of a single existing Vim instance, so that I don't get a bunch of terminal windows?

Edit: The question was originally only targeted at Midnight Commander. Now the scope has been extended to command-line and to other file-managers too.


Yes, but first make sure vim --version | grep clientserver returns +clientserver. If it returns - instead of +, an easy way to enable that capability is to install gVim. In Debian and Ubuntu, for example, sudo apt-get install vim-gtk3 installs it. An alternative is to compile Vim with that option.

Now that you have +clientserver, create the file vimserver with contents:

case "$(vim --serverlist)" in
    *XTVIM*) vim --servername XTVIM --remote-tab "$@" ;;
    *) x-terminal-emulator -e vim -p --servername XTVIM "$@" & ;;

Don't forget to put vimserver in a directory in your $PATH and to make it executable with chmod +x vimserver. Also, for the fourth line, find in your terminal manpage what is the appropriate flag to execute the command in a new terminal window. For XTerm, it is indeed the -e flag, but for Gnome-terminal1, Xfce4-terminal and Terminator, it is the -x flag.

Test it on some files (multiple files can be passed in the arguments):

vimserver file1 file2
vimserver file3 file4 file5

Each file should be opened in a new tab of a same Vim instance. If you want new files to be in a new buffer, not a new tab, just change --remote-tab to --remote and remove the -p flag.

File managers

Midnight Commander

Edit the extension file ~/.config/mc/mc.ext, adding:

 Open=vimserver %s

The %s macro means that not only a single selected file can be opened, but also multiple tagged files!



filextype * vimserver %f

to ~/.config/vifm/vifmrc and also accordingly modify the text filetypes you have possibly set (e.g. filextype *.c vimserver %f). Opening multiple tagged files is suported, too.


Just put vimserver in Edit > Preferences > Programs > Text editor. Xfe can also open multiple tagged files at once.

Pcmanfm, Thunar, Nemo, Rox, and probably other GUI file-managers

Although the menu entries labels may vary a bit, the procedure is the same.

Right click a text file, Open with... > Custom command line / Use a custom command. In the command text field, enter vimserver and mark the checkbox or press the button that sets it as default.

Explaining vimserver

The case statement checks if the Vim server XTVIM exists. If yes (3rd line), XTVIM loads the file. If not (4th line), a new terminal is launched and new Vim server (named XTVIM) with it. The file is loaded in that new Vim instance. A specific terminal can be specified, for example, xterm instead of x-terminal-emulator.

You may find out that your DE/WM does not focus the terminal with Vim when a file is opened in an already existing server. For XTerm, adding a xdotool line solves that:

case "$(vim --serverlist)" in
        xdotool search --classname vimserver windowactivate
        vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -name vimserver -e vim -p --servername XTVIM "$@" & ;;

Because of the -name option, xdotool can locate the Vim window and request focus to it. I have read the manpages of some modern terminals (Gnome, Xfce...) but could not find a similar option for them.


If you are OK with gVim1, do not bother with vimserver. Just use gvim --remote-tab-silent. Using MC as example, this would go in ~/.config/mc/mc.ext:

 Open=gvim --remote-tab-silent %f &

1 User @goldilocks comments about the gVim usage in his answer of What are practical uses of the client-server mode?.

| improve this answer | |

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.