How to get rid of the warnings when opening a file that has a .swp file?

This message is actually pretty important if you care about not losing text you've potentially not saved. It should not be considered annoying, and should not cause you to hastily delete the swap file or configure vim to run without it.

Any file you edit with vim will have a corresponding swap file while you edit, which vim uses to keep track of changes. When you quit editing a file, vim will automatically discard the corresponding swap file. Therefore, the existence of a swap file, and your attempt to write overtop the original file, should be cause for consideration and appropriate action.

The two scenarios presented in the message (E325: ATTENTION Found a swap file) are actually quite common: (1) either another vim program is editing the very same file you're trying to edit (it could actually be another person - in which case it really wouldn't make sense to just blindly delete the swap file - or it could be you in another terminal window or tab), or (2) a previous vim session crashed (most often this happens when you're editing remotely, and the network session is severed - in which case the vim session was not exited normally, and the .swp file remains behind; another example of this second scenario is that you've accidentally closed the terminal window or tab that had an active or backgrounded vim session).

When I encounter this message I first think about whether I am editing this file in another terminal window or tab, as I normally operate with several terminal windows with several tabs each:

enter image description here

If I realize that I am editing in another place, and can return to it, I then press the q key to (Q)uit this additional session, and return to editing via the original vim session.

Sometimes if I am not entirely sure, I (Q)uit and then run jobs to verify whether I am running vim in the exact same terminal; if nothing comes up, I run ps -ef | grep vim to check whether I am running vim elsewhere (i.e. in another terminal window or tab). The point is, I always try to resume editing via the original vim session.


If I am certain that I cannot return to the original editing session, and I am still presented with the following options, then I press r to (R)ecover.

Swap file ".notes.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

Pressing r, you will see a message like this:

Swap file ".notes.swp" already exists!
"notes" 18L, 46C
Using swap file ".notes.swp"
Original file "/private/tmp/notes"
Recovery completed. Buffer contents equals file contents.
You may want to delete the .swp file now.

Press ENTER or type command to continue

If, on the other hand, I am no longer faced with those options, because I am at the shell prompt, then I run vim with the -r option, as follows:

vim -r notes

The resultant message will be similar:

Using swap file ".notes.swp"
Original file "/private/tmp/notes"
Recovery completed. Buffer contents equals file contents.
You may want to delete the .swp file now.

Press ENTER or type command to continue

Either way, press ENTER to continue, and you will see your file.

Note: If Vim has doubt about what it found, it will give an error message and insert lines with "???" in the text. If you see an error message while recovering, search in the file for "???" to see what is wrong. You may want to cut and paste to get the text you need.

The most common remark is "???LINES MISSING". This means that Vim cannot read the text from the original file. This can happen if the system crashed and parts of the original file were not saved.

That said, I have never seen those ??? marks, so this must be a truly rare occurrence.


Next, save (i.e. write) the content to another file (usually I just append "2" to the end of the original file name):

:w notes2

Next, force-quit this vim session:

:q!

Next, compare the two files:

diff notes notes2

If the diff returns nothing, that means there is no difference, and it is safe to remove both the swap file and the second file:

rm .notes.swp notes2

At this point, open the original file and proceed as if there had never been a problem:

vim notes

If the diff returns something, that means the original file (via the swap file) had changes that, thanks to the recover, have been saved to the second file.

Since those changes are captured in the second file you are safe to delete the swap file and overwrite the original file with the second one:

rm .notes.swp
remove .notes.swp? y

mv notes2 notes
overwrite notes? (y/n [n]) y

At this point, open the original file and proceed as if there had never been a problem:

vim notes

This seems like a lot of work, but once you get used to the workflow it takes like 20 seconds max.


The pasted message suggests you still have the "notes" file open in another Vim session. It is definitely not a good idea to edit a file that is being edited elsewhere.

If that message is wrong, you need to determine how your Vim session exited improperly and avoid that in the future.

As for recovering, that may be an issue in this one instance since "notes" is newer than the Vim .swp file associated with it, but you can still try. Just make sure you copy your "notes" file to a backup location first.

It would be a good idea to read through ":help recover.txt".

The lesson here is that you're not supposed to be seeing this message, and that you are means Something Went Wrong Somewhere. It's (probably) not the fault of Vim.

If at this point you are still absolutely determined not to let Vim create .swp files so you can recover from crashes and other related issues, you may put "set noswapfile" in ~/.vimrc.


You can use -n to open vim without using a swapfile:

To do this all the time, just put

alias vim='vim -n' 

in your .bashrc or .bash_aliases file

if you ever need to run vim without the -n option, just run

\vim

Tags:

Vi

Vim