Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I get rid of the annoying warnings when I open a file that has a .swp file? Or, how do I not generate the .swp files at all? Example warning:

E325: ATTENTION
Found a swap file by the name ".notes.swp"
          owned by: james   dated: Fri Dec  3 17:38:07 2010
         file name: ~james/school/se/project-dir/rottencucumber/doc/notes
          modified: no
         user name: james   host name: james-laptop
        process ID: 2251 (still running)
While opening file "notes"
             dated: Fri Dec  3 18:46:10 2010
      NEWER than swap file!

(1) Another program may be editing the same file.
    If this is the case, be careful not to end up with two
    different instances of the same file when making changes.
    Quit, or continue with caution.

(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r notes"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".notes.swp"
    to avoid this message.

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

4 Answers 4

up vote 9 down vote accepted

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.

share|improve this answer

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) has changes that have not yet been saved.

At this point, delete the original file, and rename the second file as the original, or simply do it in one move command:

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

... then remove the swap file:

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

Or, do the last two steps in a slightly different order, by first removing the swap file and the original file:

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

... and then rename the second file as the original:

mv notes2 notes

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.

share|improve this answer
    
Can't believe this isn't the accepted answer. It's perfect. +1 –  David Aug 25 at 16:01

vim will remove the generated .swp file when you exit it properly.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.