19

While typing I realize that I need to move to the beginning of the line. Usually I use Esc and I. But I am wondering if there is another way to move to the beginning of the line in the insert mode.

2
  • What platform? Mac Linux Windows GUI or terminal? – broomdodger Jan 25 '14 at 5:18
  • Just to be said, it would certainly be preferable to stick with <Esc> then move, insert, replace or whatever. That way your changes will be registred in the undo tree. – Yannick Jan 27 '14 at 7:34
36

You can use Ctrl-o which switches to normal mode for one command. This allows you to do movements, such as:

  • Ctrl-o, 0 beginning of line
  • Ctrl-o, $ end of line
  • Ctrl-o, f, y find first y in sentence
9

I will remap some shortcut keys in my vimrc, most of them are cursor moving under the Insert mode.

For example, I will use the Emacs-Like (as same as in Linux Terminal) shortcut:

map <C-a> <ESC>^
imap <C-a> <ESC>I
map <C-e> <ESC>$
imap <C-e> <ESC>A
inoremap <M-f> <ESC><Space>Wi
inoremap <M-b> <Esc>Bi
inoremap <M-d> <ESC>cW

That means:

  • Ctrl+a: Go to beginning of the line [Normal Mode && Insert Mode]
  • Ctrl+e: Go to end of line [Normal Mode && Insert Mode]
  • Alt+f: Backward a word [Insert Mode]
  • Alt+b: Forward a word [Insert Mode]
  • Alt+d: Delete a word (backward) [Insert Mode]

Of cause, vim has default shortcut key for Delete a word (forward) [Insert Mode], that is Ctrl+w

1

The Home key works in Vim while in insert mode.

0

While in Insert mode, the "Home" key moves the cursor to the beginning of the current line. Perhaps a better way, from Insert mode, is

"CTRL+o :X"

where X is the line number, and you can now move to the beginning of any line number you specify. (Note: ":set nu" enables line numbering. ":set nonu" disables it).

If you don't need the beginning of the line, per se, please note, you can also use other movements to your liking. For example, "CTRL+o 10k" moves exactly 10 lines from the current line position. Your cursor is not moved to the beginning of the line, however, rather preserving your cursor's relative position in line.

From Normal mode, however, "SHIFT+I" positions the cursor in the first position of the current line, in Insert mode.

This is my Vim version and platform:

$>vim --version VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 21 2021 23:02:54) MS-Windows 64-bit console version Included patches: 1-2541 Compiled by appveyor@APPVYR-WIN Huge version without GUI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.