Vim 预退出(Esc 键)命令?
现在,在 Vim 中,当我转到新行(或在正常模式下按“p”或“o”)时,我会得到一个可爱的自动缩进,如果我退出插入模式而不添加任何内容,它也会消失。
有没有办法在退出插入模式之前绑定某些内容,例如插入幻像字符然后将其删除?
Right now in Vim when I go to a new line (or press 'p' or 'o' in normal mode) I get a lovely automatic indent, that also disappears if I exit insert mode without adding anything to it.
Is there a way to bind something to before I exit insert mode, such as inserting a phantom character then removing it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,我两天前才读到这件事,但我不记得在哪里了。
不管怎样,诀窍是在
之后输入一个字符并立即删除它。有很多方法可以做到这一点:--EDIT--
Vim 就是 Vim,可能还有很多其他方法。
要自动化这些,您需要向 .vimrc 添加映射:
但我不确定您是否应该像这样覆盖默认值。
--EDIT--
然而,Vim 的默认行为令人烦恼的是您可能需要执行一些在实际在非缩进行上输入一些文本之前,或者在完成后执行 。
==
或在下一次依赖于您的语言的自动缩进规则<代码>所有这些都可以通过使用
来跳过,这会让您在正确的缩进级别处进入 INSERT 模式。Argh, I just read about this exact thing like two days ago but I can't remember where.
Anyway, the trick is to input a character right after
<CR>
and delete it immediately. There are a bunch of ways to do it:--EDIT--
Vim being Vim there are probably many other ways.
To automate these, you need to add a mapping to your .vimrc:
But I'm not sure you should overwrite defaults like that.
--EDIT--
However, what is annoying with Vim's default behaviour is that you may need to do some
<Tab><Tab><Tab><Tab>
before actually inputing some text on non-indented line or do==
when you are done or rely on the automatic indentation rules for your language at the next<CR>
.All that can be skipped by using
<S-S>
which puts you in INSERT mode right at the correct indentation level.在正常模式下尝试
cc
或S
来更改与缩进相关的行。不需要幻影人物。Try either
cc
orS
in normal mode to change a line with respect to indention. No need for phantom characters.像下面这样的映射应该可以解决问题:
当您在插入模式下按 esc 时,此映射会删除尾随字符。
A mapping like the following should do the trick:
This one deletes trailing characters when you press esc in insert mode.