How to jump to the beginning or end of line in vim + OS X

17

7

In terminal I can do this by pressing shift+home and shift+end, but it doesn't seem to work in vim. Home and end keys scroll terminal window only. I would like to jump in edit mode.

JtR

Posted 2010-03-25T10:40:13.203

Reputation: 1 549

4

In command mode, you can find on the Vim Cheat Sheet (0 and $): http://www.fprintf.net/vimCheatSheet.html. I don't know about edit mode, though.

– Gnoupi – 2010-03-25T10:48:38.783

Answers

13

This answer assumes you are using vim in a Terminal window.

The default bindings for Home and End are a bit different from what a program would expect based on the usual TERM setting (i.e. an xterm variant). If you want to stick with the default bindings (and having to use the Shifted variants in Terminal), you might try putting something like this in your .vimrc:

:" map Mac OS X Terminal.app default Home and End
:map <ESC>[H <Home>
:map <ESC>[F <End>
:imap <ESC>[H <C-O><Home>
:imap <ESC>[F <C-O><End>
:cmap <ESC>[H <Home>
:cmap <ESC>[F <End>

Otherwise, you might try adjusting the codes that Terminal sends when you use Home and End (unshifted even, if you like). A search would probably turn up some places that describe how to do it (here is one). The basic idea is that you want to have Terminal send ESC[1~ instead of ESC[H for Home and ESC[4~ instead of ESC[F for End (or Shift-Home/Shift-End if you want to keep Home/End for accessing scrollback). I would describe the process in more detail, but I only have 10.4, and I know Terminal was changed a lot in 10.5 and later.

Chris Johnsen

Posted 2010-03-25T10:40:13.203

Reputation: 31 786

8

With iTerm and Vim you can use fn+right and left arrows in insert mode to jump to begin or end of line. With Terminal.app you use shift+arrows.

giorni

Posted 2010-03-25T10:40:13.203

Reputation: 126

7

use 0 and $ to move to beginning and end of line. Refer this site : http://alvinalexander.com/linux/vi-vim-editor-end-of-line.

user218243

Posted 2010-03-25T10:40:13.203

Reputation: 89

5The OP wants to jump in editing mode, not in command mode, so these keys will just literally input 0 and $. – slhck – 2013-04-19T09:32:56.447

1

As an addition to Chris answer, you may also achieve word navigation (with Option+arrow keys), just by adding those four lines:

:map <ESC>f el
:imap <ESC>b <C-o>b
:imap <ESC>f <C-o>el
:cmap <ESC>f el

Notice the o is lower case, so that it won't exit insert mode for the operation. You can also change the uppercase O in the original answer for the same effect.

Maciej Sz

Posted 2010-03-25T10:40:13.203

Reputation: 111

0

Go to Terminal -> Preferences -> Profiles -> Keyboard and add:

Key: ↖ Action: \033OH

Key: ↗ Action: \033OF

Alfredo Cardigliano

Posted 2010-03-25T10:40:13.203

Reputation: 1

0

In MacVim, you can use Command 'Left Arrow' and Command 'Right Arrow' to go to the beginning and end of line while being in edit mode.

Dominik

Posted 2010-03-25T10:40:13.203

Reputation: 767

Sounds like JtR is using vim inside Terminal.app instead of using MacVim (which I would highly recommend, and personally use, as well). – Matrix Mole – 2010-03-25T11:57:56.213