Vim command to insert blank line in normal mode

后端 未结 4 1000
时光说笑
时光说笑 2020-12-10 01:39

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 switc

相关标签:
4条回答
  • 2020-12-10 01:51

    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

    0 讨论(0)
  • 2020-12-10 01:51

    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

    0 讨论(0)
  • 2020-12-10 01:53
    :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.

    0 讨论(0)
  • 2020-12-10 02:07

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

    0 讨论(0)
提交回复
热议问题