如果 vim 中只有空格,自动缩进总是清空该行,我可以更改它吗?
例如,我通常先输入成对的大括号,然后将光标移动到块内,我期望的应该是这样的:
....{
....█
....}
但是 vim 会自动清空第二行,因为其中只有空白字符,所以出来的结果是这样的:
....{
█
....}
如何我要解决这个问题吗?
(这些点代表空格字符,█ 表示光标的位置。)
For example I usually type paired braces first then move the cursor inside the block, what I expect should be like this:
....{
....█
....}
But vim automatically empty the second line since there are only whitespace characters in it, so the result coming out is like this:
....{
█
....}
How do I fix this?
(The dots are for space characters, and █ shows the location of my cursor.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要担心当您离开插入模式时 Vim 会从空行中删除空格。正如这里其他人评论的那样,这是一件好事。相反,当您在该行上调用插入模式时,寻找一种自动恢复缩进级别的方法。
当您进入插入模式时,
i
、I
、a
和A
命令都会保留光标位置。但请检查:help cc
的文档:如果
autoindent
选项打开,然后在空行上运行cc
将切换到插入模式并恢复适当的缩进级别,根据您的需要进行设置:请注意,
S
键是同义词cc
(:help S
)。Don't worry about the fact that Vim strips spaces from an otherwise empty line when you leave insert mode. As others here have commented, this is a good thing. Instead, look for a way to automatically restore the indentation level when you invoke insert mode on that line.
The
i
,I
,a
andA
commands all preserve the cursor position when putting you into insert mode. But check the documentation for:help cc
:If the
autoindent
option is on, then runningcc
on the blank line will switch to insert mode and restore the appropriate level of indentation, setting you up just as you want:Note that the
S
key is a synonym forcc
(:help S
).如果你真的想要这样(尽管我同意@ThiefMaster),那么只需改变你的输入方式即可。
这里有 2 种解决方案:
解决方案 1:如果您喜欢箭头键
第 1 步
光标为“|”。输入
{
第 2 步
输入 Return
第 3 步
输入
}
第 4 步
按向上箭头
第 5 步
输入 Return
Solution 2
第 1 步
输入
{
第 2 步
输入 Return
第 3 步
输入
}
第 4 步
退出插入模式
第 5 步
输入
O
If you really want that (though I agree with @ThiefMaster), it's just a matter of changing the way you type it.
Here are 2 solutions:
Solution 1: If you like the arrow keys
Step 1
Cursor is "|". Type the
{
Step 2
Type Return
Step 3
Type
}
Step 4
Press the up arrow
Step 5
Type Return
Solution 2
Step 1
Type the
{
Step 2
Type Return
Step 3
Type
}
Step 4
Escape from insert mode
Step 5
Type
O
这里还有另外两个解决方案: http://vim.wikia.com/wiki/Get_the_
Correct_indent_for_new_lines_despite_blank_lines 第一个可能有用。它简单而有效:
然而,这使得 Vim 的自动完成 () 更难使用。它还与依赖默认行为的插件发生冲突,例如 supertab。
第二个,向插件或.vimrc 添加自定义函数(请参阅代码链接)似乎没有必要,因为使用
o
(如 上面由 @sidyll 描述)或O
为您进行缩进:{}O
另一种是创建片段模板。这样,您就可以
{
然后就不必键入其他按键。有不同的代码片段插件。 (如果我告诉你已经知道的事情,我很抱歉。)Here are two other solutions: http://vim.wikia.com/wiki/Get_the_correct_indent_for_new_lines_despite_blank_lines
The first one might be useful. It's simple and effective:
However, this makes using Vim's auto-complete (<C-n>) harder to use. It also clashes with plugins that rely on the default behaviour, e.g. supertab.
The second one, adding a custom function (see link for code) to your plugins or.vimrc seems unnecessary because using
o
(as described by @sidyll above) orO
does the indentation for you:{<Return>}<Esc>O
Another would be to create a snippet template. That way, you could
{<Tab>
and then you won't have to type out the other keystrokes. There are different snippets plugins. (Apologies if I'm telling stuff you already know.)