How do I deal with vim's swap file system?

24,599

Solution 1

This is not related to Ubuntu alone, since what happens is a base mechanism of nearly every Unix OS. By pressin ^Z you ave suspended (not ended) the currently running vim session. The vim session is still there and waiting for a signal to put it to foreground again.

To reactivate the session: If vim was started directly from the commandline -- use the command "fg" (which is for "ForeGroung") and vim will appear again. This works on all ksh/bourne-like shells. For t/csh I dont know. This only works when the command "fg" was given on the console of the same terminal session as from which vim was started (which is the "controlling terminal" related to the vim session).

If vim was started (mostly under the name gvim) from a menu of a windowmanager you are a little bit out of luck here, since (g)vim gets detached from its controling terminal.

Your options to recover: Use "fg" if the condition is valid described above. This is the cleanest way.

If the (g)vim session is detached from the controling terminal, which can be checked by doing a "ps -ef | grep vim". If the column for the TTY (see header of the output) shows a "?" there is no controling terminal anymore, I would recommend to send the process a SIGHUB (see manpage for the commmand "kill"/"killall") and then a SIGKILL if it is still there. Killing vim (or any other task) will probably result in inconsistent data though, cause there was no "save" command to vim before it is killed.

After that, start a new vim with the same file, do a "recover" first (as offered by vim, which sees the according swp-file) , save the file, end vim and start it again with that file and do a "delete swap file". This is the savest way possible after killing vim.

To avoid accidentically putting vim into background if not wanted, map ^Z to another, more "complicated" keysequence, which is hard to press accidentically. You can deactivate the ^Z command by adding the following line to your .vimrc:

map <C-z> ;

Addition: Your rm-command misses the dot in front of .test.swp causing rm not to find the file...or deleting another file, which is named test.swp instead of ".test.swp". By deleting swp-files via vim, you are sure to delete the correct file. Swp-files always start with a dot (hidden file) on UNIX like systems.

Solution 2

the file is going to be " .test.swp ", not " test.swp " So you'll want to:

rm .test.swp
Share:
24,599
reesjones
Author by

reesjones

Updated on January 27, 2020

Comments

  • reesjones
    reesjones over 4 years

    When using vim in ubuntu, I accidentally pressed ctrl-z which suspended my session of vim. I was editing a file (I'll call it test) which was not saved.

    When I opened the file again in vim, I got the swap file error:

    E325: ATTENTION
    Found a swap file by the name ".test.swp"
    
    
    Swap file ".test.swp" already exists!
    

    According to Found a swap file by the name question, I have two options:

    1. Find the session and finish it (preferable).
    2. Delete the .swp file (if you're sure the other git session has gone away).

    How would I do either of those things? If I perform rm test.swp it doesn't see the file:

    rm: cannot remove `test.swp': No such file or directory
    

    What am I doing wrong in the deletion of the swap file and how can I finish the session?


    EDIT: I forgot the period in test.swp So the correct way to remove the swp file is rm .test.swp.

    My remaining question is how to resume/finish a suspended session of vim.