How to map ALT key in .vimrc?
Solution 1
To see what your terminal is sending when you press a key, switch to insert mode, press Ctrl+V
, then the key. Most keys with most terminals send an escape sequence where only the first character is a control character; Ctrl+V
inserts the next character literally, so you get to insert the whole escape sequence that way.
Different terminals send different escape sequences for some key combinations. Many terminals send the same character (^O
) for both Ctrl+O
and Ctrl+Shift+O
; if that's the case, Vim won't be able to distinguish between them.
You mention that you're using Cygwin; which terminal you're running Vim in is the most pertinent information. If you're using the native Windows console, get yourself a better terminal. I recommend MinTTY for running Cygwin text mode applications in a terminal in Windows outside X, but Cygwin's Windows-native RXVT and PuTTYcyg are also good. (Also Console2 to run Windows console applications, but that's squarely off-topic here).
Solution 2
If Control+V
followed by ALT-x
shows ^[x
(type in terminal) you can fix it with this small script from vim.wikia.com:
for i in range(97,122)
let c = nr2char(i)
exec "map \e".c." <M-".c.">"
exec "map! \e".c." <M-".c.">"
endfor
Add to .vimrc for all alt key mappings.
Solution 3
This works for me on Ubuntu 16.04 xfce terminal (and alacritty rust terminal)
Set ultisnip snippet trigger to Meta-/
(just like emacs snippet)
let g:UltiSnipsExpandTrigger="^[/"
Here's now I type ^[/
in vim
In insert mode Ctrl-V Alt-/
(Meta
is the Alt
key on my PC keyboard)
Related videos on Youtube
Eric Wilson
Updated on September 18, 2022Comments
-
Eric Wilson over 1 year
In order to be able add blank lines without going into insertion mode, I'm trying to map
ALT-o
andALT-O
too<ESC>
ando<ESC>
I've tried the following in my
.vimrc
map <M-o> o<ESC> map <M-O> O<ESC>
and
map <ALT-o> o<ESC> map <ALT-O> O<ESC>
and (as suggested below)
map <A-o> o<ESC> map <A-O> O<ESC>
but none work. It just gives the usual behavior, as if
ALT-o
had not been defined.This is my first time altering the
.vimrc
file, and I can't find where the documentation tells you how to designate the various keys. But I am able to verify that my.vimrc
file is being read, by including:map <Enter> ihello<ESC>
Which sucessfully maps
<Enter>
to insertinghello
and returning to command mode.I'm using vim with cygwin.
-
Eric Wilson almost 13 yearsI'm using vim on cygwin, but I have made other scripts work, as my edit details.
-
Eric Wilson almost 13 yearsCtrl works, sort of. It doesn't seem to let me distinguish between
<C-o>
and<C-O>
. -
tcoolspy almost 13 yearsGiles answer is right on track. You should definitively switch to a more capable terminal. These problems should disappear and you'll get your keys back!
-
-
Eric Wilson almost 13 yearsThis also doesn't work.
-
tcoolspy almost 13 yearsThis isn't always the case, on different keyboard layouts and language locales they are often different.
-
Martin Ueding almost 13 yearsI tried
map <A-o> o<ESC>
and it worked for me. If you put it in your.vimrc
, did you reload it? -
tcoolspy almost 13 yearsThe asker already stated in their question that they verified the file was being loaded. This isn't relative to the user since they are on windows, but you can see different Meta/Win/Alt key configurations in the xkb geometry/symbol/keycode files. You will notice even in the gnome keyboard layout options there are several common ways to remap between Alt/Win/Meta. One of the reasons this exists is because of the disparity in base layouts.
-
Tryer over 2 yearsCan you explain the logic behind this and why this should work?
-
laktak over 2 yearsThis changes the sequence that vim expects to the one that is sent by the terminal. The loop changes it for all keys in that range (a-z).