如何在 Vim 中获得细粒度的撤销 [英] How do I get fine-grained undo in Vim

查看:33
本文介绍了如何在 Vim 中获得细粒度的撤销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 Vim 的撤销有点过于粗糙.例如.如果我输入这样的内容:

I find Vim's undo to be a bit too coarse. E.g. if I type something like this:

a // to go into edit mode
to be or not to ve
<esc> // to exit insert mode

糟糕!我打错字了.我想开始撤消,所以我按 u,但随后它会清除整行.有没有办法逐字或逐字撤消?

Oops! I made a typo. I want to start undoing so I press u, but then it clears the whole line. Is there a way to undo word-by-word or character-by-character?

推荐答案

因此,正如您从其他人那里看到的,Vi (AFAIK) 中不存在您所要求的内容.

So as you see from the others what you are asking for doesn't exist in Vi (AFAIK).

撤消会撤消您上次执行的操作.如果您的最后一个操作是进入插入模式,然后添加一行,然后退出插入模式.这将被撤消,但是如果在默认模式下您按下x"键,那么您将删除 1 个字符,或者如果在选择文本的视觉模式下,文本将被删除.如果您点击撤消,那么您将恢复该字符或选定的文本.

Undo undoes what your last action was. If your last action was to enter insert mode and then add a line and then exit insert mode. That will be undone, however if from the default mode you hit the "x" key then you will delete 1 character or if in visual mode with text selected the text will be deleted. If you hit undo then you will restore that one character or the text that was selected.

...你应该把它看作一个动作,动作可以原子地撤消或恢复

...You should think of this as an action, and actions can be atomically undone or restored

如前所述,如果您想删除前一个单词,那么您应该能够在保持插入模式的同时按 Ctrl + w 并删除前一个单词.

As mentioned previously if you wish to delete the previous word then you should be able to hit Ctrl + w and delete the previous word while remaining in insert mode.

如果您退出插入模式,您可以导航(移动)一个带有b"的单词将带有w"的单词向前移动到带有e"的单词的末尾,并且可以剪切(这使您处于插入模式)用c"或用d"删除.剪切和删除动作都可以接受跟随它们的动作,因此您可以使用dw"删除当前单词/直到下一个单词或使用cb"剪切前一个单词

If you exit insert mode you can navigate (motion) back a word with "b" forward a word with "w" to the end of a word with "e", and can cut (which leaves you in insert mode) with "c" or delete with "d". Both actions cut and delete can accept a motion following them so you can delete the current word / up to the next word with "dw" or cut the previous word with "cb"

当您记得使用."时,这个概念会变得更有用.命令(在正常模式下).这个命令是重复上一个动作.我已经多次使用它来查找和替换文件中的一小组单词(如果您对更改过多感到偏执,这尤其有用).场景如下:

This concept becomes more useful when you remember to use the "." command (in normal mode). This command is to repeat the last action. I have used this many times to find and replace a small group of words in a file (It is especially useful if you are paranoid about changing too much). The scenario would be the following:

文件:

这是我的帖子

真的是考验

这是中间

到此结束

如果我想用was"替换is",我可以这样写:%s/\/was/g

if I wanted to replace "is" with "was" I could write: %s/\<is\>/was/g

但是,如果我想将第一行和第三行的is"更改为was"(而且我不知道它们的行号,我想看看是否还有其他我想更改的地方是我可以打字吗/是"

however if I wanted to change the first line and the third line "is" to "was" (and I didn't know their line numbers, and I wanted to see if there were any other places I wanted to change is to was I could type "/is"

点击n"直到我到达我想要替换的地方,然后点击cw"并输入was"我现在可以点击n",直到到达另一个我想要替换的地方并点击.",这会将is"替换为was"(注意:我的搜索字符串不限于is"这个词,只是两个字符是"所以这个"和这个"在这种情况下将匹配)

hit "n" until I reach the place I want substituted, and then hit "cw" and type "was" I can now hit "n" until I reach another place I want substituted and hit ".", and that will replace "is" with "was" (Note: my search string didn't limit to the word "is", just the two characters "is" so "This" & "this" will match in this case)

这篇关于如何在 Vim 中获得细粒度的撤销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆