Move to the beginning of line while in Insert mode

Vim

Vim Problem Overview


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 the beginning of line.

But neither satisfies me. In first case I have to tilt my head to hit Home, because I can't blindly hit it. In second case my left arm has to leave the home row to hit Esc, which is annoying too.

Any thoughts?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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>

Solution 3 - Vim

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

Solution 4 - Vim

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.

Solution 5 - Vim

Your best course of action is to remap the action to a different key (see https://stackoverflow.com/questions/3503252/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

Solution 6 - Vim

You can map the keys to this:

inoremap II <Esc>I

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

Solution 7 - Vim

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

Solution 8 - Vim

ctrl+o then 0
     |      |
  letter  number

Solution 9 - Vim

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionValentin VView Question on Stackoverflow
Solution 1 - VimGreg SextonView Answer on Stackoverflow
Solution 2 - VimXavier T.View Answer on Stackoverflow
Solution 3 - VimJonathan CutrellView Answer on Stackoverflow
Solution 4 - VimCommanderHKView Answer on Stackoverflow
Solution 5 - VimtskulbruView Answer on Stackoverflow
Solution 6 - VimpocoOView Answer on Stackoverflow
Solution 7 - VimMahmoud Al-QudsiView Answer on Stackoverflow
Solution 8 - VimMetalGodwinView Answer on Stackoverflow
Solution 9 - VimRajat NegiView Answer on Stackoverflow