Vim, automatically remove swap file when file is saved - vim

Is it possible to automatically remove the swapfile when the file is saved, and automatically create it again when the file is changed?
And is this a good idea?
EDIT:
The reason for wanting to to this is so that I don't have to go through the diff process for files that had no changes after last save if the computer suddenly ran out of power or crashed.
The diff process referred to is; first open the file read only, saving it under a different filename, vimdiffing it and in the end delete it.
It doesn't happen often, but is just as annoying when it does.

Is it possible to automatically remove the swapfile when the file is saved, and automatically create it again when the file is changed?
And is this a good idea?
Deletion and creation of a file are relatively slow operations. It makes sense to keep the swap file in case if one going to keep editing. Or why else one would keep the VIM running?
Additionally VIM uses the swap file to detect attempts to edit the same file from different VIM instances.
All in all, I think it is a bad idea to try to remove the file.
P.S. Worth noting that when VIM is started in read-only mode as view or vim -R, it doesn't create the swap file. But if once you change the file, the swap file is going to be created.

I am assuming you have a long running vim session like I do (my first window in my screen session is always vim and it usually lives for several days, almost for the entire duration of the uptime). I think the swapfile is not kept around, if you simply close the buffer, unless you have the 'hidden' setting. What happens is that when you have the 'hidden' turned on for buffers, vim tries to keep all the undo history of the buffer, and so has to have the swapfile accessible, in case you come back to the buffer and start undoing changes. As for me, I don't like this feature and keep it turned off (which is the default) and so only have to deal with the swapfiles for those buffers that I actively have loaded in a vim window at the time of crash. Since I have macros that autosave my session every 5min., and since I also disable the Vim's default swapfile detected prompt, restarting my session after a crash is fairly painless.

Related

Git hub commit issues

Hi I am trying to update one of my remote repositories. When I use commit in git bash it shows that a SWP file already exists and askes me if I want to (R)ead only the file (E)dit (D)elete. I tried editing the file, but I cannot save the changes and therefore cannot commit my changes. Any idea what is going on.
EDIT:
It's working now thanks to those that helped. In terms of the question I will try to only show the relevant information when asking questions in the future.
When you edit a file, Vim stores your changes in a "swap file" so that you can recover your work if Vim's process is killed before you could save. This can be caused by something as problematic as a system crash or, simply, by closing your terminal window while Vim is running.
If Vim is quitted "normally", it deletes the swap file it created.
If it is not, the swap file is left behind and, the next time you open the same file, Vim will notice the presence of the swap file and offer you the possibility to recover the work that you "lost" the last time Vim quitted "abnormally".
That is the interactive screen you get with the "(R)ead only the file (E)dit (D)elete" prompt.
Now, when you are starting out with stuff like Git, Vim, the command line, etc. it may happen quite often that you find yourself in an uncomfortable situation, not knowing exactly what to do to fix it. This is frankly quite normal at this stage. In those situations, closing the terminal window might seem like a good first step in going back to a more comfortable situation to start again. In some cases, however, doing so might leave a trail of hidden files and broken states that might make it harder than you hoped to get to that comfortable situation.
When you do $ git commit, Git populates a specific temporary file located in your local .git directory:
.git/COMMIT_EDITMSG
with some text describing the commit you are about to make, and opens that file with your designated editor, which is the dreaded Vim by default.
When you start editing the file, Vim creates a swap file. If you insert your commit message, write the file, and quit Vim normally, the swap file is deleted and you won't ever be prompted about it. If you close the terminal window before writing the file, the swap file stays behind and Vim will prompt you about it the next time you try to make a commit.
From there you have quite a few options…
Go into your .git directory and delete the swap file(s) manually. They should be named .git/.COMMIT_EDITMSG.swp (or .swo, .swn, etc. see :help swap-files in Vim). This should give you a clean state for the next time you do $ git commit.
Don't close your terminal window when faced with a problem. Instead, try to analyze what went wrong and look for proper ways to fix it. If you have to close the terminal window, look for stray swap files just in case.
Learn Vim's basics so that you don't have to close the terminal window when you mess up your commits. Try $ vimtutor.
Tell Git to use a text editor you are more familiar with. Search Stack Overflow, I am sure there are dozens of Q/As about that.
Configure Vim to never create swap files. You can do it in Vim's configuration file:
# in $HOME/.vimrc
set noswapfile
This won't tell it to ignore existing swap files, though, so you might want to delete them manually anyway.
Use a graphical Git client instead of the CLI.

Prevent `mergetool` and `difftool` from autosaving in `~/.vim/view`

I like Vim as my mergetool and difftool. Whenever it runs (usually from git), it saves the files view states at ~/.vim/view folder, so when back to edition, they show up with diffmode on and have weird borders. Having to disable them manually or run rm ~/.vim/view/* outside Vim and reopen the files fixes it, but seems odd. How to prevent these tools from saving missconfigured view files?
[edit, more info about my setup]:
Very raw Vim. No plugins. No mappings. Only a bunch of random convenience tweaks on .vimrc (that I believe have nothing to do with the question: personal backup of .vimrc on GitHub). That is, as #filbranden points out, it might be possible to make an if statement to differentiate a diff session from others, and only save view files when not diffing. I am afraid not to know enough about vim script at this point.

Vim do not set syntax to file any more after crash?

My System was crashed yesterday, the problem is that syntax is disable and set autoindent is not in use.
Even I remove this file, touch this file again, it remains not right!
Swap files are not meant to edit directly.
Swap files are special files that store pieces of Vim's state, and pieces of the unsaved file, in a Vim-specific format. These are not backup files.
You may be able to use the swap file to recover any edits-in-progress. To do that, simply edit the file you were editing when your system crashed. Vim will detect the swap file and prompt you to recover the file if it is able to do so.
That is, if you haven't invalidated the swap file format attempting to edit it in a misguided attempt to recover your file from it by hand.
Now, Vim does have a separate ability to make real backup files that are copies of your file, whenever it saves. But that doesn't help you from a system crash, that helps you when you mess up your file yourself while you edit, and then save it.
There is also a proposed new feature (in the todo list) for adding a command to recover an entire file from an undo file, if the file itself got deleted somehow, but that's not included in any released Vim yet.

Vim won't write file without a "!" sometimes (E13)

Very often (as in multiple times an hour), when I save my file :w, vim tells me "E13: File exists (add ! to override)"
I don't know why it does this, I can't reliably reproduce it, it feels random.
This is my vimrc, note that it sets nobackup, nowritebackup, and noswapfile, and there is a function to strip trailing whitespace that gets run when I save a file.
Also, I tend to have 20 vims open at once, all backgrounded, often editing the same file. Also not improbable that I have the same buffer open in multiple windows (ie :vsp) and might open it, then reopen it with the e command a lot, possibly from a relative filepath, or possibly from an absolute one (the cmap %/ <C-R>=expand("%:p:h")."/"<CR>). No idea if any of this matters. Next time I have this issue, I'll check my ls and report anything odd.
Update:
When I tried to save "lib/seeing_is_believing/wrap_expressions.rb" (note that this is a different file than the one in the gif), this happened again. Here is the ss, it's buffer 3:
Update2 (for #mMontu)
I just realized that there are two errors happening here. The one in the screenshot is the readonly thing. The one in the gif is the more common one, E13: File exists (add ! to override)
The one I just hit is E13 File exists, for this one, readonly is not set:
Update 3
I'm pretty sure the problem is the ZoomWin plugin. I had switched it up to a newer version, and it simply didn't work right. So I stopped using it for a bit, and didn't have this issue. Then switched it back, b/c I miss its functionality (it's my favourite vim plugin), and the problems started again. Possibly it's ZoomWin in conjunction with NerdTree window. Probably not the lib authors' faults, vim in general seems fragile and buggy. Maybe I'll try NeoVim, see if they've done a better job. Maybe it's time to try Atom or Emacs again.
It seems that if there were read errors opening the file, Vim will print an error on :w. This can be seen by running :f:
"MANIFEST.in" [Read errors] 1 line --100%--
The errors aren't necessarily errors in reading the contents of file; they can be caused by a plugin.
I think the main problem is that the file has been modified externally, see http://vim.wikia.com/wiki/Have_Vim_check_automatically_if_the_file_has_changed_externally to reload it whenever this happens

I want Vim to be able to save and close similarly to Photoshop in regards to buffers?

This is my issue with Vim: you have it open for a couple of days. You're ready to close vim. You don't necessarily want to save all files... you want to skip any files which don't have modified changes, and you want to be left (or be asked) what you want to do with the remaining buffers with unmodified changes…
For anyone that has used Photoshop, this is very familiar… you use it for a week, and when you close Photoshop, it is really trying to close the application, and skips all files which haven't been touched, let's you chose what you want to do with the remaining files, and then closes itself.
It seems like every time I close Vim, I have to go through this circus of doing :qa, then running into a file, doing :bd!, then doing :qa again, run into a file I want, :w, and it's just a huge pain. There has to be a better way of doing this.
If it isn't already obvious… I have :set hidden in my .vimrc.
How about
:confirm qa
It asks you for each modified file whether to save or abandon it (or all remaining). This is the same behavior that GVIM exhibits when you close it via the X in the window title.
does :xa! solve your problem ?
:xa[ll]! Write all changed buffers, even the ones that are readonly,
and exit Vim. If there are buffers without a file name or
which cannot be written for another reason, Vim will not quit.
I was able to find the plugin BufOnly, and then with the help of someone else on StackOverflow, I got an answer that satisfies me:
https://stackoverflow.com/a/14690570/240287

Resources