Alex Suraci
Alex Suraci

Reputation: 191

Annoying vim (un)indent rules

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?

.vimrc

Upvotes: 12

Views: 5725

Answers (8)

Max
Max

Reputation: 1013

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 <?

Upvotes: 0

too much php
too much php

Reputation: 90998

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.

Upvotes: 3

user485898
user485898

Reputation: 101

you probably want to look at indentkeys

eg the default these days is:

indentkeys=0{,0},:,0#,!^F,o,O,e,*,<>>,,end,:

Upvotes: 0

a paid nerd
a paid nerd

Reputation: 31512

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

Upvotes: 0

user236438
user236438

Reputation:

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-=:

Upvotes: 0

Brian Carper
Brian Carper

Reputation: 72926

Try this:

:let g:PHP_default_indenting=1

See :h php-indent.

Upvotes: 0

Alex Suraci
Alex Suraci

Reputation: 191

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).

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 993085

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).

Upvotes: 3

Related Questions