4

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:

In the last post, @trVoldemort had the same issue (see the comments to the second answer).

ib.
  • 27,830
  • 11
  • 80
  • 100
jakesandlund
  • 401
  • 3
  • 8

4 Answers4

3

From the manual:

                            *cc*
["x]cc          Delete [count] lines [into register x] and start
                insert |linewise|.  If 'autoindent' is on, preserve
                the indent of the first line.

                            *S*
["x]S           Delete [count] lines [into register x] and start
                insert.  Synonym for "cc" |linewise|.

Since the line you are deleting has no indent, that indent is preserved. If you insert spaces in that line, so that $ puts your cursor here:

public function blah() {
    var i = 0;
    |
    i++
    return i;
}

then press 0 to go back to the first column, and finally press S, you'll get the desired result.

I realize this only explains the current behavior but does not directly solve your problem, but I am unsure of a clean way to solve your issue. I'd be inclined to suggested doing a mapping like this:

:nnoremap cc ddko
:nnoremap S ddko

These dd delete the current line, k move up to the previous line, o open input on the following line (using smartindent to supply the indentation).

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Conspicuous Compiler
  • 6,403
  • 1
  • 40
  • 52
  • Couldn't be better explained! – sidyll Aug 21 '11 at 14:48
  • I like your straightforward solution; however, it only works if the previous line is non-empty. Also, I think it is autoindent, but one of the options I am using will remove the spaces you add after exiting the line (or insert mode) unless you type some non-whitespace characters in the line. – jakesandlund Aug 22 '11 at 03:15
  • I've removed the `$` from your answer (in `ddk$o`) as there's no need for it. `o` doesn't care where in the line you are. – Chris Morgan Aug 22 '11 at 10:24
  • @Chris: Quite right. Just a habit of mine, I guess. Cheers! – Conspicuous Compiler Aug 22 '11 at 14:33
1

Turn on cindent...

:set 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 ]p as follows:

:nnoremap ]p oX<Esc>]pk"_dd

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:

:nnoremap ]P OX<Esc>]pk"_dd
:vnoremap ]p "_xkoX<Esc>]pk"_dd
:vnoremap ]P "_xkoX<Esc>]pk"_dd
jakesandlund
  • 401
  • 3
  • 8
1

One can explicitly auto-indent lines before applying the cc, S, or ]p Normal-mode command, as shown in the following mappings.

:nnoremap <leader>cc i.<esc>==S
:nnoremap <leader>]p ]p`[v`]=

Generally speaking, the effect of the = and == commands is not exactly the same as the effect of the autoindent option has on the cc or ]p commands (especially in case of a custom equalprg), but resulting behavior seems to, nevertheless, match your description.

ib.
  • 27,830
  • 11
  • 80
  • 100
  • @ib: it can be nicer to use `` rather than the escaped `^[` version. – Chris Morgan Aug 22 '11 at 10:25
  • @Chris: You are right! I have a habit of typing special symbols literally using `Ctrl`+`V`, and sometimes I forget that in mappings it is possible to use angle brackets expansion. – ib. Aug 22 '11 at 11:56
  • @ib: I'm in the same boat. I just recently decided to reform slightly. It won't last long, though, I dare say. – Chris Morgan Aug 22 '11 at 12:37
0

When I use :filetype indent on, pressing cc on that line indents it properly.

Josh Lee
  • 171,072
  • 38
  • 269
  • 275