Vim命令在普通模式下插入空白行

问题描述:

Vim中是否有任何命令可以执行与oO相同的命令(在当前命令之前/之后插入空白行),但是该命令也不会切换到插入模式?

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>

说明:

  • :put将在下面逐行粘贴一个寄存器. (以上:pu!)
  • :pu _将粘贴黑洞寄存器,该寄存器为空,所以我们得到一个空白行
  • '[']标记设置在已更改或被选中的文本的开头和结尾.
  • :'[会将光标移动到最后一次更改的起始行(在本例中为put)
  • :'[-1将使'[移动,但又向上移动一行
  • :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

如果您喜欢插件,那么我建议蒂姆·波普(Tim Pope)的 unimpaired.vim .其中提供了这些映射,但也需要计数.该插件还具有许多其他不错的映射.

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.