104

Reasons for swap files Swap files store changes you've made to the buffer. If Vim or your computer crashes, they allow you to recover those changes. Swap files also provide a way to avoid multiple instances of Vim from editing the same file. This can be useful on multi-user systems or just to know if you have another Vim already editing a file. ...


26

Don't Panic! Recovering files is always safe, because vim never writes any changes until you write them yourself. So recovering from a swap file and then closing without saving won't change anything. Organize your swapfiles: Get vim to put all your swap files in the same place, so you always know where they are: set directory^=$HOME/.vim/swap// or ...


20

Try this: run vim file1 in a terminal, then run vim -p file1 file2 in a different terminal. The second command will prompt you as above. If you answer Quit, you still get to edit file2. If you answer Abort you just quit Vim, thus "aborting any further commands".


16

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


11

Edit the same file again. Vim will prompt you with a few options; say you want to Recover. Then save the file, quit Vim, and remove the swap file (otherwise Vim will prompt you again the next time you edit the same file). The result may or may not have your latest edits, but it's better than nothing. Do check that everything is ok.


10

Yes it saves to swap file. You can do a quick test to get a feel of this. Go to a directory. Say vim notest.txt and write some text. You do not even have to save the file. If you list files in the working directory you will find a swap file. Copy it to a different directory (where you have no "notes.txt" file, as say vim notest.txt. You get a note about ...


10

Through the SwapExists event, you can influence the choice (and skip the dialog) by setting the special v:swapchoice variable: autocmd SwapExists * let v:swapchoice = "e" (I personally would at least add an | echomsg "Concurrent editing" to notify that there's concurrent editing going on.)


8

Is it possible to only have .*.swp-files, when file on disk and file in vim are different? Yes. The snippet below (adapted from tpope's vimrc) will disable the swap file for a buffer when it isn't modified, so swap files only exist for modified files. autocmd CursorHold,BufWritePost,BufReadPost,BufLeave * \ if isdirectory(expand("<amatch>:h")) |...


8

vim -r at the command line will list all swap files in the current directory and temporary directories, and whether they contain any unsaved changes. Look for the line that says modified: no/YES. I don’t know how to tell Vim to look in a different directory, so you’ll need to change to each directory that contains a swap file and run vim -r. You could come ...


6

The "Delete it" option isn't displayed if the Vim process is still running; I can't find this documented anywhere but I looked it up in the source code; from memline.c, line 4512 (slightly simplified): do_dialog( [..] process_still_running ? (char_u *)_("&Open Read-Only\n&Edit anyway\n&Recover\n&Quit\n&Abort") : ...


6

Vim does not interpolate the ~ when using the let & syntax. You must do this manually using expand(). se nobackup writebackup let s:vim_cstmztn_files_dir=expand('~/tools/Vim_Files/') let &directory=s:vim_cstmztn_files_dir.'vim_backup' let &backupdir=s:vim_cstmztn_files_dir.'vim_backup'


6

:swapname This will show the filename of the swapfile for the current buffer. (For example: /c/path/.swu) I found this (after a few tries) by typeing :swTAB. I also found this question: VIM swap-file best practices?, which includes some other interesting and useful commands.


5

The options you need are directory and backupdir they allow you to set the directory where swap files and backup files are stored. The second part of the problem is how to detect that you are editting files in a webdav directory to set these options. I would have tried an autocommand to do so but according to this discussion it seems to be pretty hard: From ...


5

You can combine autocommands with the getfsize(file) function to automatically disable options for large files. " A global variable that contains the size of which a file is considered " large. In this case, it is 10 megabytes. let g:large_file = 1024 * 1024 * 10 " This autocmd runs before reading the file into the buffer. It " gets the file that the ...


5

I think you might be misinterpreting the meaning/operation of the swap file. The swap file might indicate that some other person/process is currently editing that file, but it doesn't necessarily mean that. If an instance of Vim crashed, for example, the swap file will hang around until you take steps to remove it, by e.g. selecting the (D)elete option at ...


4

I agree with you, and this seems like it could be improved, because one of those you would think would not create the buffer, and I just verified that I see the same behavior you describe, but if I were in your shoes what I would do to guarantee the buffer is immediately destroyed and which comes close to automatic is [O]pen Read-Only followed immediately by ...


4

When writing a buffer Vim will create a temporary file which it will fchown() to verify it has permission to create files in this directory; simplified version from buf_write() in src/fileio.c: /* * Check if we can create a file and set the owner/group to * the ones from the original file. * First find a file name that doesn't exist yet (use some * ...


4

@FDinoff suggested checking the vim help. In case anyone wanted to see these more easily: WHAT TO DO? *swap-exists-choices* If dialogs are supported you will be asked to select one of five choices: Swap file ".main.c.swp" already exists! ~ [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it: ~ ...


4

One solution would be to use a single instance of vim per project, which would prevent this issue unless your multiple projects had overlapping files. I'm going to assume that you are looking for a automated solution, instead of a suggestion on changing your behavior though. There is the possibility of another solution in FocusLost that is more native to ...


4

You could try :noswapfile e FILENAME: :nos[wapfile] {command} *:nos* *:noswapfile* Execute {command}. If it contains a command that loads a new buffer, it will be loaded without creating a swapfile and the 'swapfile' option will be reset. If a buffer already had a swapfile it is not removed and 'swapfile' is not ...


3

If you set A option Vim will just forget about swap at all. You'll stop getting messages about a swap file being found and if you're editing the same file in different terminals at the same time, it's your problem <3 EDIT: What I mean is that Vim will not care about the swap file Let's put a example: You open main.c in terminal A, and you write ...


3

Long lost and almost forgotten. Meant to re-factor the code etc. but post is as is. This code is used on a 32-bit system. Not sure how it would behave on 64-bit (or other). Have a hack in C-code somewhere as well, (extracting PID). Leave it as a post for anyone to hack and tweak. My current script as a whole (using pid as option it should work on any GUI ...


3

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 Explained: " start diff in current window with restored swap file :diffthis " read origin file in new vsplit :vnew | r # " delete ...


3

Personally, I have turned off swap and backup files, as it tends to create swap files and backup files everywhere and frequently creates conflicts. Vim's persistent-undo has been much more reliable and error-free. Vim currently has persistent undo, its a much more fine grained method of backup. Every time, you press Esc, the current state of the file is ...


2

I think these are the vim backup files. This should turn them off: set nobackup There will still be one while vim is actually writing the file (e.g, during the :w command; use set nowritebackup to disable). There is also a swap file, another temporary file. That exists only when vim is running; its purpose is to recover unsaved changes if e.g., vim ...


2

It seems that nvi uses a special "index file" in /var/tmp/vi.recover/, named recover.XXXXX (where XXXXX is randomly generated). On my system the contents look like: X-vi-recover-file: /home/martin/recover X-vi-recover-path: /var/tmp/vi.recover/vi.AI8Iqs Reply-To: root From: root (Nvi recovery program) To: martin Subject: Nvi saved the file recover ...


2

You can find information about the format from the source. It starts with a block named block0 which holds basic information, see below. The rest of the file is copies of memory blocks in a tree structure. See description in top of source file. The base information could easily be extracted. I.e: $ ./vimswap .foo.swp b0_id 6230 Version VIM 8....


2

This is probably a netrw bug. You might be able to work around by creating a file ~/.vim/after/ftplugin/netrw.vim (create non-existent directories) and put in there :set noswapfile


2

I believe the option you're looking for is autoread. To use it put this in your vimrc: set autoread From :h 'autoread': When a file has been detected to have been changed outside of Vim and it has not been changed inside of Vim, automatically read it again. When the file has been deleted this is not done.


2

Didn't intend to reply to my own question, and this is not necessarily better than the swapfile method, but this works too: :w then :e! I initially tried :e as I mentioned but I hadn't tried :e!. And I still have undo history with :e! (tested only on Vim 7.4), just like with the swapfile solution, but... The negative with this solution of course is that ...


Only top voted, non community-wiki answers of a minimum length are eligible