Valentin V
Valentin V

Reputation: 25729

Move to the beginning of line while in Insert mode

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?

Upvotes: 85

Views: 55222

Answers (9)

Rajat Negi
Rajat Negi

Reputation: 1

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>

Upvotes: 0

MetalGodwin
MetalGodwin

Reputation: 3860

ctrl+o then 0
     |      |
  letter  number

Upvotes: 0

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29519

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

Upvotes: 0

pocoO
pocoO

Reputation: 1

You can map the keys to this:

inoremap II <Esc>I

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

Upvotes: 0

Jonathan Cutrell
Jonathan Cutrell

Reputation: 516

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

Upvotes: 25

CommanderHK
CommanderHK

Reputation: 1138

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.

Upvotes: 3

Greg Sexton
Greg Sexton

Reputation: 9279

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.

Upvotes: 159

Xavier T.
Xavier T.

Reputation: 42218

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>

Upvotes: 60

tskulbru
tskulbru

Reputation: 2346

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

Upvotes: 1

Related Questions