Vim undofile does not work when file is too long - linux

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.

Related

Share marks in vim across machines

I use a few different machines and I was wondering if there was a way to store or share marks in files that are opened on the different machines.
If so, how could this be done? I am fine saving/re-storing a helper file in the .vim directory if that is necessary to do the above.
This would be for both lowercase and uppercase marks. I suppose one option would be to put the .viminfo in a git repository and share that between machines, but I'm wondering what unintended consequences that might have. Currently I have:
set viminfo='100,f1,n~/.vim/viminfo
Note that I'm not interested in storing it across sessions on the same machine, but different computers (one local one in aws).
My advice is to use a separate session-like file that only stores marks. The command :wviminfo can write a separate file. So the plan is: save &viminfo and clear it, write a file with marks, restore &viminfo.
function! SaveMarks()
let save_vim = &viminfo " Save viminfo
set &viminfo='100,:0,/0,<0,#0 " Clear viminfo - store only file marks for 100 files
wviminfo! marks.vim
let &viminfo = save_vim " Restore viminfo
endfunction
Copy the file to the other host, read it using :rviminfo:
:rviminfo! marks.vim

How to easily delete swap file placed in another directory (vim)?

I had a session with 7 files opened in vim. After accidental crash and reloading the session each file gives E325: ATTENTION explaining that the files are newer than swap ones! I don't remember how these files become newer that the original swap ones, but for now it doesn't matter, because I want to learn how to handle this case.
I think, the solution would be simple if swap files was placed along the files itself (eg .filename.swp next to filename). Because I could remove it just by :!rm .%.swp.
However I changed the default directory of swap files, which is now set to directory=~/.vim/tmp/swp//. So the question is how can I get the full pathname to the swap file. For example:
vim /home/timur/code/src/project/main.go
E325: ATTENTION
...
Swap file "~/.vim/tmp/swp//%home%timur%code%src%project%main.go.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort
E or R
After I opened the buffer I want to type something like this:
:remove *swapname*
or using a shortcut:
<leader>rw
I've read the docs and found the command to show current swap file name: :swapname. But it seems the output name isn't fit to passing it into rm command, because there is additional slash before the swap name. For example:
Actual file:
/home/timur/.vim/tmp/swp/%home%timur%code%own%src%file
The output of :swapname (adds odd slash before swap name)
/home/timur/.vim/tmp/swp//%home%timur%code%own%src%file.swp
^ ???
To put it simple: how to get direct/explicit path to swap file of current buffer?
Inspired by issue 355, here is your mapping
nnoremap <silent> <Leader>rw :call DeleteSwapFile()<CR>
function! DeleteSwapFile()
redir => s:a
exe ':sil sw'
redir end
let b:swapname = s:a[1:]
call delete(b:swapname)
endfunction
:h redir
If you don't mind solving the problem using a vim plugin try Recover.vim plugin.
When opening a new file, it checks if a swapfile is already present.
If so gives two additionals options:
[D]elete: delete the swapfile (i use this option very rarely)
D(i)ff: compare the recover version of the file and the current version. If they are the same it asks you if you want to delete the swap file. If they are not the same open a vimdiff session between to visually compare the differences.
Really a must have.

How to automatically name a file when saving in vim

I'm trying to emulate in vim a behaviour similar to that of TextEdit.app.
As I work I often open a lot of files to take notes, and keep them there without saving them. When I restart the laptop, the TextEdit files will still be there and open thanks to AutoSave. If I do the same thing with vim (specifically MacVim) the files are (as expected) not saved and I lose their content.
My recipe for solving this problem has two bits. The first one is to automatically save the files when I'm not using them, so using a command like:
autocmd BufLeave,FocusLost * silent! wall
This works fine with files that have already been saved, but it ignores ones that have not yet been saved. This is where the second bit comes into play, I would like vim to automatically give these files a default name if it tries to save them and they don't already have a name. Possibly I would also like there to be a default save directory.
The ideal solution would be that when an unnamed file/buffer loses focus it gets saved as ~/Documents/notes/note_1.txt, the second one note_2.txt, etc etc.
I did look around for any pointers that could help in either direction (default name and default directory - the latter is not fundamental though), but couldn't find anything.
Can anybody help?
I don't like your idea, but it is doable.
You need a function:
function! SaveIt()
if bufname("%")==''
exec 'w /path/note_'.localtime()
else
w
endif
endfunction
and in your autocommand, just call the function. Some points you need to note:
the filename would be /path/note_( ms since 1970). your 1,2,3.. index will make vim check filesystem to find out the index. It could be a better name, e.g note_2013-09-11_11:11:11.233 You just change the localtime()
this may throw exception when you try to save a readonly buffer. (help, qf ...) You could check for them though.
Note that I didn't add the ! in w cmd.
it may not work for your autocmd wall. if you want to do it, you have to loop through all buffers, and for each buffer call the function.
after all the function shows the direction how it could be done, it (the quality) is still very far away from "production" level.

How to stop Vim from creating *-e files

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.

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 ;-)

Resources