iTerm2 Vim alt+right/left arrow
As a Linux user, I am very used to jump from word to word in vim/nano using ALT+left or right.
This doesn't seem to work properly using iTerm, I am using zsh, I tried adding;
bindkey -e
bindkey '^[[1;9C' forward-word
bindkey '^[[1;9D' backward-word
It does work, but inside zsh only, then I commented those lines and added in iTerm a keyboard shortcut;
It does work, but only for the ALTleft
How can I make it work for the right arrow too?
Solution 1:
In iTerm's properties go to Profiles -> Keys and setup there
- For ⌥→ Send Escape Sequence
[1;5C
- For ⌥← Send Escape Sequence
[1;5D
Solution 2:
Start by viewing the key code your terminal is sending to vim:
$ sed -n l
^[[1;9D
In the above example, i ran the sed command and pressed Alt + Left.
The ^[[1;9D
is the escaped sequence being sent to vim, so we can user that for our mapping.
Add to vimrc:
map <Esc>[1;9D :tabn<CR>
alternative solution for macOS
if sed -nl
is not working as expected, try CTRL + V instead
You can determine the current mapping using the "control-V trick": First press control-V, then the keystroke you like to examine. This will print the characters that are sent to the terminal. http://www.macfreek.nl/memory/Backspace_and_Delete_key_reversed
For example typing control-V + delete
prints ^?
. I have only verified this on macOS 11.
Solution 3:
I read another post describing that for option-left and option right, you need to bind them to the actions ^[b and ^[f, respectively. That is, you bind them to "Send escape sequence" and bind key b and f.
http://elweb.co/making-iterm-2-work-with-normal-mac-osx-keyboard-shortcuts/