79

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?

1

14 Answers 14

48

I use a mapping for that:

" Quickly insert an empty new line without entering insert mode
    nnoremap <Leader>o o<Esc>
    nnoremap <Leader>O O<Esc>

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

Note: One could argue that it requires as many keystrokes as o<Esc> 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.


Edit I ended up using these mappings instead because there were some edge cases where the new line contained unwanted whitespaces characters:

    nnoremap <Leader>o o<Esc>0"_D
    nnoremap <Leader>O O<Esc>0"_D

Also, the plugin vim-unimpaired does it a bit differently:

function! BlankUp(count) abort
  put!=repeat(nr2char(10), a:count)
  ']+1
  silent! call repeat#set("\<Plug>unimpairedBlankUp", a:count)
endfunction

function! BlankDown(count) abort
  put =repeat(nr2char(10), a:count)
  '[-1
  silent! call repeat#set("\<Plug>unimpairedBlankDown", a:count)
endfunction

nnoremap <Plug>unimpairedBlankUp :call BlankUp(v:count1)<CR>
nnoremap <Plug>unimpairedBlankDown :call BlankDown(v:count1)<CR>

The difference is that my version puts your cursor on the newly created line whereas Pope's version adds the line but leave your cursor on the same place. He also make uses his vim-repeat plugin so that the . command works better.

41

If you don't want to add mappings or a package dependency, you can just yank yy an empty line and then pasted p for below cursor and P for above the cursor.

4
  • 1
    This is probably the best answer without mapping. Commented Jan 3, 2018 at 9:31
  • 12
    Or save it to the o register with "oyy and paste it from there whenever needed with "op.
    – mike23
    Commented Dec 3, 2019 at 20:36
  • This solution combined with the register saving proposed by @mike23 is the smoothest approach. Commented Jun 2, 2023 at 9:20
  • Expert-level simplicity :D Commented Apr 30 at 21:35
28

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 <silent> <leader>o :<C-u>call append(line("."),   repeat([""], v:count1))<CR>
nnoremap <silent> <leader>O :<C-u>call append(line(".")-1, repeat([""], v:count1))<CR>
3
  • 1
    That is a great solution, I even prefer using oo and OO instead of <leader>o
    – Walker Boh
    Commented Dec 31, 2019 at 9:33
  • @WalkerBoh I tried using oo and OO, but there was a delay when using o and O after. Specifically you have to wait 1 sec for o and O to take effect because vim is listening for another potential keystroke. How did you work around this?
    – VishnuVS
    Commented Oct 25, 2020 at 8:57
  • You might want to decrease the timeout, e.g. set timeoutlen=250 ttimeoutlen=0
    – Walker Boh
    Commented Oct 26, 2020 at 13:42
15

I personally recommend using Tim Pope's Unimpaired plugin. It provides many mappings but the ones you will looking for are [<space> and ]<space> 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 <silent> [<space>  :<c-u>put!=repeat([''],v:count)<bar>']+1<cr>
nnoremap <silent> ]<space>  :<c-u>put =repeat([''],v:count)<bar>'[-1<cr>
10

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 <S-Enter> moO<Esc>`o
1
  • 6
    Note that mapping <S-Enter> doesn't work in terminal emulators (it only works in gVim). So you'll need to choose a diffferent key combination to map it to ;-) Commented Jul 8, 2015 at 7:42
9

I use the Vim extension in Visual Studio Code, and I can insert a new line while staying in normal mode with Ctrl+Enter.

6
  • 2
    Welcome to Vi and Vim! I’ve edited your post with a little bit of formatting. Note that what you’re describing sounds a lot like a VSCode-specific key sequence, as that shouldnt work by default in vim.
    – D. Ben Knoble
    Commented Aug 4, 2020 at 23:24
  • I have been trying to find, is this behaviour in VSCode because of a plugin? Do you have any idea? Commented Aug 11, 2020 at 14:24
  • @HamzaZubair I used this plugin
    – zrodr22
    Commented Aug 12, 2020 at 15:30
  • Sorry, not what i meant to ask, I am also using the same extension, what i meant to ask is, what inside that vscodevim-extension is allowing that behaviour, is it a setting, is it a vim-plugin inside the vscode-plugin? Commented Aug 18, 2020 at 6:52
  • @HamzaZubair Seems default behaviour in my VSCode. Using the same plugin. Commented May 15, 2021 at 19:24
4

This answer by Maxim Kim was life-changing:

noremap <silent> <space> :exe "normal i".nr2char(getchar())<CR>

and my riff on it:

noremap <silent> <s-space> :exe "normal a".nr2char(getchar())<CR>

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.

3

These mappings will do exactly what you are looking for.

nnoremap <c-n> @="m`o\eg``"<cr>
nnoremap <c-p> @="m`O\eg``"<cr>   

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 "<cr> :let @@="\n"<cr>
3

I need normal mode much more often than insert mode after inserting a line so I just swapped both:

nnoremap o o<Esc>
nnoremap O O<Esc>

To get the same functionality as before after pressing o or O you just have to press i to get into insert mode.

1
2

My mapping for this

 " Create Blank Newlines and stay in Normal mode
nnoremap <silent> zj o<Esc>k
nnoremap <silent> zk O<Esc>j

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

2
  • what does <silent>mean?
    – Alex
    Commented Jul 8, 2015 at 2:39
  • It prevents echoing anything to the status line. I'm pretty sure it doesn't do anything in this specific case, it was just there wherever I originally copied it from, long ago. See :h map-silent.
    – Kevin
    Commented Jul 8, 2015 at 2:42
2

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"<CR>p``
0

I think the easiest is of course o and then Esc and then for as many lines as you need, you either keep pressing . or do it like 5. (say if you want 5 blank new lines)

0

Simple and effective: In normal mode you don't need <Enter>, so make it do exactly the opposite of J (join lines).

Just map as:

:nnoremap <Enter> i^M<Esc>k$

To add ^M type Control+v and Enter. To add it directly to your ~/.vimmrc, use:

nnoremap <Enter> i<Enter><Esc>k$

Now you can simply press <Enter> and undo with caps J and vice-versa.

1
  • 1
    Bad advice :) Try <Enter> in Normal mode from the Command line window (see q:).
    – xaa
    Commented Feb 25, 2022 at 15:34
0

I use normal ways.

Insert blank lines above:

  O<Esc> ... 3.

Insert blank lines below:

  o<Esc> ... 3.
1
  • 1
    Welcome and thank you for writing an answer. Note, however, that this is precisely what OP ruled out in their question. Furthermore, your answer's almost identical to S. Iqbal's.
    – Friedrich
    Commented Jun 2 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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