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

47

9

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:

James

Posted 2010-12-04T06:41:02.310

Reputation: 4 189

Answers

45

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.

user664833

Posted 2010-12-04T06:41:02.310

Reputation: 756

2Can't believe this isn't the accepted answer. It's perfect. +1 – David – 2014-08-25T16:01:27.433

1It's not always the case that the message is useful. If I'm editing a file, I care if the file has changed since I started editing, at the moment I try saving it, not whether I'm also editing it in another vim window. It's perfectly fine for me to open a file in two terminals, so long as saves to the file don't interleave. Vim only really needs to nag me when I try to save, not when I open it. – Ken Simon – 2017-08-03T17:35:35.687

Unfortunately this doesn't answer the question, it only says that I should not ignore the nag screen. MY scenario is that I've already got the file open in vim (for editing, maybe), want to see different parts of the file at the same time side by side, and don't want split buffers because clang_complete keeps resizing the buffers. In this case, I actually do want to know how to open a file readonly in Vim and have it ignore the swap file. – Lelanthran – 2019-08-07T20:39:21.923

@Lelanthran vim -n file_name – user664833 – 2019-08-07T23:18:07.743

19

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.

Heptite

Posted 2010-12-04T06:41:02.310

Reputation: 16 267

1BE CAREFUL! I almost lost all my work messing around with VIM. Copy your code before you try anything. – Squirrl – 2015-04-16T16:39:44.210

I strongly suggest readers review the answer by user664833 (This message is actually pretty important...) as I think it gives much better informatgion and advice that this answer imho – Michael Durrant – 2017-03-18T13:26:10.050

11

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

Brad Parks

Posted 2010-12-04T06:41:02.310

Reputation: 1 775

3I wouldn't recommend always running without a swapfile, but "-n" is actually precisely what I needed. I use a swap file, normally. However, when using Vim as a diff tool, I'd like to be able to view diffs of files I'm editing without those diffs giving me this message or interfering with my swap file. – Agentlien – 2015-07-14T08:26:04.213

1I like this answer - it's short, it's accurate and it answers the actual question! – Lelanthran – 2019-08-07T20:40:15.527

2

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

Ignacio Vazquez-Abrams

Posted 2010-12-04T06:41:02.310

Reputation: 100 516

1

How can I get rid of the annoying warnings when I open a file that has a .swp file?

Use the short message option in your ~/.vimrc:

set shortmess=A

Which is also the default when running POSIX mode.

However, do not do this: it's like removing your car's air bags to increase fuel efficiency. In the long run, you'll spend more time recovering than you ever did answering the question.

bishop

Posted 2010-12-04T06:41:02.310

Reputation: 150

1Assuming a single-user system, it's not that horrible of a configuration. I like to have lots of terminal tabs open, and sometimes I want to edit a file that I already have buried in some other tab. If I want to make a few quick edits that's fine. If I find myself back in the other vim window, the beauty is that it'll warn me then that the file has changed before I save it, and I can do a quick :e to see what's happened (including a quick u to undo back to what I had in the buffer.) I like that a lot better than being nagged about already having a file open. – Ken Simon – 2017-08-03T17:47:33.870

1

This happened to me when my terminal unexpectedly closed my connection with vi open.

Thankfully I had no changes lost.

To remove that warning and discard the swap:

  1. Find the process id looking at process ID: 2251 (still running), in this case it's 2251
  2. Verify you got the right id ps 2251
  3. Kill the process using kill 2251

And that's it.

Remember swap files are there to protect you, and you should not disable them completely.

kintsukuroi

Posted 2010-12-04T06:41:02.310

Reputation: 111

But what if there is no vi(m) process? – Scott – 2018-04-02T05:00:43.560

-2

I have solved this issue in three simple steps :

1:) Run "ls -la" where the file exist so that you can view the .swp file for the same.

2:) Now "rm -i *.swp" i.e., remove the ".swp" file from the directory .

3:) Now confirm wether the ".swp" file has been removed .

4:) Now enjoy from those warnings.

Ankur Srivastava

Posted 2010-12-04T06:41:02.310

Reputation: 1

3Why in the world would you suggest rm -rf for removing a file? I replaced -rf with a more sensible -i – Nifle – 2015-02-04T16:34:19.517