How to map ALT key in .vimrc?

17,361

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)

Share:
17,361

Related videos on Youtube

Eric Wilson
Author by

Eric Wilson

Updated on September 18, 2022

Comments

  • Eric Wilson
    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 and ALT-O to o<ESC> and o<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 inserting hello and returning to command mode.

    I'm using vim with cygwin.

    • Eric Wilson
      Eric Wilson almost 13 years
      I'm using vim on cygwin, but I have made other scripts work, as my edit details.
    • Eric Wilson
      Eric Wilson almost 13 years
      Ctrl works, sort of. It doesn't seem to let me distinguish between <C-o> and <C-O>.
    • tcoolspy
      tcoolspy almost 13 years
      Giles 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
    Eric Wilson almost 13 years
    This also doesn't work.
  • tcoolspy
    tcoolspy almost 13 years
    This isn't always the case, on different keyboard layouts and language locales they are often different.
  • Martin Ueding
    Martin Ueding almost 13 years
    I tried map <A-o> o<ESC> and it worked for me. If you put it in your .vimrc, did you reload it?
  • tcoolspy
    tcoolspy almost 13 years
    The 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
    Tryer over 2 years
    Can you explain the logic behind this and why this should work?
  • laktak
    laktak over 2 years
    This 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).