Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim command to insert blank line in normal mode

Tags:

Is there any command in Vim that will do the same thing as o or O (insert a blank line before/after the current one), but which doesn't also switch into insert mode?

like image 282
JSBձոգչ Avatar asked Jul 20 '11 16:07

JSBձոգչ


People also ask

How do I insert a blank line in Vim?

Press Enter to insert a blank line below current, Shift + Enter to insert it above.

How do I insert a blank line in Linux?

To answer the question as asked, you'd have to do sed 's/pattern. */&\n/' , otherwise you'll insert the newline right after the match instead of at the end of the line.

How do I enter normal mode in Vim?

By default, Vim starts in “normal” mode. Normal mode can be accessed from other modes by pressing Esc or <C-[> .

How do you make a new line in vi?

vi new line commandsThe lowercase letter "o" lets you open a new line just below your current line. When you use this command, vi creates a new blank line below your current line, and puts you in insert mode at that position. The uppercase letter "o" lets you open a new line just above your current line.


Video Answer


1 Answers

:nnoremap <silent> [<space> :pu! _<cr>:']+1<cr> :nnoremap <silent> ]<space> :pu _<cr>:'[-1<cr> 

Explanation:

  • :put will paste a register linewise below. (:pu! above)
  • :pu _ will paste the blackhole register, which is empty so we get a blank line
  • '[ and '] marks are set at the start and end of a changed or yanked text.
  • :'[ will move the cursor to the starting line of the last change (the put in this case)
  • :'[-1 will move the '[ but up one more line

If you prefer a plugin then I suggest Tim Pope's unimpaired.vim. Which supplies these mappings, but will also take a count. The plugin also has many other nice mappings.

like image 68
Peter Rincker Avatar answered Nov 04 '22 06:11

Peter Rincker