Vim 中的 smartindent 问题
在打开 smartindent 的 vim 中:
- 说出 if 语句后按 Enter 输入
- {
- 按 Enter 两次
- 输入 }
- 如果您点击 ↑ 并转到上一行,则空行中的缩进会被删除。
甚至 vim 文档也这么说:
如果您在新行上除了
或 CTRL-D
之外没有输入任何内容,然后输入 < ;Esc>
、CTRL-O
或
,缩进再次被删除。
有什么办法可以保留这个缩进并且没有删除吗?
In vim with smartindent on:
- Press Enter after say an if-statement
- Type in {
- Press Enter twice
- Type in }
- If you hit ↑ and go to the previous line, indentation is removed from the blank line.
Even the vim documentation says that:
If you do not type anything on the new line except <BS>
or CTRL-D
and then type <Esc>
, CTRL-O
or <CR>
, the indent is deleted again.
Is there any way to keep this indentation and not have it deleted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Shift+S 在空白行上开始编辑(显然是在命令模式下)。 这将以预期的缩进级别开始光标。
另一个没有回答问题,但总的来说是一个更好的解决方案:
在插入模式下键入左大括号时,这将插入一组匹配的大括号
并将光标放在中间的新行上。
同样,这将自动插入匹配的括号和方括号。
(在添加到
vimrc
时去掉前导:
。)正如我对 Victor 的回答的评论,更改 Vim 的缩进行为将在整个文件中留下包含无关空格的“空”行。 IMO,这是完全无法容忍的。
Use Shift+S to start editing on a blank line (from command mode, obviously). This will start your cursor off with the expected level of indentation.
Another doesn't-answer-the-question-as-asked-but-is-a-better-solution-overall:
When typing an opening brace in insert mode, this will insert a matching set of braces
and leave the cursor on a new line in the middle.
Similarly, this will auto-insert matching parens and square brackets.
(Strip off the leading
:
when adding tovimrc
.)As I commented on Victor's answer, changing Vim's indentation behavior will leave "empty" lines containing extraneous spaces throughout your files. IMO, this is completely intolerable.
当这种情况发生在我身上时,我有时会使用 ddko (或 ddO )删除没有足够空格的行,并使用正确的缩进打开一个新行。 或者,我只需按
A
,然后按Tab
多次即可获得正确的缩进。When this happens to me, I sometimes use
ddko
(orddO
) to delete the line without enough spaces and open a new line with the correct indent. Or, I'll just pressA
and thenTab
enough times to get to the correct indent.这里 文章讨论了您遇到的同样的问题,以及将哪些内容放入 vimrc 中来修复它。
我还没有完全测试过这个。
同一篇文章还链接到更短的替代解决方案。
the article here talks about you're very same problem, and what to put in vimrc to fix it.
I havn't exactly tested this tho.
also the same article links to a shorter alternate solution.
我首选的方法是
{}shift+o
因为它比{}k shift+s
代码> 几笔。 不过,我对此很墨守成规,最终只是使用 o 或 O 来抓取新的、正确缩进的空行,而当我应该使用 S 时。也就是说,设置你的支撑结构并打开上面的行:
你会得到你期望的缩进。
open-above 技巧并不比
少按键,但是通过重新映射转义键和和弦,你可以很快地把它放进去。另外,不要忘记手动缩进重置和块移动。 如果您位于损坏的花括号块内,只需使用
={
(如果您位于其中一个大括号的顶部,则使用=i{
)。 当我有一个好主意需要尽快查看文本时,我会使用它,并且在我喘口气之前我不会担心任何格式的问题。My preferred method is
{<CR>}<esc>shift+o
as it outpaces{<CR><CR>}<esc>k shift+s
by several strokes. I get in a rut with it, though, and end up just using o or O to grab new, properly-indented lines off an empty when I should be using S.That is, set up your bracing structure and open line-above:
And you get the indenting you expect.
The open-above trick isn't any fewer keypresses than
<up><end><cr>
, but with escape remapped and shift being chorded, you can throw it in quite fast.Also, don't forget your manual indent reset and block-movement. If you're inside a mangled curly brace block, simply use
={
(or=i{
if you're on top of one of the braces). I use that when I have a Good Idea that needs to see text asap, and I don't worry about any formatting frippery until I take a breather.