当我们编辑一个文件时,我们通常会连续执行多次UNDO,比如20次。在 VIM 中,这通常通过按u
20 次来执行,这使得 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
答案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
一开始一样。