Vim on Mac OS X Terminal: moving the cursor over a word

I followed the recommendations in Is there any way in OS X Terminal to move the cursor by word? , and now in the terminal I can move the cursor word by word.

However, in Vim, I can only move backward in a word. I can’t move forward word by word, regardless of whether I set \ 033f to option+cursor-right or shift+cursor-right . The only workaround I found out was to go into normal mode and press <w> to go to the next word. Any idea on how to fix this? Thanks.

+8
vim macos
source share
3 answers

w not a workaround. This is the main way to move around a word (see also: w , b , b , e , e ). What you want is a workaround that won't help you learn Vim at all.

+9
source share

Some may argue that navigating with VIM keyboard shortcuts is not a workaround, but for me it is accurate and very inconvenient and very inefficient. Especially when you need to switch between insert and normal modes to go to the next word.

That's why I came up with a solution based on this answer on SuperUser. The idea is to map the input provided by Terminal.app directly to VIM. The answer to SU shows what to put in your vimrc file for the Home / End keys to work as expected.

My modified version below includes navigation on the words Option+arrow (or Alt+arrow ). I tried to simulate the behavior of a terminal moving as close as possible. Therefore, pressing Option+Right ( Alt+Right ) will move the carriage to the next character after the word (as opposed to the last character of the word, which is VIMs w native bahavior).

 let tp=$TERM_PROGRAM if tp == 'Apple_Terminal' :" map Mac OS X Terminal.app " map Home/End: :map <ESC>[H <Home> :map <ESC>[F <End> " small 'o' letter in <Co> means no exit from the insert mode :imap <ESC>[H <Co><Home> :imap <ESC>[F <Co><End> :cmap <ESC>[H <Home> :cmap <ESC>[F <End> " map Option+Left/Option+Right: " for this to work you must have the bindings in Settings > Keyboard set " as follows: " 'option cursor left' to '\033b' " 'option cursor right' to '\033f' :map <ESC>f el :imap <ESC>b <Co>b :imap <ESC>f <Co>el :cmap <ESC>f el endif 

As a small but significant bonus, you get start / end navigation without leaving insert mode. Tested on 10.8.5 .

+1
source share

Let me clarify something for you. Bash (a shell program running inside the terminal) has the behavior you're used to. (Using Alt + f to move forward in a word and Alt + b to move backward in a word.) This was originally done as Emacs. You can use the command

 set -o vi 

to switch to vim behavior. In this mode, you can use Esc to switch to normal mode and navigate like in vim; then press i to return to insert mode.

So don't be surprised that Vim is not trying to act like Emacs. Behind these simple movements lies all the vim power.

+1
source share

All Articles