I have a way to handle swap files.

And it works but it takes a while.

Here goes:

  • First I try to find the original vim session -- track down if the PID is still alive -- and if the PID is still alive I go try and find that vim session and save the file and quit... and I'm done (stop here) -- If I can't open the vim session I kill the PID... and have to recover the file.

  • try to view the diff

    • I hit r to recover the file
    • save the recovered file to a temporary file
    • quit vim
    • do a command line vimdiff between the two
    • manually fix any conflicts
    • save the file
    • quit vim
    • delete the temporary file
    • reopen vim
    • done

I wanted to find a way to do the same thing but more efficiently.

How do I view the diff of the file with the recovered file, resolve any differences, save the file without quitting vim or using plugins?


Here is the vim commands you need to view the diff, resolve any differences, save the file without quitting vim or using plugins:

  • from the command line open the file
    • vim path/to/file
  • all the rest of the commands are inside of vim
  • recover the file
    • r
  • save the recovered file (if the destination file exists, then overwrite)
    • :sav! ~/.recovered
  • open the original (not-recovered file) in a new window
    • :vsplit
    • ctrl-w w
    • :bn
    • e
  • now we have the recovered file on the left and the un-recovered file on the right
  • diff the two files
    • :diffthis
    • ctrl-w w
    • :diffthis
  • now we have a diff of the two files (see man vimdiff)
  • resolve any conflicts (see man vimdiff for more info)
  • save off any changes made to the unresolved file
  • delete the swap file -- :!rm -v path/to/.file.swp
  • quit vim
    • :q

I am sure there are ways to improve my solution and if you know of improvements please edit my solution!

I started with the solution from http://vim.wikia.com/wiki/Swap_file_%22...%22already_exists!_-_so_diff_it

I modified the linked solution because

  • the linked solution does not handle opening a file that is under a directory
  • the instructions were not as clear as I would prefer
| improve this answer | |
  • 1
    I won't edit your solution, because I don't know if it's really an improvement or not, but personally I would do :windo diffthis instead of :diffthis <C-w> :diffthis. Also, I believe the <C-w>s in your answer should be <C-w> ws instead; this will switch between windows, which seems to be the goal. – brhfl Mar 15 '18 at 18:28

View diff of a swap file without plugins

Without plugins, you can use vim alternate-file. Wikia source: Diff current buffer and the original file

In short, try this:

:diffthis | :vnew | r # | exe "norm! ggdd" | :diffthis


" start diff in current window with restored swap file

" read origin file in new vsplit
:vnew | r # 

" delete first empty line 
:exe "norm! ggdd" 

" start diff mode in vsplit

View and merge diff of a swap file using Recover plugin

Also, there is a plugin Recover.vim.

Found a swap file by the name "test/normal/.testfile.swp"
        owned by: chrisbra   dated: Wed Nov 28 16:26:42 2012
        file name: ~chrisbra/code/git/vim/Recover/test/normal/testfile
        modified: YES
        user name: chrisbra   host name: R500
        process ID: 4878 [not existing]
While opening file "test/normal/testfile"
            dated: Tue Nov  6 20:11:55 2012
Please choose:
[C]ompare, (O)pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete:

After merge, you can delete swap-file using command :FinishRecovery

| improve this answer | |
  • 1
    Hi @BLayer, thx. Updated answer - added solution using alternate-file – Yasen Jul 2 '19 at 9:40

Based off Trevor's answer, I made the following commands for dealing with swap files:

noremap <LEADER>s <C-w>o:sav! ~/.vim/.recovered<CR>:vs<CR><C-w>w:bn<CR>
noremap <LEADER>t  :wa<CR>:bp\|bd #<CR><C-o>

Upon reaching the ATTENTION: Found a swap file... prompt, I

  • type r to recover the swap
  • type <LEADER>s to save the swapped version as .recovered and open the original in a new split
  • type e at the swap prompt to edit the original file
  • type :windo diffthis to diff the two files
  • make any necessary changes to the original
  • type <LEADER>t to write the original file, close it, and reopen it
  • type d at the swap prompt to delete the swap file
  • close the .recovered file (typing something like :bp<CR>:bp\|bd #<CR>).
| improve this answer | |

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.