53

When I open a new line (via 'o') my cursor jumps to a correctly indented position on the next line. On the other hand, entering insert mode while my cursor is on a blank line doesn't move my cursor to the correctly indented location.

How do I make vim correctly indent my cursor when entering insert mode (via i) on a blank line?

Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173

3 Answers3

91

cc will replace the contents of the current line and enter insert mode at the correct indentation - so on a blank line will do exactly what you're after.

I believe that the behaviour of i you describe is correct because there are many use cases where you want to insert at that specific location on a blank line, rather than jumping to wherever vim guesses you want to insert.

sml
  • 2,160
  • 13
  • 12
  • 2
    Well my solution will make 'i' behave that way only when the line is empty - so just one use case. 'i' will behave normally in all other situations. That being said, cc is built in and more appropriate imo. Accepting – Dane O'Connor Jun 11 '10 at 18:24
  • what I love about vim is no matter how long you use it, you can still learn new stuff every day. I use dd, yy, and c with other modifiers extensively every day, never occured to me to try cc. +1 – Matt Briggs Oct 01 '10 at 00:26
  • 2
    For me, this doesn't work *if there's a blank line* between the previous line and the line you want to indent. (i.e. you've hit `o` twice, because you want spacing between the previous line and the next section of code.) I'd still love to see a quick way to jump into insert mode, at the indentation level of the *previous line of actual code*. – ELLIOTTCABLE Nov 29 '15 at 05:24
  • This only started working for me after activating `cindent`, with `smartindent` `cc` didn't work as described. – pcworld Jan 16 '17 at 15:23
  • I agree with @ELLIOTTCABLE. That's how I found my way here. Regarding sml's comment about "many use cases....". Perhaps, but that's ~2% of the time for me, in which case I can my way back to the beginning. 98% of the time, I would like to use my present level of indent. – Myles Prather Jul 13 '22 at 15:08
14

Well this actually wasn't as bad as I thought it would be. One way to enable this is to add the following to your ~/.vimrc

"smart indent when entering insert mode with i on empty lines
function! IndentWithI()
    if len(getline('.')) == 0
        return "\"_ccO"
    else
        return "i"
    endif
endfunction
nnoremap <expr> i IndentWithI()

It simply checks for an empty line when you hit 'i' from insert mode. If you are indeed on an empty line it will delete it and open a new one, effectively leveraging the working 'open line' behavior.

Note: "_ before the cc makes sure that your register doesn't get wiped

Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
  • I found that `ccO` didn't work either, but `ddko` did (delete current line, go to previous line, add new line). – Edward Hughes Mar 30 '17 at 21:24
  • 1
    I had to change `"\"_ccO"` to `"\"_cc"`, it was inserting the O and I'm not sure why it was there. Otherwise super helpful function, thanks. – Connor Sep 10 '19 at 18:30
  • After using this for a few days, I changed this behavior to work with `a` and not `i`, which makes more logical sense to me since I think of them as being on different sides of the implicit indentation. So `i` inserts at the far left as before, `a` appends after the auto-indentation. – Connor Sep 19 '19 at 15:43
9

On an empty line, to enter insert mode correctly indented, you can simply use s.

Note that s is a synonym for cl, so if you're not actually on an empty line, it'll end up deleting a single character and not indenting. In that case, you're better off using cc, as sml suggested some 18 months ago. But I've frequently improved my score at VimGolf by using this shortcut, so thought I'd mention it. ;)

Trevor Powell
  • 1,154
  • 9
  • 20