If I try cc or S when the cursor (| below) is in the following
position:
public function blah()
{
var i = 0;
|
i++
return i;
}
It stays in the first column instead of moving under the letter v in var. Pasting with ]p also pastes starting from column 1.
My setup (in MacVim) is: autoindent, smartindent, nocindent, and indentexpr=, if that helps.
According to the following SO questions, correct indentation should be possible:
- Vim: Smart indent when entering insert mode on blank line?
- Vim: Indent current (blank) line and insert
- Autoindent in vim always empty the line if there are only whitespace in it, can I change that?
In the last post, @trVoldemort had the same issue (see the comments to the second answer).
Turn on
cindent…This works for indenting with “cc” and “S”, as long as the language has similar indentation to C (or support for cindent).
With this, the smart-indent paste (
]p) still doesn’t work on an empty line, however. For that, remap]pas follows:This will create a new line at the correct indentation before doing the re-indent paste. Then it goes back and deletes that line, making sure not to overwrite the default buffer.
You can also remap the alternative versions of the smart-indent paste: