Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am able to map the key combination Right Alt+Left Arrow with <a-left> or by <m-left>. But I am not able to map the Left Alt key.

Is there a way to do that?

Edit:
Just to clarity <a-left> means Alt+Left Arrow.
The important question here is what is difference between Left Alt and Right Alt from the perspective of keymappings in Vim?

share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted
+150

If you do :help key-notation in vim, you'll see that the only modifiers Vim recognizes are shift, control, alt/meta (treated as synonyms), and the Mac command key. You could easily use xmodmap to map your right alt key to a new modifier that Vim recognizes, but perhaps none of those modifiers are currently unused by you. (I don't know whether it's possible to get Linux systems to represent the Mac command key modifier as Vim sees it.) There are other modifiers that X recognizes: Mod4, Hyper, and so on, but it looks like Vim just won't see them or at least can't bind them.

Another option is to arrange for RightAlt-Left Arrow to generate a specific key sequence, for instance, Esc-[MyUniqueGarbageHere. This is certainly doable, and then you could tell Vim to bind that to whatever action you like, which is different than your left alt key is bound to. The downside of this is that you need to do it for each RightAlt+Key combination individually. But if you just want to do this for a few keys, it's very feasible.

Exactly how you implement it will depend on whether you're talking about using Vim in X or in a Linux console, or in some other environment. I'll assume you're talking about using it in X.

  1. Use xmodmap to bind RightAlt to, let's say, Mod3. Arrange for this to be run once when your X session starts. Some distributions already have an /etc/X11/xinit/xinitrc file that will load files at specific locations containing xmodmap commands.

    xmodmap -e "remove Mod1 = Alt_R" -e "add Mod3 = Alt_R"
    
  2. I use the terminal urxvt. It allows one to define keybindings in a Xresources-type file. You could do it in ~/.config/Xresources; I do it in ~/.config/app-defaults/URxvt. I'd put a line like this in that file (the exact syntax is sensitive to which file you put it in):

    URxvt.keysym.Mod3-Left:    \033<RAlt-Left>
    

    Now start a new terminal, type ctrl-V then type RightAlt+Left. You should see at the command line the characters ^[[<RAlt-Left>. Does it work? Good.

  3. In your ~/.vimrc, put this:

    set t_Ff=^[<RAlt-Left>
    

The '^[' here is a single escape character. Each of the other characters ('<', 'R', 'A', etc.) are themselves. Use vim's "literal" key prefix, followed by an escape, to enter the '^[' (I forget what the "literal" key is by default---also ctrl-V, perhaps?) t_Ff is just an unused terminal entry. Another is t_Fg. You can use :set termcap in vim to see which terminal entries are assigned, and just use t_XX for some XX that isn't in use. Follow the .vimrc line presented above with these next ones:

    map <t_Ff> whatever-vim-keys-you-want-it-mapped-to
    map! <t_Ff> whatever-vim-keys-you-want-it-mapped-to

It's also possible to directly do:

    map ^[<RAlt-Left> whatever-vim-keys-you-want-it-mapped-to

However, I've found the technique that uses an idle terminal entry to work much more reliably.

I made a lot of assumptions here: that you're using Linux, X, and so on. If you're using a different terminal program than urxvt, it may or may not provide powerful enough keybinding techniques to do what I've described. If you choose less exotic keys, you may still be able to do something like I've described here. For example, you might arrange for RAlt-Left to be bound to F21, and then go from there. Let us know what your requirements are.

share|improve this answer
    
Does this also apply to gvim? –  Glyph Sep 24 '11 at 7:44
    
I've found this particularly helpful. I use lxterminal and want to map my alt key in vim. In the terminal, when I typed Ctrl-V and then my desired key combo (Alt-T, for instance), it spat out ^[t. I used this in my .vimrc instead of <A-t> to perform the mapping (^[ of course being the single escape character) and it performed flawlessly. (^[^T also correctly binds Ctrl+Alt+T) –  denaje Dec 19 '12 at 17:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.