31

As part of the program I wrote, I constantly read and write data from files. I noticed that as part of doing so, I am inadvertently creating swap .swp files.

What do you think is going on? What would cause swap files to appear if you had to reproduce the problem?

40

The .swp file is not a swap file in the OS sense. It is a state file. It keeps your changes since the last save (except the last 200 characters), buffers that you have saved, unsaved macros and the undo structure.

You can read more in VIM's help: vim +help\ swap-file. If there is a crash (power failure, OS crash, etc.), then you can recover your changes using this swap-file. After saving the changes from the swap file to the original file, you will need to exit vim and remove the swap file yourself.

19

.swp files are created by Vim, they should be automatically deleted when you close the file and exit the editor.

  • 1
    Can they be created from outside of Vim? In my case files are written from my program directly, without using the editor – JAM Dec 29 '11 at 19:31
  • @JAM: What language and libraries is this program written with? – jwodder Dec 29 '11 at 20:07
  • 5
    @JAM Generally no, it is a Vim feature. I doubt these files are being written directly from your program without you knowing, it doesn't make sense. It is much more probable that you are confused, these files are being created while you edit the files with Vim, and you are attributing them to your program. – Juliano Dec 29 '11 at 21:34
4

.swp files are nothing but a kind of lock file which you editor, generally vim, creates to indicate that file is being edited. This way if you open the file in another vim instance of if someone in the network did that, they'll see a warning that the file is being edited.
You need not to delete them manually. You editor will remove the swap file once you close the file in your editor.

2

I just executed a mv command on a 31GB file and a 13GB .swp file was created, no editing involved.

mv X.csv Y.csv created Y.csv and .Y.csv.swp

CentOS 6.5, ext4 file system

  • Thanks. Have you had a similar experience with `rsync' too? – imriss Jun 18 '18 at 19:46

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.