Vim: sync read-only status between buffer and file on disk - vim

I'm working with Perforce and all files in my client are read-only by default. So when I'm browsing the source code in vim buffers are marked read-only as well (may be this is not an accurate wording: I can edit its content but cannot save it without '!'). At some point I'm starting to do some edits and discover that I have to checkout file in Perforce. I have a 'nice' command for that:
command PE !p4 edit %
But after it finishes vim offers me a choice to either:
Load content from disk (which I don't want to do, because I'll lost my edits).
Or keep buffer as it is, but this preserves read-only mode (which I don't want either).
Of course I can change RO manually by doing :set noro but obviously want to avoid that.
Currently I've added it to the PE command above, but this doesn't check the real status of the file, so seems to be a little bit dangerous:
command PE !p4 edit % | set noro
What I really want is "true" synchronization of read-only state between file on disk and buffer in vim.
UPD: Mysteriously, the last version works out of the box -- if p4 edit fails RO state on buffer is not removed. No ideas how it is working...

Vim has a filewritable() function to query the file's access state, why don't you use that?
command PE execute '!p4 edit %' | let &readonly = !filewritable(expand('%'))

Related

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

Vim: load changes from previous unsaved session

I have edited file X and closed the file without saving it. Now when I try to open file X vim alerts "No write since last change...".
Is there a way to get those changes back?
Below explanation from :help message
*E37* *E89*
No write since last change (add ! to override)
No write since last change for buffer {N} (add ! to override)
You are trying to |abandon| a file that has changes. Vim protects you from
losing your work. You can either write the changed file with ":w", or, if you
are sure, |abandon| it anyway, and lose all the changes. This can be done by
adding a '!' character just after the command you used. Example: >
:e other_file
changes to: >
:e! other_file
*E162*
No write since last change for buffer "{name}"
This appears when you try to exit Vim while some buffers are changed. You
will either have to write the changed buffer (with |:w|), or use a command to
abandon the buffer forcefully, e.g., with ":qa!". Careful, make sure you
don't throw away changes you really want to keep. You might have forgotten
about a buffer, especially when 'hidden' is set.
[No write since last change]
This appears when executing a shell command while at least one buffer was
changed. To avoid the message reset the 'warn' option.

Can I get a versioned recovery from a VIM swap file?

I have a file x.cpp which a while ago contained, say, "AAAAAAA".
I accidentally copied over it with another file containing, say, "BBBBBBB".
PANIC!
Hang on, let's look at .x.cpp.swp -- there's a load of binary junk then "BBBBBBB" then more junk then "AAAAAAA", so it looks like the swap file contains both versions. Hooray!
So, how do I recover the "AAAAAAAA" version? If I do vim -r then I get "BBBBBBB". Unfortunately, I can't recover-then-hit-undo. Is there an incantation? Can I hack the swap file?
Massive gratitude if you know a trick here.
No, but if you use Vim 7.3+ you can enable undofiles
To get started:
:he undo-persistence
That way you can navigate the undo tree even after a (involuntary) close of your editor.
So yes, the usual trick of going back in time, yanking, going forward in time, pasting will work even after the editor was restarted.
100g- (locate text, yank into register), 100g+ (locate destination, put register).

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