如何在 Vim 中进行细粒度撤消

发布于 2024-10-06 11:00:04 字数 232 浏览 9 评论 0原文

我发现 Vim 的撤消有点太粗糙了。例如,如果我输入这样的内容:

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分分钟 2024-10-13 11:00:04

您可以通过 :help i_ctrl-g_u 中断撤消。如果您想要每个角色,您可以映射它,但这可能有点复杂。将其映射到空格按钮是一种方法。

:inoremap <Space> <Space><C-g>u

之后每个单词都可以通过 u 撤消

You can break undos via :help i_ctrl-g_u. You can map this if you want for every character, but that might a little bit complicated. Mapping this to space button is a way.

:inoremap <Space> <Space><C-g>u

After that every word can be undo via u

忘羡 2024-10-13 11:00:04

不,这是不可能的,实际上也没有必要。 Vim 有一百万种方法来处理这个问题。例如,尝试 cb 。或bC。或brb。或者用 Tspace 来跳回,而不是 b。或ciw

当然,您可以在插入模式下使用大多数解决方案(首先按 CTRLo),或将其中一个绑定到您最喜欢的组合键(:help map:help imap)。

No, it is not possible and is actually not necessary either. Vim has a million ways of dealing with that. Try cb for example. Or bC. Or brb. Or Tspace to jump back instead of b. Or ciw.

You can, of course use most of these solutions in insert mode (by pressing CTRLo first), or bind one to your favorite key combination (:help map and :help imap).

静谧 2024-10-13 11:00:04

因此,正如您从其他人那里看到的那样,Vi 中不存在您所要求的内容(据我所知)。

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

...您应该将其视为一个操作,并且操作可以自动撤消或恢复

如前所述,如果您希望删除前一个单词,那么您应该能够按 Ctrl + w 并删除前一个单词,同时保持插入状态模式。

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

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

文件:

这是我的帖子

这确实是一个考验

这是中间

到这里就结束了

如果我想用“是”替换“是”,我可以写:
%s/\/was/g

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

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

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

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

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.

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:

File:

This is my post

really it is a test

this is the middle

This is the end

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

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"

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)

缱倦旧时光 2024-10-13 11:00:04

在 Linux 上,在输入模式下使用 control-w 会删除最后一个“单词”。

On Linux, using control-w while in input mode deletes the last 'word'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文