how to insert a newline without leaving normal mode

2018-01-03 04:52:09

I wonder if I can insert a new line without leaving normal mode. For example, I have two lines:

this is line one

this is line two

And the cursor is on line one. Now I want a new line between line one and line two, like this:

this is line one

this is line two

I know that when I am in normal mode, I can simply press o. But this would enter insert mode.

How can I insert a new line like and stay in normal mode?

I use a mapping for that:

" Quickly insert an empty new line without entering insert mode

nnoremap o o

nnoremap O O

This way you can insert a line under your cursor with o and one on the previous line with O.

Note: One could argue that it requires as many keystrokes as o but hopefully you choosed your leader to make this kind of mapping easy. Also I'm really not sure there is a built-in way to do this.

A solution that doesn't go through insert mode, doesn't move the cursor, and allows you

  • I use a mapping for that:

    " Quickly insert an empty new line without entering insert mode

    nnoremap o o

    nnoremap O O

    This way you can insert a line under your cursor with o and one on the previous line with O.

    Note: One could argue that it requires as many keystrokes as o but hopefully you choosed your leader to make this kind of mapping easy. Also I'm really not sure there is a built-in way to do this.

    2018-01-03 04:52:12
  • A solution that doesn't go through insert mode, doesn't move the cursor, and allows you to use a counter to append several lines at once (3\o etc.):

    nnoremap o :call append(line("."), repeat([""], v:count1))

    nnoremap O :call append(line(".")-1, repeat([""], v:count1))

    2018-01-03 05:05:39
  • My mapping uses Shift-Enter. It differs from the others in that the cursor is returned to the same line and column using mark o.

    " Insert new line above without going into insert mode

    " (uses mark o to return to the previous cursor column)

    nnoremap moO`o

    2018-01-03 05:16:59
  • I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [ and ] which create blank lines above and below the current line respectively. Unimpaired also provides nice mappings for moving through the quickfix list, buffer list, option toggling, and many others. See :h unimpaired for more.

    If you do not want to use unimpaired plugin but like the mappings below are some quick mappings to put in your ~/.vimrc file:

    nnoremap [ :put!=repeat([''],v:count)']+1

    nnoremap ] :put =repeat([''],v:count)'[-1

    2018-01-03 05:17:02
  • My mapping for this

    " Create Blank Newlines and stay in Normal mode

    nnoremap zj ok

    nnoremap zk Oj

    Different from statox's as this keeps the cursor at the same location.

    2018-01-03 05:24:21
  • This answer by Maxim Kim was life-changing:

    noremap :exe "normal i".nr2char(getchar())

    and my riff on it:

    noremap :exe "normal a".nr2char(getchar())

    Put those lines in your vimrc. Then go to the beginning of the second line of your file and hit Space, Enter, or go to the end of the first line and hit Shift+Space, Enter. Either will add the empty line between the first and second lines, and leave you in normal mode.

    You can also use these for any other single key - hit Space or Shift+Space followed by the key you want and it will insert or append it, respectively.

    2018-01-03 05:41:53
  • These mappings will do exactly what you are looking for.

    nnoremap @="m`o\eg``"

    nnoremap @="m`O\eg``"

    These insert n number of newlines before or after the cursor; keeping the cursor exactly where it is, without changing the jump list.

    Another solution is to just make a shortcut that fills the

    unnamed register with a newline. Then you can just use the put

    commands, p or P to insert newlines.

    For example:

    nnoremap " :let @@="\n"

    2018-01-03 05:46:53
  • If you don't want to add mappings, you can yank yy an empty line and then pasted p for blow cursor and P for above the cursor.

    2018-01-03 05:52:10
  • My personal solution sticks a newline character into the expression register and pastes it after the current line, then jumps back to the original position:

    nnoremap K m`"="\n"p``

    2018-01-03 05:52:21