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

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

Related

Does .swp file affect the real file?

I was editing a file using the following command vi /etc/iptables/rules.v4 file and my terminal crashed.
When I again tried to edit the same file I got the following message
E325: ATTENTION
Found a swap file by the name "/etc/iptables/rules.v4.swp"
.
.
.
.
(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r /etc/iptables/rules.v4"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/etc/iptables/rules.v4"
to avoid this message.
Swap file "/etc/iptables/rules.v4.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
My question is will this affect my original rules.v4 file?
As after this is stopped working on the file
I am completely new to linux
swap file is a temporary file that is generated by vim to keep track of changes you have made in case of a crash. If the file rules.v4 is ok, you can delete it. Otherwise I strongly recommand to recover it, save it, and remove the .swp file. note that as long as you don't save, the Recover option will not write in the rules.v4

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.

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

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('%'))

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.

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