如何暂停VIM历史记录?

如何暂停VIM历史记录?

当我们编辑一个文件时,我们通常会连续执行多次UNDO,比如20次。在 VIM 中,这通常通过按u20 次来执行,这使得 VIM 在历史堆栈中“上升”20 个位置。如果您随后执行某个change,则所有最后 20 个历史命令都会丢失,并替换为change。我想在change不失去这 20 个职位的情况下完成任务。所以我想我想在我这样做之前告诉VIM停止记录历史change,然后恢复历史(不想change在历史中)。

编辑 试图更清楚:我有一个函数FF可以在写入缓冲区时更新文件的最后几行。因此,如果我执行20 undos + write,最后一个write会打开一个新的撤消分支。我尝试undojoin在内部添加FF(尝试遵循下面 jlmg 的建议),但是序列 write-undo-write 给出错误:撤消后不允许撤消联合。我可以sed ....在离开后做一些事情vim,但由于我使用它,所以SSH我更喜欢仅使用 vim 的解决方案(卸载缓冲区后执行命令不会写入文件)。

编辑2尝试在 VIM 中执行此操作:打开一个空白文件,然后执行以下操作:

i1<ESC>:wa2<ESC>:wa3<ESC>:wa4<ESC>uu:w

如果现在你执行 a <CTRL>R,VIM 会将 '3' 写回,进一步<CTRL>R你会得到4.有时候是这样的甚至如果你:w在每个之后执行一个<CTRL>R.但是,如果每次执行 a 时都:w通过 执行函数BufWritePre,则<CTRL>R不会写3回。这就是我想做的,这就是为什么我写信“暂停历史”,但也许我所要求的除了与完整的undotree().

答案1

您不需要暂停 Vim 历史记录。实际数据形成一棵树,您可以使用该undotree()函数检索它。当然,有许多插件可以将其变成更用户友好的东西,fi军多,蒙多, 和撤消树。如果您还启用撤消持久性(参见:h undo-persistence),您可以轻松浏览文件的整个更改历史记录。

答案2

如果我理解正确的话,你想要的是,之后执行撤消操作应该撤消该撤消操作change以及 20 次撤消操作。

当 vim 执行函数或命令时,它执行的所有操作都会一起撤消。我不确定这些操作是否可能包括撤消。也许文档的这一部分可以提供帮助:

3. Undo blocks                      *undo-blocks*

One undo command normally undoes a typed command, no matter how many changes
that command makes.  This sequence of undo-able changes forms an undo block.
Thus if the typed key(s) call a function, all the commands in the function are
undone together.

If you want to write a function or script that doesn't create a new undoable
change but joins in with the previous change use this command:

                        *:undoj* *:undojoin* *E790*
:undoj[oin]     Join further changes with the previous undo block.
            Warning: Use with care, it may prevent the user from
            properly undoing changes.  Don't use this after undo
            or redo.
            {not in Vi}

This is most useful when you need to prompt the user halfway through a change.
For example in a function that calls |getchar()|.  Do make sure that there was
a related change before this that you must join with.

This doesn't work by itself, because the next key press will start a new
change again.  But you can do something like this: >

    :undojoin | delete

After this an "u" command will undo the delete command and the previous
change.

To do the opposite, break a change into two undo blocks, in Insert mode use
CTRL-G u.  This is useful if you want an insert command to be undoable in
parts.  E.g., for each sentence.  |i_CTRL-G_u|
Setting the value of 'undolevels' also breaks undo.  Even when the new value
is equal to the old value.

唉,我尝试使用:undo 2 | undojoin | normal ohi,但收到错误消息E790: undojoin is not allowed after undo

但是,如果操作是在如下函数中完成的:

function F()
  undo 2
  normal ohi
endfunction

然后调用它确实在一个撤消块中:call F()执行该操作和其他操作。undo请注意,因为您正在使用撤消,所以您正在创建一个新的撤消分支。完成后可以使用普通模式命令g-, 来撤消F();使用起来u就像:undo 3一开始一样。

相关内容