I have a Vimscript function which is called on BufWritePre to check the integrity of the file. Typically this involves trimming whitespace and checking that the fold structure is "valid" (according to my company's conventions).
My problem is that if I edit a file and then save it, the undo tree contains the edits made by the checking function. Is there a simple way to say that I don't want a function to modify the undo tree? Or can I save its state at the start of the function and restore it at the end?
With :undojoin, you can join your changes with the last changes done by the user (though I would find that more confusing than having them separate). In general, undo is sequence of steps; you cannot simply omit steps in the middle, that would both be unintuitive and also difficult to implement in the undo engine.
Anyway, what's so bad about including your automatic reformatting in the undo history? As I understand it, it'll be re-applied on the next save when the user undoes his last edits.
If your BufWritePre action is performing manipulations that ultimately leave your file unchanged, you can:
:wundo <tmpfile>
...Make changes leaving file the same afterwards...
:rundo <tmpfile>
This will leave you with your undo history prior to the temporary change, however if the file doesn't match it's previous state you'll get the error File contents changed, cannot use undo info.
If you wish to make the changes done during the BufWritePre impossible to undo, you can:
:set noundofile
.. Perform changes ...
:set undofile
However, this will lose all history prior to the BufWritePre
Related
I can press ctrlO to go to the last jump point. However, if there are any changes in the current document it warns me with:
E37: No write since last change (add ! to override)
Is there a way to ignore that message and do ctrl-o and either (1) discard any changes; or (2) save any changes and do ctrl-o?
Discard any changes --- No.
Save any changes --- set autowrite or set autowriteall
Hide buffer but keep it in memory (can ask for saving it later) --- set hidden
Ask if you want to save a buffer --- set confirm
Note: (2), (3) and (4) are global options and change Vim behaviour for many(!) commands.
2) Here is a simple command for your .vimrc that will save the file before jumping:
noremap <C-O> :update<C-M><C-O>
EDIT: Use :update instead of :w to avoid re-writing the file when it was not previously modified.
1) I looked for the equivalent solution to discard the changes instead of saving them, using :e! instead of :w, but that modifies the list of jump points in a way that break <C-O> completely.
After switching git branches, any files that existed on my previous branch raise an E211: File "path/to/file.txt" no longer available warning. I already know that and I find it very annoying that I'm warned about it every time I change the tab or pane that I'm focused on. Especially if I need to close 8 panes of files that no longer exist.
Is there any way to disable this warning or make it something that does not require any input to continue?
You can tweak Vim's default behavior via the :help FileChangedShell event.
This autocommand is triggered for each changed file. [...] If a FileChangedShell autocommand is present the warning message and prompt is not given.
Unfortunately, by defining an :autocmd (e.g. invoking a no-op like an empty :execute), you'll lose all the default functionality, and would have to re-implement parts of it (without the message on deletion) by inspecing v:fcs_reason. If the sledgehammer approach is fine for you, this will do:
:autocmd FileChangedShell * execute
Instead of *, you could enumerate all of your Git working copies, to make this a bit more targeted.
I was reading :help :edit but I really don't understand the following sentence:
This is useful to re-edit the current file, when it has been changed outside of Vim
And what does start all over mean in :help :edit!?
This is useful if you want to start all over again
Could anyone please provide some use cases of them?
"Changed outside of Vim" means that the file that you're editing has been written to by another program. :e will load the latest version, and :e! will do that even if you have unsaved changes.
Loading the current file from the file system is useful if you're following a log, or viewing a generated file that gets updated when you run :make and other situations.
One use for this is to throw away the changes you've made since the last save with :w and go back to the most recent saved version. Which is to say, it's not necessary for the file to have changed behind Vim's back for this to be useful. Although Vim has enough undo depth that you can usually undo your way to back to the unmodified state, it's cumbersome. You can easily "overshoot" and then have to redo. The status line shows you whether the file is [Modified] or not, but as you undo, it gets overwritten with information about each undo, so you have to use Ctrl-G to re-display the file status.
I had no idea :e by itself with no argument did this re-loading, by the way; I have been using :e% for years!
I followed add-custom-header-to-your-file to create my own custom header plugin. When I save the file it will auto. update the current date time to Last-Modified field. But this will add a edit history to undo list. How can I disable the undo temporary ?
You cannot disable undo without losing the undo history. Think about the undo implementation; would be pretty hard if you had a gap in there.
If you don't worry about losing the history,
:set undolevels=-1
(and then restoring to the previous value) will do. Also, :undojoin can be used to fuse two otherwise separate entries together. But in your situation, it's probably best to keep the (automated) change inside the undo history.
In Vim, is there a way to select a block of text and use undo to only undo changes to that block of text?
Let's say I rewrite a function, then go and make some changes elsewhere in my file. Afterwards, I realize that my first function implementation was indeed better. I'd like to undo the changes I made in that function, but leave my subsequent additions intact.
I don't know if this is even possible, but I often find myself wanting this feature.
Currently... No. Vim 7.3 has undo branches that you can traverse but as far as I know Vim does not pay attention to any selected text during an undo.
Maybe this:
http://sjl.bitbucket.org/gundo.vim/
http://stevelosh.com/blog/2010/09/coming-home-to-vim/
https://github.com/tpope/vim-pathogen