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, even though we won't use it.
In Debian and Ubuntu, for example, sudo apt-get install vim-athena
installs it. An alternative is to build Vim with that option.
Now that you have +clientserver, create the file vimserver with contents:
#!/bin/sh
serv=$(id -u)
if vim --serverlist | grep -qxF "$serv"; then
vim --servername "$serv" --remote-tab "$@"
else
xterm -maximized -e vim -p --servername "$serv" "$@" &
fi
Don't forget to put vimserver in a directory in your $PATH
and to make it executable with chmod +x vimserver.
Also, if you change the terminal, find in your terminal manpage what is
the appropriate flag to execute the command in a new terminal window1.
Test it on some files (multiple files can be passed in the arguments):
vimserver file1 file2
vimserver file3 file4 file5
Each file will be opened in a new tab of a same Vim instance.
If you want new files to be in a new buffer,
just change --remote-tab to --remote and remove the -p flag.
Explaining vimserver
The server name, assigned to serv variable, is the user's effective ID.2
This is particularly useful when editing some files as root and others as
your normal user because it keeps separate windows for each.
Grep checks if the corresponding server already exists. If yes, the server loads the file, otherwise, a new terminal is launched and a server with it.
Requesting focus
The terminal is not focused when a file is opened in an already existing server. For XTerm, Rxvt and Kitty, giving the window the server name and adding a Xdotool line solves that:
#!/bin/sh
serv=$(id -u)
if vim --serverlist | grep -qxF "$serv"; then
vim --servername "$serv" --remote-tab "$@"
xdotool search --classname "^$serv\$" windowactivate
else
xterm -maximized -name "$serv" -e vim -p --servername "$serv" "$@" &
fi
Because of the -name option, Xdotool can locate the Vim window and request focus to it.
Some terminals (Xfce, Gnome, ...) unfortunately lack that option.
File managers
Midnight Commander
Edit the extension file ~/.config/mc/mc.ext, adding:
type/text
Open=vimserver %s
The %s macro means that not only a single selected file can be opened, but also multiple tagged files!
Vifm
Add
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.
GUI file-managers
Find how your file manager associates files with programs to launch them. For many of them, the procedure is similar to this:
Right click a text file, Open with... > Custom command line / Use a custom command.
In the command text field, enter vimserver and, if any, mark a checkbox or press the button that sets it as default.
gVim
If you are OK with gVim3, do not bother with vimserver. Just use
gvim --remote-tab-silent.
1Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.
2Bash and Zsh and some other shell users can use $EUID instead of $(id -u). I want to
keep the solution portable, so I have used the latter.
3User @goldilocks comments about the gVim usage in his answer of What are practical uses of the client-server mode?.