vim temporary disable undo? - vim

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.

Related

disable abbreviation in Vim

Anyone know how to disable specific abbreviation such as :cabbr in Vim
I know how to clear them all with :cabclear
But I can not find any doc for clear specific :cabbr
Also after I disable all the abbre, is there any way I can recover all the abbre? or restore some specific abbreviations
The command you're looking for is :cunabbrev. Check the help files.
Also after I disable all the abbre, is there any way I can recover all the abbre? or restore some specific abbreviations
No, at least not in the same session since you don't really "disable" but rather remove the abbreviations. To bring them back you need to reload your vimrc and plugin files.
Ideally, you should remove the undesired abbreviation at its source instead of removing it after it was created. In case you're unsure where it's coming from, you can check that with :verbose:
:verb ab unwanted-abbrev

how to reset Vim & TMUX dotfiles

Let say I have messed up my vim & tmux configuration. Is it possible to delete or reset them without losing any configuration changes such as PATH etc? If so, then how?
At this point? There probably is no way to get those files back. But, as a preventative measure in the future, keep config files like those in some sort of version-control system (like git or mercurial).
However, if this is too tedious or you don't want to for whatever reason, you can also add the following lines to your .vimrc:
set undofile
set undodir=~/.vim/undodir
Don't forget to mkdir ~/.vim/undodir.
This configuration enables persistent undo history in vim (I assume you're using vim to edit these files). With this feature, even after you close the file, and reopen it, you can still undo changes you made in the previous editing session. So if you'd had this set, your problem would be solved simply by hitting u until the files were in a good state. See :help undofile for more info.
Also, check out the great plugin vim-mundo. It provides a visual "undo tree" and makes browsing vim's complicated undo history very easy. Plus it's compatible with neovim.

Prevent Vim from updating its undo tree

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

Manually enable undodir, when needed

I have enabled undodir, to keep track of changes I make even after I close my vim session.
I sometimes want to edit a file, make some changes, save it, then return to the original file of that session. (This might be one of the possible use cases)
If I had undodir disabled, I could simply keep hitting u until it showed me a message.
So I want undodir to be disabled in undo/redo by default, and I should have a command to enable it when needed.
All changes to the file must be tracked in either states, at all times.
Is this possible?
As far as I understand the documentation, only setting undodir does nothing unless undofile is set to true.
So, I assume that you want to activate undofile for certain files.
First thing that comes in mind is a modeline in order to set undofile to true for certain files. But unfortunately, this doesn't work.
The issue, however, is present on the vim developer mailing list and there was a fix provided in January: http://comments.gmane.org/gmane.editors.vim.devel/32896. This fix could be present in current sources; so if you'd like to try, grab the latest vim sources, build it and check if you could use a modeline for setting udf
Until there's an official version containing that fix, you could get around your issue using undofile.vim. Excerpt of it's description:
If you want 'undofile' only for certain files, you will notice that 'undofile' cannot be set in a modeline, or once the buffer is loaded (because an existing undo file will not be loaded then). Bram suggests to use a BufReadPre autocmd which sets 'undofile' before the buffer is loaded. This script does the steps for you.

Is there a block undo in VIM?

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

Resources