Solve Alt or Option Key Binding And Map Vim Command With It
27 August 2022 | 2 min read | vim
Let’s solve Alt/Option
Meta (M) key binding and use it along with some other keys to execute vim commands. First of all, we need to find
what characters the terminal is sending when Alt/Option
key is pressed along with other key. To find that, execute the cat
command and
press Alt
key with the key you want. In this example, Press Alt
along with right arrow Alt + →
~ $ cat
^[f
Pressing Alt + →
generated the character ^[f
in the terminal. Here character ^[
is a iteral escape character and character f
is a
normal f key. Basically,Alt + →
and Alt + f
is the same, verify that running cat
command again and press Alt + f
the output will be
the same and pressing escape
key outputs ^[
Now, we know that character f
is used for right arrow.
Open vim configuration file .vimrc
and add the following.
exec "set <M-f>=\ef"
map <M-f> gt
The preceding code tells vim to recognize the escape-sequence as a Meta-character and maps the Meta-character with the vim command gt
open next tab. We have fixed the meta key (Alt/Option) keybinding with the right arrow Alt + →
. To fix all the character bindings along with Meta key Vim Tips Wiki has a very good article (Fix meta-key) , check it out.