Vim:在空行上进入插入模式时智能缩进?
当我打开新行(通过“o”)时,我的光标跳转到下一行的正确缩进位置。另一方面,当光标位于空行时进入插入模式不会将光标移动到正确的缩进位置。
在空白行上进入插入模式(通过 i)时,如何使 vim 正确缩进光标?
When I open a new line (via 'o') my cursor jumps to a correctly indented position on the next line. On the other hand, entering insert mode while my cursor is on a blank line doesn't move my cursor to the correctly indented location.
How do I make vim correctly indent my cursor when entering insert mode (via i) on a blank line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这实际上并没有我想象的那么糟糕。启用此功能的一种方法是将以下内容添加到 ~/.vimrc 中,
当您在插入模式下按“i”时,它只是检查空行。如果您确实处于空行中,它将删除它并打开一个新行,从而有效地利用工作的“打开行”行为。
注意:抄送前的“_”确保您的寄存器不会被擦除
Well this actually wasn't as bad as I thought it would be. One way to enable this is to add the following to your ~/.vimrc
It simply checks for an empty line when you hit 'i' from insert mode. If you are indeed on an empty line it will delete it and open a new one, effectively leveraging the working 'open line' behavior.
Note: "_ before the cc makes sure that your register doesn't get wiped
在空行上,要进入正确缩进的插入模式,只需使用
s
即可。请注意,
s
是cl
的同义词,因此,如果您实际上不在空行上,它最终会删除单个字符且不缩进。在这种情况下,您最好使用cc
,正如 sml 大约 18 个月前建议的那样。但我经常通过使用这个快捷方式提高 VimGolf 的分数,所以我想提一下。 ;)On an empty line, to enter insert mode correctly indented, you can simply use
s
.Note that
s
is a synonym forcl
, so if you're not actually on an empty line, it'll end up deleting a single character and not indenting. In that case, you're better off usingcc
, as sml suggested some 18 months ago. But I've frequently improved my score at VimGolf by using this shortcut, so thought I'd mention it. ;)cc 将替换当前行的内容并在正确的缩进处进入插入模式 - 因此在空白行上将完全执行您想要的操作。
我相信您描述的
i
行为是正确的,因为在很多用例中您想要在空行的特定位置插入,而不是跳转到任何地方vim 猜测你想要插入。cc
will replace the contents of the current line and enter insert mode at the correct indentation - so on a blank line will do exactly what you're after.I believe that the behaviour of
i
you describe is correct because there are many use cases where you want to insert at that specific location on a blank line, rather than jumping to wherever vim guesses you want to insert.