How to make Home and End behave in vi, under OS X.
So you've got Home and End working properly in Terminal.app. Now you go to use vi, start editing a file, switch to insert mode, and quickly find out that Home and End don't work.
Yeah, go ahead. Curse Apple. Let it all out.
Feel better? Right then. Let's fix vi.
First, type vi ~/.vimrc. It is essential we edit this file in vi as it gives us the ability to type keystrokes without actually doing what those keystrokes do.
Next, switch to insert mode and type map followed by a space. Now press Ctrl-V, and then press the Home key. Depending on what you've set Home to be in your Terminal.app preferences, you will see a character sequence. I have Home set to Escape-[-H (or \033[H), so I see a blue ^\[ followed by \[H. Now type space, and then type <Home>. This is literally the word "Home" in angle brackets, not the Home key.
Got that? The first "Home" is Ctrl-V, Home. The second "Home" is <Home>.
Once you type that, you will see something like this on-screen (provided you have syntax highlighting on):
map ^[[H <Home>
So what does this do? Well, as you can see here, map allows you to map keys in normal, visual, and operator-pending mode. Really the only thing we're concerned about is normal mode, which is the first mode you're in when you launch vi. So, this line tells vi, "when I press Home, this really means Home and not Escape-[-H."
However that's not really what we want here --- what we want is to map Home in insert mode. We do that with imap. So now type imap, space, Ctrl-V, Home, space, <Home>. This is what I saw after I typed that:
imap ^[[H <Home>
Okay. Let's test it out. Save and exit (:wq) and re-enter vim. Now go into insert mode and try pressing Home and End.
Now repeat the same thing for End. You will also want to add another rule for forward delete. Logically, <Delete> represents the delete key.
This is the full contents of my ~/.vimrc file:
map ^[[H <Home>
imap ^[[H <Home>
map ^E <End>
imap ^E <End>
imap ^D <Delete>
Hope that helped!☯