0

I will try to describe my problem with an example. Given the position of my cursor at:

function Test() {
  return (
    <div>█
    </div>
  )
}

NORMAL 

When I press o:

function Test() {
  return (
    <div>
      █                        // <-------- The state I want as result.
    </div>
  )
}

INSERT

Then if I press Esc:

function Test() {
  return (
    <div>
█                              // <-------- The space-indentation disappear.
    </div>
  )
}

NORMAL 

Is there a option that I can set in my vimrc to control this behavior or do I have to write some vimscript by myself?

Edit: my purpose is to remap the default behaviour of o to let it end in normal mode instead of insert mode, and yes I want to keep the indentation :)

3
  • 1
    yes, type a letter and delete it directly again. Apr 15 at 6:14
  • @ChristianBrabandt: May I ask you that would these approaches of type-and-delete in general slower than other options?
    – job_start
    Apr 15 at 14:51
  • sorry, don't understand the question :/ Apr 15 at 15:19
3

You don't need spaces on empty line. This is the reason Vim removes auto-indentation after Esc.

Whenever you're going to write something on empty line with indentation applied, you can press cc or S. Or you can re-indent text after you've finished with it (e.g. == or ='[ and so on).

3
  • Thank you, I will take the first option
    – job_start
    Apr 15 at 14:49
  • :help i_ctrl-t is another option.
    – romainl
    Nov 28 at 10:00
  • @romainl Yes, I used c-d/c-t for some time too. But they are really useful only if moving by lines in insert mode or if the auto-indentation is wrong. Neither of these two should happen often.
    – Matt
    Nov 28 at 12:13
2

You have autoindent on:

            *'autoindent'* *'ai'* *'noautoindent'* *'noai'*
'autoindent' 'ai'   boolean (default off)
            local to buffer
    Copy indent from current line when starting a new line (typing <CR>
    in Insert mode or when using the "o" or "O" command).  If you do not
    type anything on the new line except <BS> or CTRL-D and then type
    <Esc>, CTRL-O or <CR>, the indent is deleted again.  Moving the cursor
    to another line has the same effect, unless the 'I' flag is included
    in 'cpoptions'.

As a workaround you can type something there, for example space and backspace which could be mapped as:

nnoremap o o<space><bs>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.