When editing PHP code (I'm not sure if it's specific to that language) and I create a new line in the middle of comma-separated lists that span multiple lines, the indent rules always unindent the line I'm leaving. Here's a video of it. This happens in arrays, function argument lists, etc.
Is there anything I can do to stop this from happening, or any quicker methods of fixing the error than moving up, re-indenting it, moving down, re-indenting (since it gets cleared when you leave the line), and continuing?
Try :set indentexpr=""
and see if that helps. See :help filetype-indent-off
for the section that deals with filetype plugins (which is probably where this indentexpr is coming from).
Your indenting is controlled by the PHP indent script ("filetype indent on" in your .vimrc). I use these options for my PHP indenting, which you put in ~/.vim/after/ftplugin
:
setlocal autoindent
setlocal cindent
setlocal cinwords=if,else,elseif,do,while,foreach,for,case,default,function,class,interface,abstract,private,public,protected,final
setlocal cinkeys=0{,0},0),!^F,o,O,e
setlocal nosmartindent " don't use smart indent option
There's more on this topic on the vim wiki page for source indenting.
Using "o" in normal mode seems to avoid the issue. Hitting <esc>o
from insert mode isn't optimal but it's better than the hard way (as described above).
Try this:
:let g:PHP_default_indenting=1
See :h php-indent
.
You can also do
set cindkeys=-0#
For the # char. If you are having the same problem as I had with css declarations like
width: 100%
getting shifted left as well, you can add
set cindkeys-=:
I think cindent
screws everything up and smartindent
is usually what you want.
Try putting this in ~/.vim/filetype.vim
:
au BufNewFile,BufRead *.html,*.css setlocal nocindent smartindent
you probably want to look at indentkeys
eg the default these days is:
indentkeys=0{,0},:,0#,!^F,o,O,e,*,<>>,,end,:
Find your php.vim indent file. e.g. /usr/share/vim/vim73/indent/php.vim
Then look for these lines:
elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
let lnum = lnum - 1
And comment them out by prepending quotation marks, like this:
"elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
" let lnum = lnum - 1
That'll fix it! No more de-indenting on opening PHP tags <?
来源:https://stackoverflow.com/questions/313359/annoying-vim-unindent-rules