Mapping Left Alt key in Vim?
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?
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.
-
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"
-
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.
-
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.