8

Using vim I keep getting a message saying "Swap file xxx already exists" when I'm editing an apache config. However, I don't see it in the working directory on in tmp. How do I delete this?

migrated from serverfault.com Feb 29 '12 at 3:57

This question came from our site for system and network administrators.

13

Vim swap files are normally hidden (Unix hidden files begin with a .). In order to view hidden files as well as regular ones, you need to ls -A (mnemonic: A for All). That should show you whether a swap file is there or not.

5

This happens only when the file you were trying to edit is not saved completely on to the disk. Scenarios are :

  1. You shut down your system improperly, when a file is still open in vi/vim.

  2. when your file is being accessed concurrently via different/same consoles in vi.

vi/vim creates a .swp file whenever a file is opened in vi/vim. Everytime a file is opened in vi/vim, vi/vim checks for this file and if found this warning pops up

This is can be avoided in two ways

  1. one by deleting the .swp file which is named as

"path/of/the/file/you/were/editing/.your_file_name.swp"

  1. you can also opt for recover mode of vi/vim when opening the file and pressing recovery option .

I am just talking about the first scenario and it is safe for only that scenario. Scenario second is the different case.

  • If there were unsaved changes, you can recover them using vi -r (or vim -r if you prefer). I usually write the recovered file to a temporary file and diff it against the main file. (I don't quite trust the recovery not to lose information.) – Keith Thompson Feb 29 '12 at 3:26
1

If the file you're editing is called httpd.conf then the swap file will be called .httpd.conf.swp. It is hidden unless you add -a to your ls arguments.

ls -a /etc/httpd/conf/
1

To remove the swap file in Linux use this command

rm .filename.swp
0

In the message, you should see the full path to the file. You can rm it by copy-pasting the path as follows:

rm -f /path/to/the/file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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