Stop Vim from saving a file if content has not changed - vim

In GUI text editors I've seen, when text has not changed (or has changed and been reverted to its original state), the Save option is greyed out to indicate so.
For something similar, can Vim disable saving a file in such a case? That is, doing the :w would not actually change the mtime of the file.

You can use the :update command (:up for short) which writes the file only if the buffer has been modified. If you felt strongly you could add an update option to gvim's File menu, but maybe the Ex command is sufficient.

WRITING WITH MULTIPLE BUFFERS *buffer-write*
*:wa* *:wall*
:wa[ll] Write all changed buffers. Buffers without a file
name or which are readonly are not written. {not in
Vi}
:wa[ll]! Write all changed buffers, even the ones that are
readonly. Buffers without a file name are not
written. {not in Vi}
:wa will save all changed files
:xa will save all changed files, and then quit vim.

Related

Find file in vim, switch to next file without saving current file

Vim newbie here.
I use :find command to open a file and make some changes.
Suppose I want to open another file without saving the current changes, then how should I do it.
What is the best way to list all open files and quickly open them again for editing.
Thanks in advance.
:e! filename
will allow you to open a named file in a new buffer (note the exclamation mark overrides the warning re. an unsaved file).
CTRL-W + cursor
will move between visible buffers and does not enforce the saving of your file as you navigate.
:buffers
will list your open buffers and allow you to navigate to each one (albeit with some cryptic flags indicating which buffers are visible/hidden/amended etc.)
:help buffers
will tell you about all the buffer-related features.
:help :wincmd
will tell you all about windows navigations
See also the VIM buffer FAQ

automatically save the current buffer when is going to edit a new file

I've made some changes in the current buffer, and I want vim to automatically save the current buffer when I'm going to edit a new file with the following command:
:e another_file_which_is_not_a_buffer_in_vim_yet
I added the following line in my .vimrc file, but it didn't work.
autocmd BufLeave * update
Vim still prompted me No write since last change, why? How can I make it work? By the way, I only want to save the current buffer instead of all buffers, because saving all buffers seems to mess up the order of the buffers, which would bring trouble when I run :bp or :bn.
Vim has an option to save also for, among others, :edit in addition to what triggers autowrite:
set autowriteall
Relevant manual excerpts:
autowrite:
'autowrite' 'aw' boolean (default off)
global
Write the contents of the file, if it has been modified, on each
:next, :rewind, :last, :first, :previous, :stop, :suspend, :tag, :!,
:make, CTRL-] and CTRL-^ command; and when a :buffer, CTRL-O, CTRL-I,
'{A-Z0-9}, or `{A-Z0-9} command takes one to another file.
Note that for some commands the 'autowrite' option is not used, see
'autowriteall' for that.
and autowriteall:
'autowriteall' 'awa' boolean (default off)
global
{not in Vi}
Like 'autowrite', but also used for commands ":edit", ":enew", ":quit",
":qall", ":exit", ":xit", ":recover" and closing the Vim window.
Setting this option also implies that Vim behaves like 'autowrite' has
been set.
This line of code shold do the trick. Place it inside .vimrc file. Location of this .vimrc file can be found if you type :version inside of vim.
set autowrite
More about this topic can be found on this link

In Vim, how can I undo opening?

I started using NERDTree recently.
When I opened a file while having split window, the window of a new file was not I intended.
Consequently, I had lost my unsaved buffer by opening a file.
Is there any way to undo opening files or reopen my unsaved buffer?
You can go back to the previous file in a buffer using Ctrl6.
You haven't lost anything (yet), the buffer was hidden. Since it is the alternate buffer of that window, you can just switch back via <C-^>. You'll also find it in the buffer list of :ls (with a h prefix for hidden), from which you can re-edit it via its buffer number, e.g. :buffer 42 or :sbuf 42.
If there are unsaved changes, Vim will prompt you on :quit (unless you provide a ! to it).

Recover a vim file from the .un~ file without the undo command

How can I restore a vim file from the undo file without hitting undo?
I had a vim file that I saved while adding text. Then I ran a python command that emptied the file's contents, and I can see some of the words the file contained in the file's .un~ file. When I try to undo in the file, it says Already at latest change. I can't find the swap file in my swap files directory.
As the other answers have noted, you can't recover the whole file from vim's undo files, simply because vim seems to only keep diffs in the undo files, not the whole contents. (That makes a lot of sense, as it's space efficient.)
One thing you can try though, is to extract what's possible from your undo file:
$ strings <undo-file>
The output will not be pretty, but you could end up finding something that's valuable to you.
You can't. The undo information is linked to Vim's last knowledge of the file's contents; when they don't correspond any more, Vim cannot re-apply the changes. This is documented at :help undo-persistence:
Vim will detect if an undo file is no longer synchronized with the file it was
written for (with a hash of the file contents) and ignore it when the file was
changed after the undo file was written, to prevent corruption.
Best you can do is try to manually salvage recognizable bits in the undo file, e.g. with a hex editor, or Vim's binary mode.
It is not exactly possible, as the undo file only contains the text that was changed in a single change. If you at some point reloaded the file, the undofile should contain the complete buffer for that and starting from there one could theorectically recover the file (by going through the undo states).
I have written about this before at the vim_use mailinglist here and here (which even contains a patch, that let's you force reading in the undo-file)
You could try to patch vim and see if you can recover at least some data.
A reminder that if you have set in your .vimrc file
set backupdir=$HOME/tmp
You may have temp copies of the files that are readable and that can be renamed

Vim forgetting its history when a file becomes read-only

After I commit a file to Perforce with vi, it will become read-only.
If I have this file open in vim, then when it becomes readonline I lose my undo-redo history, without even being asked.
Is there an option in VI to preserve the undo-redo history when the file becomes read only while you are editing?
It is a Vim bug. Whenever you :edit filename, implicitly or explicitly it seams that Vim is zeroing all undo history for this file because ( I guess ) it think that it is newly opened file. And after perforce commit, your file is kind of “changed outside” and Vim should ask you “Reload file?” unless you set “autoread”.
Check you vimrc for “set autoread” option.
Maybe you could try to make it readable with modelines :
#vim : set noreadonly:
I wrote # but of course you must replace it with the adequate symbol for a comment.

Resources