Why is vim indenting my Perl code incorrectly?

孤街醉人 提交于 2019-12-03 14:27:46

Maybe this is magical thinking, but … I used to have:

filetype plugin on
filetype indent on 

in my _vimrc (on Windows XP, self-compiled gvim, various versions), and I would get all sorts of interesting indentation problems in Perl, LaTeX, and HTML files.

Now, I have

filetype indent on 
filetype plugin on

and everything seems to be hunk-dory. YMMV.

Also, I highly recommend Andy Lester's vim-perl.

cindent is specific to the c language and is broken when used with a lot of other languages. What you probably want to use is filetype plugin indent on. You can add that to your .vimrc and vim will figure out the correct syntax/indentation for most languages out of the box. You can also add syntax/indentation guides fairly easily if vim doesn't already have them.

My system indents your code correctly using filetype indent on (versus filetypepluginindent on). [Vim 7.2]

Tracked this issue down to a quirk in the vim regular expression matcher, in the perl.vim indent file there are several places where the regular expression includes an attempt to escape a [ in a collection with \[....

 **let bracepos = match(line, '[(){}\[\]]', bracepos + 1)**

but for whatever reason the \[ matches any \ or [ in the line not just [ so to fix the vim indent file unescape the left brackets in all match statements, ie...

 let bracepos = match(line, '[(){}[\]]', bracepos + 1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!