In my daily work, I frequently switch between various applications like web browsers, email clients, messaging apps, and presentation tools — all of which require mouse interaction. Because of this, I no longer use GVim as a full IDE but rather for its original purpose: a powerful text editor.
Within this workflow, it's quite common to double-click a file to open it. However, I've noticed that doing so with text files launches a new instance of GVim each time. This not only slows things down, as the vimrc is reloaded with every launch, but also clutters my workspace with multiple GVim windows. The ideal behavior would be to reuse an existing GVim instance when opening files, avoiding repeated vimrc reloads and improving responsiveness.
Fortunately, I found a solution using GVim’s built-in server-client feature, which I’ll explain next.
We assume to use Windows 11 and that the current user has no Admin rights.
The trick to re-use an existing GVim instance when double-clicking on a text file is to use a server-client setup.
Let's assume that we cannot run the GVim installer, nor we have Admin rights. We can download the the portable version of GVim and install it anywhere. I personally use C:\Users\ubaldot\AppData\Roaming\vim\vim91 as installation folder.
Next, create a shortcut to the installed gvim.exe
and call it start_gvim_sever
.
Right click on the shortcut, select Properties and change the field
Target to C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& 'C:\Users\ubaldot\AppData\Roaming\vim\vim91\gvim.exe' --servername VIM"
or something similar, depending on where we have powershell.exe
installed.
At this point, by double-clicking on the newly created shortcut, the GVim server is launched.
Note that we can automatically start the server at Windows startup by copying the shortcut in the Windows Startup folder.
To open such a folder you can hit Win+r and type shell:startup
.
Next, assume that .txt
file extensions are associated to C:\Users\ubaldot\AppData\Roaming\vim\vim91\gvim.exe
.
If we double-click on a .txt
file, then a new instance of GVim is created instead of re-using the existing instance.
This happens because when we double-click on a text file, then the program gvim.exe
is launched,
whereas we would need to invoke C:\Users\ubaldot\AppData\Roaming\vim\vim91\gvim.exe --servername VIM --remote-silent /path/to/file
to re-use the existing instance.
Although the issue can be nicely accommodated through a .bat
file, we cannot associate
file extensions to .bat
files, but we can only associate them to .exe
programs.
However, in-spite we can convert .bat
files to .exe
programs through some external tool, they may need to run in Admin mode,
which is something that may not be allowed, as it is in my case.
A solution would be to resort to the good old Python.
That is, we write a simple Python script and we create a .exe
program from it through the pyinstaller
package.
A simple Python script to achieve such a goal could be the following:
r"""
Script for creating an executable to open GVim in a client-server fashion.
You need to have a Vim server running first.
"""
import subprocess
import sys
import os
# Adjust this path to your Vim install
gvim_path = r"C:\Users\ubaldot\AppData\Roaming\vim\vim91\gvim.exe"
if len(sys.argv) > 1:
file_path = os.path.abspath(sys.argv[1])
subprocess.run([gvim_path, "--servername", "VIM", "--remote-silent", file_path])
else:
subprocess.run(
[gvim_path, "--servername", "VIM"]
)
Save the above script as open_in_vim.py
and download a nice icon
for the modified GVim .exe
program that we are going to create and call it vim_nice_multi.ico
.
Let’s assume we name the modified GVim launcher open_in_vim.exe
and place it — along with the icon we just downloaded,
— in C:\Users\ubaldot\AppData\Local\bin.
To generate open_in_vim.exe
from the Python script above, run the following command:
pyinstaller --noconsole --onefile --distpath "C:\Users\ubaldot\AppData\Local\bin" --icon="C:\Users\ubaldot\AppData\Local\bin\vim_nice_multi.ico" open_in_vim.py
Finally, all you have to do is to right click on a .txt
and select Open with... and
select C:\Users\ubaldot\AppData\Local\bin\open_in_vim.exe
. Confirm by clicking on Always.
Next, when you double-click on a .txt
, it will open in the running GVim instance.
Note that if you cannot see the correct icon, either the icon is not good for Windows,
in which case you have to pick another icon or you have to convert the existing,
or you need to kill explorer: open cmd.exe
and run taskkill /im explorer.exe /f & explorer.exe
.
We have just seen how to install GVim and how to re-use existing GVim instance when double clicking on text files when we don't have Admin rights on Windows 11 in an efficient way, as vimrc, plugins and eventually scripts are loaded only once.