How to stop Vim from creating *-e files - vim

Recently my Vim has been littering my folders with extra files ending with -e. For example, if I'm editing a file called test.php, sometimes (but not always!) I'll wind up with another file called test.php-e as well.
I have no idea what's causing this, but it's fairly annoying. I haven't changed anything recently, as far as I can tell. The backup files all wind up in my ~/.vim/backups directory, so I'm not sure where these things are coming from. Thoughts?

set nobackup
or set backup dir to something like /tmp and purge from time to time.
From VIM docs (:help nobackup)
'backup' 'bk' boolean (default off)
global
{not in Vi}
Make a backup before overwriting a file. Leave it around after the
file has been successfully written. If you do not want to keep the
backup file, but you do want a backup while the file is being
written, reset this option and set the 'writebackup' option (this is
the default). If you do not want a backup file at all reset both
options (use this if your file system is almost full). See the
|backup-table| for more explanations.
When the 'backupskip' pattern matches, a backup is not made anyway.
When 'patchmode' is set, the backup may be renamed to become the
oldest version of a file.
NOTE: This option is reset when 'compatible' is set.

Related

Vim undofile does not work when file is too long

I use a vim persistent undo setup as follows, which have worked all that way here.
set undofile
set undodir=~/.vim/undodir
However now I'm facing a problem when the undofile became too long, notice that an undofile has a name in the form of absolute path like %home%user%directory%...%directory%file that means if your file has a big name, is under a big path directory or both, the chances for reaching the linux file name max length will be higher. In my case, when try to save the open file it raises the error:
E828: Cannot open undo file for writing: /home/user/.vim/undodir/%home%user%workspace%%app%javascript%packs%domains%components%grid%column_cell_factory%inspection_cell_factory.jsx
How can I manage in order to keep my persistent undo working even for these situations?
UPDATE
This is not my real filename, I removed username and others personal information intentionally, at the end they are not relevant and don't contain any special char. The file length is 160, and the whole path with the folder it should be stored is 189 length.

vim: recovering a text file "already at oldest change"

I am using vim to edit .sh file. Last time, I was making changes I got: "E297: Write error in swap file" and I accidentally managed to erase the content and save.
Now, all I have is .sh .sh~ .su~ .sv~ .sw~ .sy~ .sz~ files with empty content and "E297: Write error in swap file" message. When I do :u, it says: "already at oldest change".
when I do :recover, it says: "E305: no swap file found"
How to recover my file? thanks
In this case, the original file may be recovered using swap files which have the form .filename.sh.swp. These are files that Vim creates to back up in case of a potential crash. In order to recover your original file, try these steps:
Look for swap files in the current directory (there could be more than one for a particular file):
ls -a
Open the first swap file in Vim from the terminal:
vi .filename.sh.swp
or alternatively, launch Vim with vim and edit the swap file with :e .filename.sh.swp.
From within the swap file type :recover. Now Vim will load the recovered file on a new buffer. If this is the file that you needed then simply save the file :w.
It could be that the recovered file is not exactly the latest version of your filename.sh, in this case repeat steps 2 and 3 above with a different swap file e.g. .filename.sh.swo. Once the desired file has been recovered removed the swap files.
These help pages are also relevant for recovering files:
:help swap
:help recover
:help e305

vimscript using another file instead of .viminfo

I installed a vimscript written by expert in this question.
But there is one problem in the script. When I restart the computer, the bookmarks saved by the script will disappear.
Is it possible to store the bookmarks to another file (e.g. mybookmarks.txt) instead of .viminfo file? Bookmarks stored in .viminfo disappear unpredictably.
Yes. Use the 'viminfo n' option.
From :he 'viminfo':
Name of the viminfo file. The name must immediately follow
the 'n'. Must be the last one! If the "-i" argument was
given when starting Vim, that file name overrides the one
given here with 'viminfo'. Environment variables are expanded
when opening the file, not when setting the option.
Since it seems like a good tool in general, I extracted it to a plugin: http://www.vim.org/scripts/script.php?script_id=3826. This would store bookmarks only, regardless of viminfo settings, in ~/.vim_bookmarks. The filename is configurable by changing g:simple_bookmarks_filename.
Since it's a full blown plugin now, you can send bug reports on the issue tracker, so if it's not working quite right, let me know.
Cheers.

Can I recover my code lost during my last change in vim?

I am very sad I deleted wrong function without commit to SVN server by using vim
After I compiled it I found I made the mistake. I 'make' the file also via vim.
Now I haven't closed the file and it has .swp file.
I tried to use 'u' command to restore my deletion but failed. vim said it's the latest changes. sigh.... Anyway I can restore my function?
Million thanks.
To make Drasils pointer a lot more explicit:
:undolist
g- to 'go back in time'
g+ to 'go forward in time'
Vim 7.3+ has undo 'branches': meaning that it will save state snapshots, even if linear history was overwritten (so it isn't reachable by simple u and )
Vim usually saves the previous version of any file edited as a backup with a ~ appended -- you could check to see whether that file is there and if so, whether it's got the right contents.
There are a couple of ways to recover text that you may have unwittingly lost due to a crash or because you closed your program unintentionally.
Use persistent-undo. Persistent undo provides almost all the features provided by swap/backup file option in points #2 and #3, along with some other options such as granular history traversal.
a. Set persistent-undo on:
Put this in your .vimrc:
set undofile
set undodir=~/.vim/undodir
b. Create the undodir
mkdir ~/.vim/undodir
c. To undo a change, use either of the following options
1) g+ in normal mode to go forward in history
2) g- in normal mode to go backward in history
3). :earlier 20s to go back in time by 20s or earlier 10m to go back in time by 10min etc
4) :later 20s to go forward in time by 20s or later 10m to go forward in time by 10min etc
5). Use :undolist to get a list of undo changes
d. To get a visualization of your undo-branches, you can use plugins like gundo.vim: http://bitbucket.org/sjl/gundo.vim/ to visualize the branches
Use backup files
a. Use this in your .vimrc
set backup
b. Set up the backup directory by putting this in your .vimrc
set backupdir=~/tmp/
c. Set up the backup file name to be added to the backup file by setting this in your .vimrc
set backupext=string
Use swap files
a. Use this in your .vimrc
set swapfile
b. Set up the swap directory by putting this in your .vimrc. This may not be a good idea, because it will prevent you from having two files with the same names, or cause conflicts in swap file names.
set directory=~/tmp/
A better option is to provide multiple paths, so if vim encounters a conflict it can then save it to the directory that it can write to using
set directory=,~/tmp/
In this case, it will try to write the swap file to the current directory. If it can't, then it will attempt to write it to the ~/tmp directory
c. Set up the backup file name to be added to the backup file by setting this in your .vimrc
set backupext=string
TL;DR Use persistent-undo. It provides almost all features of swap and backup, and provides additional features such as granular undo which is not provided by backup and swap file options.
References
1. ftp://ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf
I don't know if you can recover something here, but for the future, if you user vim 7.3, you should active these options I explain in my previous comment.
I must say that the savevers plugin has saved me a lot of hours ;-)

Vim: Only show “file has been changed” warning if content is different

Is it possible to setup Vim so that it will only show:
WARNING: The file has been changed since reading it!!!
If the file is actually different, not just when the timestamp changes?
For example, I'll quite frequently background Vim (^Z), roll back to an older version of a file (eg, to run the test suite against it), revert back to the current version and fg Vim again… But I still get the “file has changed” warning because, even though the content is identical, the timestamp has changed.
If you try on vim 7.3
:help timestamp
It is said that
When Vim notices the timestamp of a file has changed, and the file is being
edited in a buffer but has not changed, Vim checks if the contents of the file
is equal. This is done by reading the file again (into a hidden buffer, which
is immediately deleted again) and comparing the text. If the text is equal,
you will get no warning.
So I guess that in your case, something has changed other than the file timestamp ( or there is a bug in Vim).
In my case, I often get that message when I check out files : they change from "read only" to "read write" even if their content has not changed.
So I guess that if the properties of a file are affected, it is considered "changed" even if the content is the same.

Resources