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?
5 Answers
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.
This happens only when the file you were trying to edit is not saved completely on to the disk. Scenarios are :
You shut down your system improperly, when a file is still open in vi/vim.
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
- one by deleting the .swp file which is named as
"path/of/the/file/you/were/editing/.your_file_name.swp"
- 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
(orvim -r
if you prefer). I usually write the recovered file to a temporary file anddiff
it against the main file. (I don't quite trust the recovery not to lose information.) Commented Feb 29, 2012 at 3:26
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
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/