在“插入”模式下移动到行首

我知道我可以使用:

  1. 插入模式下的 Home
  2. Esc + i退出插入模式并再次进入,有效地进入行的开头。

但我都不满意。在第一种情况下,我必须倾斜我的头击中 Home,因为我不能盲目地击中它。在第二种情况下,我的左臂不得不离开主排去打 Esc,这也很烦人。

有什么想法吗?

52800 次浏览

Your best course of action is to remap the action to a different key (see How to remap <Ctrl-Home> to go to first line in file? for ideas)

I'd think of how often I use this "feature" and map it to a keystroke accordinly

Ctrl+O whilst in insert mode puts you in command mode for one key press only. Therefore Ctrl+O then Shift+I should accomplish what you're looking for.

You could enter insert mode using I (capital i).

It will put the cursor at the beginning of the line.

Similarly you can use A to add something at the end of the line.

Though, it does not really solve the problem of moving while already being in Insert mode.

I have just checked help on Insert mode, there is no key combination in insert mode to move at the beginning of the line.

Other idea : Remap a new command only in insert mode

inoremap <C-i> <Home>

If you are using MacOS Terminal go to Preferences...>Settings>Keyboard and map the end key to Ctrl-O$ (it is displayed as \017$) and then use fn+left to simulate the end key. Do the same for the home key. Escape sequence \033[H also works for home.

I've got Ctrl+a and Ctrl+e mapped to beginning and end of line, respectively. This matches the behavior of most bash command lines. Works well for me.

inoremap <C-e> <Esc>A
inoremap <C-a> <Esc>I

You can map the keys to this:

inoremap II <Esc>I

ref: http://vim.wikia.com/wiki/Quick_command_in_insert_mode

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 <silent> _ :call DoubleUnderscore()<CR>

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

ctrl+o then 0
|      |
letter  number

i use this command to go to end of line without leaving insert mode

inoremap jl <esc><S-a>

Similarly to go to beginning of line will be:

inoremap jl <esc><S-i>