I want to be able to use Option-left and Option-right to skip words (and Cmd-left/right to go to beginning and end of lines) within Vim as it does at my shell prompt. My Iterm2 preferences have mappings to do this (e.g. Option-left
to Esc-H
and a one for option-right
to Esc-F
to skip over words), and this works in the shell locally or when ssh'd to a remote server.
When I use Vim locally or remotely, option-left
works, but option-right
does not. I suspect this is because Vim naturally listens for Esc-H
, but not Esc-F
. I am able to get around this by modifying .vimrc file to Esc-b
to b
and Esc-f
to f
, but I don't want to do this to every server I'm connecting to.
Similarly, I have the same desired setup for Cmd-left/right for going to beginning and end of a line. I can get this working in the shell via Iterm2 mappings (e.g. Cmd-left
to Esc-[h
), but Vim doesn't respond at all to this unless I map keys again (e.g. Esc-[h
to ^
).
Update: I just figured out how to get option-left/right working. I changed mapping in iTerm2 for these to be escape-[1;5D
and escape-[1;5C
respectively. I still want to solve the Cmd-left/right problem though (I changed my question's title to reflect this). Any ideas?
To mimic OS X's behavior of sending Cmd-left/right
to the beginning/end of a line, I add the following mappings in iTerm2:
Cmd-left
to escape-sequence [1~
Cmd-right
to escape-sequence [4~
To mimic OS X's behavior of sending Option-left/right
to the previous/next word, I add the following mappings in iTerm2:
Option-left
to escape-sequence [1;5D
Option-right
to escape-sequence [1;5C
Special thanks to this blog post for tracking down what I was missing with the cmd-left/right
mappings
Note: this doesn't work in my local python shell on OS X, but when ssh'd to linux machine, these work in python shell. No idea why. If I use the mappings ilias mentioned below, they don't work in Vim, but everywhere else, including python they work
The python shell in OS X doesn't use readline unless you install pypi.python.org/pypi/readline
This works nice in
vim
but it doesn't work inbash
(e.g. when writing a long command) :(Hi @DolanAntenucci , what the equivalent mappings would be in .vimrc (my workflow is Terminal.app, tmux, and vim)? Something like map <M-Left> [1;5D and <M-Right> [1;5C ?
Neither this solution, nor Karim's worked for my Cmd-left/right bindings. I had to set
bindkey '^[[1~' beginning-of-line
andbindkey '^[[4~' end-of-line
in my~/.zshrc
, then I was able to set the escape codes per this answer. Everything works as expected now in both iTerm2 and Vim.