How to map <modifier> + hjkl to arrow key functionality?

I've been getting so used to vim's hjkl for moving around recently, and find myself wanting to use them right after alt tabbing out of vim.

What would be the best way to map fn + h/j/k/l to left/down/up/right?

edit: any <modifier> + h/j/k/l would be satisfactory as long as it doesn't conflict with other keyboard shortcuts.


Try these, no guarantee if it will break anything, but I hope it won't:

  1. if you look at the mapping by xmodmap -pke, you see it has four columns of characters mapped to each key, for different modifications. The order for these modifications is like

    keycode <x> = <1> <2> <3> <4>

    <1> <key>

    <2> <shift-key>

    <3> <"Mode_switch"-key>

    <4> <shift-"Mode_switch"-key>

  2. "Mode_switch", not completely sure, but I guess maps to nothing by default. Actually, you can see the code for the key with xmodmap and looking for it somewhere at the bottom of the results and finding out what key that code belongs to. What I did was I reassinged it to another key, for example Super_L, which is the lefp super key, with these:

    xmodmap -e "keysym Super_L = Mode_switch"
    

    or you can put your own choise in place of Super_L

  3. Now, go ahead and edit the column for the four keys you wanted to reassign. Let's find them in your xmodmap file:

    xmodmap -pke | grep h
    

    among the output should be line which says

    keycode 43 = h H h H
    

    edit it so that it's like

    keycode 43 = h H Left H
    

    to do that, use the command

    xmodmap -e "keycode 43 = h H Left H"
    

    and then try if it worked.

  4. Do that for the rest of the keys you want to map.

I think this is not likely to break anything serious, as long as you can go back and reedit the keycodes to their originals. The only scary part was where I mapped "Mode_switch" to Super_L. (Edit: this actually disabled my super key, which I was able to get back to work by reversing the assignment, that's xmodmap -e "keysym Mode_switch = Super_L". Map a key you know would not fail you to Mode_switch)


While I sympathize with you (I have a Happy Hacking keyboard, and would love to have the same layout on my laptop, with Fn + ;'[/ for left right up down respectively), the Function key Fn is almost always implemented entirely in the hardware, and there is no way to bind it as a modifier key in software.


If any modifier will do, then the following work for AltGr using xmodmap.

First, find the keycodes for h,j,k and l. This may be done running xev in terminal, then pressing the respective keys. For me, the keycode for h is 43. To map this plus AltGr to left, use the command

xmodmap -e "keycode 43 = h H NoSymbol NoSymbol Left"

Repeat for the remaining keys, and you'll be done :) To set on start up, see this Q&A.