4

I often try to edit a file with Vim that is already open in another Vim instance. In this case, you get the error dialog "Swap file "..." already exists!" with options Open Read-Only, Edit anyway, Recover, Quit, and Abort.

In almost all cases, I would simply like to switch to the Vim instance that is already open, and continue editing it. Instead, I have to close this attempt with Quit or Abort and then hunt for the open window of the other instance (which may be difficult if there are many open windows, some hidden, some on another workspace, etc.)

Is there any way to do this?

Keywords: gvim, editexisting, foreground, window focus, raise window.

4 Answers 4

0

You could work around this by setting up your environment to use Vim's client/server feature.

Aliasing vim to vim --servername VIM --remote may be enough to solve your "problem".

But I think you should probably revise your workflow and open related files from vim itself.

Sign up to request clarification or add additional context in comments.

1 Comment

'gvim --remote' and 'gvim --remote-silent' will open all files in the same VIM instance, i.e., all your files will be in 'vim windows' inside a single Window Manager window. Not necessarily what the OP asked. The 'workflow' described by OP is perfectly normal and should need no revision. In particular, this workflow (and the requested behavior) is expected if one double-clicks a file from a GUI file manager - it should find and raise an existing editor for it, or open a new one.
0

I would suggest putting the following line in your .bashrc file

alias vim='vim --remote-silent' 

That way if the file is already open you won't get the annoying "Swap file..." message and if it is not already open you won't be shown an error.

1 Comment

This is the same as a previous answer - and will open all files in the same desktop window, not necessarily what's desired. (see my comment in the other answer).
0

Install wmctrl, then you can write a script aliased to vim or gvim that looks in your /tmp directory for the existence of a swap file with the same name. If it exists, then you use wmctrl to bring the right window to the foreground, otherwise call vim/gvim as appropriate.

Comments

-1

The 'edit existing' plugin for vim/vim.gtk3 which worked well in previous installs and did exactly what the OP is requesting, got broken on Xubuntu 24.4 (with vim9.1).

This worked out-of-the box on Ubuntu 20.4 and 22.4 - they had a script named 'editexisting' in the /usr/share/vim/vim(version)/pack/dist/opt/editexisting/plugin/ directory, which was auto-activated via a script in the /usr/share/vim/addons directory.

This is no longer the case in the vim9 install on Ubuntu 24 - and even if you enable it by adding packadd! editexisting in your ~/.vimrc file, it finds the existing instance, but fails to raise its window to the foreground.

Here is what I did on Xubuntu 24.4, which fixed it for me.

(1) copy the editexisting.vim script to a place where it will be auto-loaded (don't do this, if on your OS it is already being loaded, but just doesn't work - instead, edit the file where it is as per step (2) below, or disable the OS installed copy before making your own). Note, the file location is for vim9.1, change as needed:

mkdir -p ~/.vim/plugin cp /usr/share/vim/vim91/pack/dist/opt/editexisting/plugin/editexisting.vim ~/.vim/plugin 

Try it out: when re-opening a file that's already edited, gvim will no longer display an error about the swap file existing, instead it should show a message like File is being edited by <instancename>.

If that works, but the window isn't raised, do:

(2) make a replacement for the vim's 'foreground()' function, which no longer works on some window managers (it is BROKEN on my Xubuntu 24.04, for example).

# get wmctrl, if not installed already # (use your OS package manager in place of 'apt-get install', as needed) sudo apt-get install wmctrl mkdir -p ~/bin # or ~/.local/bin or whatever you have in the PATH cat >~/bin/raise_gvim <<"END" #!/bin/sh [ -z "$1" ] && exit 1 wid=`wmctrl -l | awk '/ '"$1"'$/ {print $1}'` [ -z "$wid" ] && exit 1 # echo $wid wmctrl -i -R "$wid" # NOTE: use '-a' instead of '-R', if you prefer to have the WM jump to the workspace # where the window is, rather than bring it to the current workspace. END chmod +x ~/bin/raise_gvim 

Re-start your shell, to ensure ~/bin is in the PATH (required, if you just created ~/bin - in many distros, the /etc/profile script doesn't add ~/bin if it doesn't exist).

Test it, before proceeding - the wmctrl command worked for my window manager, but there's no guarantee that it will work for you. Edit something with gvim, note the instance name, it will normally be something like GVIM, GVIM1, GVIM2, ... Switch to a shell window and run this:

raise_gvim GVIM1 # replace the instance name, as needed

If this raises your editor window, you're almost set. Now, edit ~/.vim/plugin/editexisting.vim and replace

call remote_expr(servername, "foreground()")

with this:

exe ":silent !raise_gvim " . servername

NOTE: the old answer suggesting the use of --remote or --remote-silent DOES NOT WORK. First, it will mess up if you do 'gview' on a file that's already open with 'gvim' or vice-versa. Also, it will cause all files to be edited in the same instance of GVIM (in a single window-manager window) rather than open a new one for each new file. That's not what most people want when they use the GUI variant of VIM.

1 Comment

Anonymous downvoter: kindly add a comment explaining why you found the answer unsatisfactory. I always do that if I downvote - to give the author an opportunity to correct/improve the answer (or delete it, if proven entirely unusable).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.