22

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?

9

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.

8

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>
5

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

  • This is probably the best answer without mapping. – Willem van Ketwich Jan 3 '18 at 9:31
  • Or save it to the o register with "oyy and paste it from there whenever needed with "op. – mike23 Dec 3 at 20:36
4

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
  • 5
    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 ;-) – Martin Tournoij Jul 8 '15 at 7:42
4

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>
2

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.

2

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>
1

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.

  • what does <silent>mean? – Alex Jul 8 '15 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 Jul 8 '15 at 2:42
-1

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``

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.