缩进评论以匹配vim中的代码

我在vim中完成了所有的编码工作,对此我感到非常满意(所以请不要使用不同的编辑器),但是一直在烦恼,因为smartindentfunction不希望缩进以#开始的注释。 例如,我想要

# Do something $x = $x + 1; if ($y) { # Do something else $y = $y + $z; } 

而不是vim的首选

 # Do something $x = $x + 1; if ($y) { # Do something else $y = $y + $z; } 

我已经能够阻止将注释发送到行首的唯一方法是在按#之前插入和删除行中的字符(每次都必须记住干扰)或者完全closuressmartindent (当我打开/closures花括号时,丢失自动缩进增加/减less)。

我怎样才能设置vim维护我的缩进评论,而不是发送到行的开始?

它看起来像你在Perl编码。 确保在.vimrc中设置了以下内容:

 filetype plugin indent on syntax enable 

这些将告诉Vim在打开缓冲区时设置文件types,并configuration缩进和语法突出显示。 不需要显式地设置smartindent,因为Vim包含的Perl语法文件会自动设置它(和任何其他的Perl专用定制)。


注意:在~/.vimrc set smartindent和/或set autoindent可能会阻止解决scheme的工作。 如果你有问题,找他们。

如果使用“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. 

我使用“smartindent”,并可以确认描述的修补程序适用于我。 它通过input“X”replace“#”的按键,然后点击退格键,然后再次input“#”来欺骗VIM。 你可以手动尝试这个,看看它不会触发自动缩进。

这个问题可以通过在你的_vimrc文件中添加以下内容来解决。

 set cindent set cinkeys=0{,0},!^F,o,O,e " default is: 0{,0},0),:,0#,!^F,o,O,e 

更多信息 …

我认为“smartindent”是为Cdevise的,所以它认为“#”是预处理指令的开始,而不是注释。 我不知道它的解决scheme,除非你键入一个空格,然后退格,那么“#”它不会这样做。