简体   繁体   中英

Vim command to insert blank line in normal mode

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?

: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.

In insert mode:

:normal O

From vim inline manual:

Execute Normal mode commands {commands}. This makes it possible to execute Normal mode commands typed on the command-line. {commands} are executed like they are typed. For undo all commands are undone together. Execution stops when an error is encountered. If the [,] is given. mappings will not be used. {commands} should be a complete command, If {commands} does not finish a command. the last one will be aborted as if or was typed.

http://vimdoc.sourceforge.net/htmldoc/various.html#:normal

I got the hint there: https://unix.stackexchange.com/a/16452/7914

you can try something like this:

:map <c-j> o<esc>

this is: when you press control + j it will add a line below (and go to insert mode) and then switch back to normal mode if you want to stay in the same line you where before just add a k at the end, something like this:

:map <c-j> o<esc>k

I also added a control + k to add a line before the one I's standing on

:map <c-k> O<esc>

and just like the one before you can add a j at the end to stay on the same line:

:map <c-k> O<esc>j

If you need or want something more advanced you can check this posts: Quickly adding and deleting empty lines or Insert newline without entering insert mode

dd on a blank line, p to restore it, and then p where you want the blank line(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM