1

On a Mac I can go to the start and end of an line in vi, when not in INSERT mode:

^ (start of line)
$ (end of line)

This is for example explained here: How do I do “end” , “home”, “page up” and “page down” in my terminal/VIM editor?

But how do I go to the beginning and end of an line in vi, when in INSERT mode?

cmd+/ scrolls between windows
alt+ goes backwards one word
ctrl+/ does nothing
shift+/ goes backwards/forwards one word
ctrl+cmd+/ goes backwards/forwards one word
fn+left/ goes to the beginning/end of the terminal buffer

Summary of solution provided by @romainl below:
Terminal > Preferences > Settings > Keyboard > +
Key: Home, Modifier: None, Action: Send Text, \017^
Key: End, Modifier: None, Action: Send Text, \017$

I found this very neat solution, which saves a key stroke: Move to the beginning of line while in Insert mode.

ctrl+o followed by 0 (home) or $ (end) or ^ (first non-white-space character)

And here is a very elaborate answer: Traversing text in Insert mode

1
  • Your person view points does not really add anything of value to the question. Can you remove this commentary and focus on what your question is exactly?
    – Ramhound
    Apr 25, 2014 at 16:05

2 Answers 2

-1

First, you are not supposed to move the cursor around when in insert mode.

Second, only some Mac keyboards come without <Home>/<End> keys. You should have taken a closer look before shelling out all that cash for a computer that's so painful to use.

Third, you can use the following shortcuts when you don't have physical <Home>/<End> keys:

  • fn+ for <Home>
  • fn+ for <End>

Fourth, Terminal.app captures <Home>/<End> anyway so you'll have to edit those bindings in the "preferences" window if you want Vim to receive <Home> and <End>.

For <Home>, press Ctrl+o (it inserts \017) then ^:

Home

For <End>, press Ctrl+o (it inserts \017) then $:

End

4
  • 0) Thanks. Worked. You are genius. 1) I can on Linux and Windows, so I want to be able to on Mac. 2) Wanted Unix based OS and work only supports Ubuntu, which I like much less than Mint, which I run on my PC at home. 3) Thanks. Doesn't work in INSERT mode. 4) Thanks for the solution. I have added a short summary of your solution to my question. Thanks! Apr 29, 2014 at 9:32
  • I just have to be careful not pressing the home key, when on the terminal command line, because the written command is executed, if I press home or end; i.e. after applying your fix. Apr 29, 2014 at 9:55
  • You could set iterm up so that it sends a very special sequence and map that sequence in Vim.
    – romainl
    Apr 29, 2014 at 10:02
  • I will try that. If I do \033[F for End and \033[H for Home, then Home and End works in the terminal, but not in Vim. Apr 29, 2014 at 10:56
1

The system gvimrc file distributed with MacVim has two sets of mappings. One is enabled by default, the other disabled. Curiously, neither contains a command to go to the start/end of the line in Insert mode, but it is still worth taking a look:

:e $VIM/gvimrc

You could add something to your vimrc file like this:

inoremap <D-Left> <Home>
inoremap <D-Right> <End>

A little test shows that this works in the GUI but not the terminal, so maybe that should go in your gvimrc file. How about

inoremap <C-V><S-Left> <Home>
inoremap <C-V><S-Right> <End>

Where you do NOT type those commands literally: <C-V> is one character and <S-Left> is another. On my machine, using Mac's Terminal.app, I get the same effect with this, typed literally:

inoremap <Esc>OC <End>
inoremap <Esc>OD <Home>

When using the GUI, this may get overwritten by the settings in the system gvimrc file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .