I do all my coding in vim and am quite happy with it (so, please, no "use a different editor" responses), but have an ongoing annoyance in that the smartindent feature wants to not indent comments beginning with # at all. e.g., I want
我在vim中完成所有编码并且对它非常满意(所以,请不要“使用不同的编辑器”响应),但是因为smartindent功能不希望缩进以#开头的注释而持续烦恼。例如,我想要
# Do something
$x = $x + 1;
if ($y) {
# Do something else
$y = $y + $z;
}
instead of vim's preferred
而不是vim的首选
# Do something
$x = $x + 1;
if ($y) {
# Do something else
$y = $y + $z;
}
The only ways I have been able to prevent comments from being sent to the start of the line are to either insert and delete a character on the line before hitting # (a nuisance to have to remember to do every time) or turn off smartindent entirely (losing automatic indentation increase/decrease as I open/close braces).
我能够阻止评论被发送到行首的唯一方法是在点击#之前插入和删除行中的字符(每次都要记住要做的麻烦)或完全关闭smartindent (当我打开/关闭括号时,失去自动缩进增加/减少)。
How can I set vim to maintain my indentation for comments instead of sending them to the start of the line?
如何设置vim来维护我的缩进注释而不是将它们发送到行的开头?
4 个解决方案
#1
44
It looks like you're coding in Perl. Ensure that the following are set in your .vimrc:
看起来你在Perl编码。确保.vimrc中设置了以下内容:
filetype plugin indent on
syntax enable
These will tell Vim to set the filetype when opening a buffer and configure the indentation and syntax highlighting. No need to explicitly set smartindent since Vim's included Perl syntax file will set it (and any other Perl-specific customizations) automatically.
这些将告诉Vim在打开缓冲区时设置文件类型并配置缩进和语法突出显示。无需显式设置smartindent,因为Vim包含的Perl语法文件将自动设置它(以及任何其他Perl特定的自定义)。
Note: having either set smartindent
and/or set autoindent
in ~/.vimrc
may prevent the solution from working. If you're having problems, look for them.
注意:在〜/ .vimrc中设置smartindent和/或设置autoindent可能会阻止解决方案工作。如果您遇到问题,请寻找它们。
#2
17
If you are using the "smartindent" indenting option, a fix for your problem is explained in the ":help smartindent" VIM documentation:
如果您使用的是“smartindent”缩进选项,则可以在“:help smartindent”VIM文档中解释您的问题:
When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. When using the ">>" command, lines starting with '#' are not shifted right.
I use "smartindent" and can confirm that the fix described works for me. It tricks VIM by replacing the keystroke for "#" with typing "X", then hitting backspace, then typing "#" again. You can try this yourself manually and see that it does not trigger the auto-outdenting.
我使用“smartindent”并且可以确认所描述的修复对我有用。它通过键入“X”替换键入“#”来欺骗VIM,然后点击退格键,然后再次键入“#”。您可以自己手动尝试,并看到它不会触发自动outdenting。
#3
8
This problem can be solved by putting the following in your _vimrc file.
将以下内容放在_vimrc文件中可以解决此问题。
set cindent
set cinkeys=0{,0},!^F,o,O,e " default is: 0{,0},0),:,0#,!^F,o,O,e
More info...
#4
6
I think "smartindent" is designed for C, so it thinks "#" is the start of a pre-processor directive instead of a comment. I don't know a solution for it, except if you type a space, then a backspace, then the "#" it won't do that.
我认为“smartindent”是为C设计的,所以它认为“#”是预处理器指令的开始而不是注释。我不知道它的解决方案,除非你键入一个空格,然后是退格键,然后“#”就不会这样做。
#1
44
It looks like you're coding in Perl. Ensure that the following are set in your .vimrc:
看起来你在Perl编码。确保.vimrc中设置了以下内容:
filetype plugin indent on
syntax enable
These will tell Vim to set the filetype when opening a buffer and configure the indentation and syntax highlighting. No need to explicitly set smartindent since Vim's included Perl syntax file will set it (and any other Perl-specific customizations) automatically.
这些将告诉Vim在打开缓冲区时设置文件类型并配置缩进和语法突出显示。无需显式设置smartindent,因为Vim包含的Perl语法文件将自动设置它(以及任何其他Perl特定的自定义)。
Note: having either set smartindent
and/or set autoindent
in ~/.vimrc
may prevent the solution from working. If you're having problems, look for them.
注意:在〜/ .vimrc中设置smartindent和/或设置autoindent可能会阻止解决方案工作。如果您遇到问题,请寻找它们。
#2
17
If you are using the "smartindent" indenting option, a fix for your problem is explained in the ":help smartindent" VIM documentation:
如果您使用的是“smartindent”缩进选项,则可以在“:help smartindent”VIM文档中解释您的问题:
When typing '#' as the first character in a new line, the indent for that line is removed, the '#' is put in the first column. The indent is restored for the next line. If you don't want this, use this mapping: ":inoremap # X^H#", where ^H is entered with CTRL-V CTRL-H. When using the ">>" command, lines starting with '#' are not shifted right.
I use "smartindent" and can confirm that the fix described works for me. It tricks VIM by replacing the keystroke for "#" with typing "X", then hitting backspace, then typing "#" again. You can try this yourself manually and see that it does not trigger the auto-outdenting.
我使用“smartindent”并且可以确认所描述的修复对我有用。它通过键入“X”替换键入“#”来欺骗VIM,然后点击退格键,然后再次键入“#”。您可以自己手动尝试,并看到它不会触发自动outdenting。
#3
8
This problem can be solved by putting the following in your _vimrc file.
将以下内容放在_vimrc文件中可以解决此问题。
set cindent
set cinkeys=0{,0},!^F,o,O,e " default is: 0{,0},0),:,0#,!^F,o,O,e
More info...
#4
6
I think "smartindent" is designed for C, so it thinks "#" is the start of a pre-processor directive instead of a comment. I don't know a solution for it, except if you type a space, then a backspace, then the "#" it won't do that.
我认为“smartindent”是为C设计的,所以它认为“#”是预处理器指令的开始而不是注释。我不知道它的解决方案,除非你键入一个空格,然后是退格键,然后“#”就不会这样做。