Prevent Vim from updating its undo tree

牧云@^-^@ 提交于 2019-12-18 07:43:23

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12741977/prevent-vim-from-updating-its-undo-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!