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?

4 Answers 4


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.

  • For some reason, my ~/.swp file is 11.7GB. I just deleted it. Commented Nov 24, 2019 at 11:54
  • Can you explain what "last 200 characters" means? Also, if it keeps the undo structure, does this mean that it keeps all the unsaved changes?
    – ado sar
    Commented May 31 at 20:25

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

  • 2
    Can they be created from outside of Vim? In my case files are written from my program directly, without using the editor
    – JAM
    Commented Dec 29, 2011 at 19:31
  • 1
    @JAM: What language and libraries is this program written with?
    – jwodder
    Commented Dec 29, 2011 at 20:07
  • 6
    @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
    Commented Dec 29, 2011 at 21:34
  • 1
    What will happen if we don't delete them?
    – Jdeep
    Commented Sep 25, 2020 at 13:52

.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.


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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .