Move to the beginning of line while in Insert mode

前端 未结 9 639
慢半拍i
慢半拍i 2021-01-30 02:04

I know that I can use either:

  1. Home in insert mode
  2. Esc + i to exit insert mode and enter it again, effectively going to th
9条回答
  •  一向
    一向 (楼主)
    2021-01-30 02:51

    A shortcut that has worked for me (both muscle memory and intuitiveness) is to map __ (which is a double _) to "insert at start of current line".

    Rationale:

    • _ already goes to the start of line
    • in vim, doubling anything is a very common way of doing that "to this line"
    • double _ doesn't conflict with any motions (you're already at the start of line)
    • your hand is already in the right place if you went to the beginning of the line and now want to insert.

    vimscript:

    "insert at start of current line by typing in __ (two underscores)
    function DoubleUnderscore()
        if v:count == 0 && getcurpos()[2] == 1
            :silent call feedkeys('I', 'n')
        else
            :silent call feedkeys('^', v:count + 'n')
        endif
    endfunction
    nnoremap  _ :call DoubleUnderscore()
    

    It's this complicated because the easy alternative nnoremap __ _I causes vim to delay on pressing _ to distinguish between _ and __.

提交回复
热议问题