Skip to main content
Use an environment variable to specify a server, so the user does not need to stick with a single server.
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

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, sudo apt install vim-athena0 installs it. An alternative for experienced users who definetly want to skip Gvim is to build Vim from sources with flags --with-x=yes --disable-gui.

#!/bin/sh
caseserv=${S:-xtvim}
if "$(vim --serverlist)" in
| grep -qxiF "$serv"; *XTVIM*)then
    vim --servername XTVIM"$serv" --remote-tab "$@" ;;
 else
   *) xterm -e vim -p --servername XTVIM"$serv" "$@" & ;;
esacfi
vimserver file1 file2
vimserver file3 file4 file5
S=xyz vimserver file6

Each file will be openedopens in a new tab of a same Vim instance named "XTVIM", except for file6, which goes to a different instance, "XYZ". If you want new files to be in a new buffer, just change --remote-tab to --remote and remove the -p flag.

The case statement checks if the Vim server XTVIM — the name is arbitrary — exists. If yes, assigned to the (3rd line)serv variable, defaults to "xtvim", but as seen XTVIM loadsabove a different one can be specified with the fileenvironment variable S. If not

Grep looks for the exact value of serv in the server list (4th linebut case-insensitively since Vim forces its server names to uppercase),. aIf it is found, that server loads the file, otherwise, a new terminal is launched and new Vim and the server (named XTVIM) with it. The file is loaded in that new Vim instance.

The terminal is not focused when a file is opened in an already existing server. For XTerm, URxvt and Kitty, settingserver; Setting an instance name for the window and adding a Xdotool line solves that:

#!/bin/sh
caseserv=${S:-xtvim}
if "$(vim --serverlist)" in
| grep -qxiF "$serv"; *XTVIM*)then
        xdotool search --classname XTVIM"^$serv$" windowactivate
        vim --servername XTVIM"$serv" --remote-tab "$@" ;;
 else
   *) xterm -name XTVIM"$serv" -e vim -p --servername XTVIM"$serv" "$@" & ;;
esacfi

Because of the -name option, Xdotool can locate the Vim window and request focus to it. Some terminals lack that option, in which case try to match the window title by substituting the Xdotool line with but have wmctrl -a XTVIM-role, which Xdotool orcan also use for xdotool search --name XTVIM windowactivate (assuming you did not fuss with Vim's titlestring option)since version 3.20210804.2.

0: There is also the GTK GUI, but Athena is the lightest.
1: Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.
2: Goldilocks comments about the Gvim usage in What are practical uses of the client-server mode?.

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, sudo apt install vim-athena0 installs it. An alternative for experienced users who definetly want to skip Gvim is to build Vim from sources with flags --with-x=yes --disable-gui.

#!/bin/sh
case "$(vim --serverlist)" in
    *XTVIM*) vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -e vim -p --servername XTVIM "$@" & ;;
esac
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.

The case statement checks if the Vim server XTVIM — the name is arbitrary — 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.

The terminal is not focused when a file is opened in an already existing server. For XTerm, URxvt and Kitty, setting an instance name for the window and adding a Xdotool line solves that:

#!/bin/sh
case "$(vim --serverlist)" in
    *XTVIM*)
        xdotool search --classname XTVIM windowactivate
        vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -name XTVIM -e vim -p --servername XTVIM "$@" & ;;
esac

Because of the -name option, Xdotool can locate the Vim window and request focus to it. Some terminals lack that option, in which case try to match the window title by substituting the Xdotool line with wmctrl -a XTVIM or xdotool search --name XTVIM windowactivate (assuming you did not fuss with Vim's titlestring option).

0: There is also the GTK GUI, but Athena is the lightest.
1: Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.
2: Goldilocks comments about the Gvim usage in What are practical uses of the client-server mode?.

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, sudo apt install vim-athena installs it. An alternative for experienced users who definetly want to skip Gvim is to build Vim from sources with flags --with-x=yes --disable-gui.

#!/bin/sh
serv=${S:-xtvim}
if vim --serverlist | grep -qxiF "$serv"; then
    vim --servername "$serv" --remote-tab "$@"
else
    xterm -e vim -p --servername "$serv" "$@" &
fi
vimserver file1 file2
vimserver file3 file4 file5
S=xyz vimserver file6

Each file opens in a new tab of a same Vim instance named "XTVIM", except for file6, which goes to a different instance, "XYZ". If you want new files to be in a new buffer, just change --remote-tab to --remote and remove the -p flag.

The server name, assigned to the serv variable, defaults to "xtvim", but as seen above a different one can be specified with the environment variable S.

Grep looks for the exact value of serv in the server list (but case-insensitively since Vim forces its server names to uppercase). If it is found, that server loads the file, otherwise, a new terminal is launched and the server with it.

The terminal is not focused when a file is opened in an already existing server; Setting an instance name for the window and adding a Xdotool line solves that:

#!/bin/sh
serv=${S:-xtvim}
if vim --serverlist | grep -qxiF "$serv"; then
    xdotool search --classname "^$serv$" windowactivate
    vim --servername "$serv" --remote-tab "$@"
else
    xterm -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 lack that option but have --role, which Xdotool can also use for search since version 3.20210804.2.

1: Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.
2: Goldilocks comments about the Gvim usage in What are practical uses of the client-server mode?.

Revert to simpler script (better to use sudoedit for sudo edits); Match window title for terminals lacking -name.
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

First make sure vim --version | grep clientserver returns +clientserver. If it returns - instead of +, an easy way to enable that capability is to install gVimGvim, even though we won't use it. In Debian and Ubuntu, for example, sudo apt-get install vim-athena0 installs it. An alternative for experienced users who definetly want to skip Gvim is to build Vim with that optionbuild Vim from sources with flags --with-x=yes --disable-gui.

#!/bin/sh
serv=$(id -u)
ifcase "$(vim --serverlist | grep -qxF "$serv";)" thenin
    *XTVIM*) vim --servername "$serv"XTVIM --remote-tab "$@"
else ;;
    xterm*) -maximizedxterm -e vim -p --servername "$serv"XTVIM "$@" & ;;
fiesac

Don't forget to put vimserver in a directory in your $PATH$PATH and to make it executable with chmod +x vimserver. Also, ifIf you change theuse other terminal, find in your terminalits manpage what is the appropriate flag to execute the command in a new terminal window1.

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.

Grepcase statement checks if the correspondingVim server alreadyXTVIM — the name is arbitrary — exists. If yes (3rd line), the serverXTVIM loads the file, otherwise. If not (4th line), a a new terminal is launched and a and new Vim server (named XTVIM) with it. The file is loaded in that new Vim instance.

The terminal is not focused when a file is opened in an already existing server. For XTerm, RxvtURxvt and Kitty, givingsetting an instance name for the window the server name andand adding a Xdotool line solves that:

#!/bin/sh
serv=$(id -u)
ifcase "$(vim --serverlist)" |in
 grep -qxF "$serv"; then*XTVIM*)
    vim --servername "$serv" - xdotool search -remote-tabclassname "$@"XTVIM windowactivate
    xdotool search   vim --classnameservername "^$serv\$"XTVIM windowactivate
else--remote-tab "$@" ;;
    xterm*) -maximizedxterm -name "$serv"XTVIM -e vim -p --servername "$serv"XTVIM "$@" & ;;
fiesac

Because of the -name option, Xdotool can locate the Vim window and request focus focus to it. Some Some terminals lack that option, in which case try to match the window title by substituting the Xdotool line with wmctrl -a XTVIM or xdotool search --name XTVIM windowactivate (Xfce, Gnome,assuming you did not fuss with Vim's ...titlestring option) unfortunately lack that option.

Midnight CommanderVifm

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

typefilextype <text/text
*> Open=vimservervimserver %s%f

The %s macro means that not only a single selected fileMultiple files can be opened, but also multiple tagged files! at the same via visual mode or tagging.

VifmMidnight Commander

AddEdit the extension file ~/.config/mc/mc.ext:

filextype *type/text
 vimserverOpen=vimserver %f%s

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

Find how your file manager associates files with programs to launch them. ForFor many of them, the procedure is similar to this:

Right click right clicking 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.

gVimGvim

If you are OK with gVimGvim32, do not bother with vimserver. Just use gvim --remote-tab-silent.

10: There is also the GTK GUI, but Athena is the lightest.Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.

 
21: Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e.Bash 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.

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

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.

#!/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.

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.

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.

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.

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?.

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, sudo apt install vim-athena0 installs it. An alternative for experienced users who definetly want to skip Gvim is to build Vim from sources with flags --with-x=yes --disable-gui.

#!/bin/sh
case "$(vim --serverlist)" in
    *XTVIM*) vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -e vim -p --servername XTVIM "$@" & ;;
esac

Don't forget to put vimserver in a directory in your $PATH and to make it executable with chmod +x vimserver. If you use other terminal, find in its manpage what is the appropriate flag to execute the command in a new terminal window1.

The case statement checks if the Vim server XTVIM — the name is arbitrary — 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.

The terminal is not focused when a file is opened in an already existing server. For XTerm, URxvt and Kitty, setting an instance name for the window and adding a Xdotool line solves that:

#!/bin/sh
case "$(vim --serverlist)" in
    *XTVIM*)
        xdotool search --classname XTVIM windowactivate
        vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -name XTVIM -e vim -p --servername XTVIM "$@" & ;;
esac

Because of the -name option, Xdotool can locate the Vim window and request focus to it. Some terminals lack that option, in which case try to match the window title by substituting the Xdotool line with wmctrl -a XTVIM or xdotool search --name XTVIM windowactivate (assuming you did not fuss with Vim's titlestring option).

Vifm

Edit ~/.config/vifm/vifmrc:

filextype <text/*> vimserver %f

Multiple files can be opened at the same via visual mode or tagging.

Midnight Commander

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

type/text
 Open=vimserver %s

Tagging multiple files is also possible in MC.

For many of them, the procedure is right clicking 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 Gvim2, do not bother with vimserver. Just use gvim --remote-tab-silent.

0: There is also the GTK GUI, but Athena is the lightest. 
1: Gnome-terminal, Xfce4-terminal and Terminator use the -x flag, not -e. 
2: Goldilocks comments about the Gvim usage in What are practical uses of the client-server mode?.

Improved script: Now keeps separate servers for different EUIDs.
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78

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-gtk3athena installs installs it. An An alternative is to compilebuild Vim with that option.

#!/bin/sh
case "$serv=$(id -u)
if vim --serverlist)" in
| grep -qxF "$serv"; *XTVIM*)then
    vim --servername XTVIM"$serv" --remote-tab "$@" ;;
 else
   *) xterm -maximized -e vim -p --servername XTVIM"$serv" "$@" & ;;
esacfi

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 window. For XTerm, itfind in your terminal manpage what is indeed the -e flag, but for Gnome-terminalthe appropriate flag to execute the command in a new terminal window1, Xfce4-terminal and Terminator, it is the -x flag.

Each file shouldwill 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, justjust 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

Explaining vimservergVim

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.

Requesting focus

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:

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

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.

gVim

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

type/text
 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?.Gnome-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?.

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.

#!/bin/sh
case "$(vim --serverlist)" in
    *XTVIM*) vim --servername XTVIM --remote-tab "$@" ;;
    *) xterm -e vim -p --servername XTVIM "$@" & ;;
esac

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 window. For XTerm, it is indeed the -e flag, but for Gnome-terminal1, Xfce4-terminal and Terminator, it is the -x flag.

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

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.

Requesting focus

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:

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

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.

gVim

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:

type/text
 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?.

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.

#!/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.

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

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?.

Cleaned up some excessive bits of the answer
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Added Vifm; Code more robust (does not clash with GVIM server); Minor rewordings
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Added more file managers; Made the script accept multiple files in a single call.
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
vim-gtk3 is present in both Debian and Ubuntu, whereas vim-gnome is only present in the latter
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Some terminals use -x flag, not -e flag. Some terminals have the "role" option instead of "name", which window managers might handle.
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
-silent prevents GVim from complaining if no instance is open yet
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading
Source Link
Quasímodo
  • 19.4k
  • 4
  • 41
  • 78
Loading