8

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.

3
  • is the fn part really important?
    – arsaKasra
    May 14, 2014 at 18:12
  • I figured it would be a good choice to avoid shortcut collisions. But no, I'd settle with something else.
    – Daniel Que
    May 14, 2014 at 18:19
  • 1
    If you have a layout with AltGr enabled you can remap AltGr-hjkl using xmodmap (but excpect to have to call xmodmap often) or by defining a new keyboard variant.
    – Rmano
    May 14, 2014 at 18:43

5 Answers 5

4

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)

1
  • I had to use this to make AltGr+h work asl left arrow: xmodmap -e "keycode 43 = h H h H Left Hstroke hstroke"
    – rubo77
    Mar 28, 2017 at 20:48
3

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.

1

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.

0
1

Since some time xmodmap is depricated! to get a system wide setting you have to use xkb. So edit the language file in /usr/share/X11/xkb/symbols/ to add your changes there.

see https://askubuntu.com/a/898462/34298

1

Here is a full example using Alt_l as the modifier, tested w/ Ubuntu 21.04

xmodmap -e "keycode 64 = Mode_switch" # set Alt_l as the "Mode_switch"
xmodmap -e "keycode 43 = h H Left H" # h
xmodmap -e "keycode 44 = j J Down J" # j
xmodmap -e "keycode 45 = k K Up K" # k
xmodmap -e "keycode 46 = l L Right L" # l

note: you can use the xev command to find the keycode for any key you may want to use as the modifier

1
  • 1
    This solution works good in Xubuntu 22.04, thanks.
    – korst1k
    Dec 16, 2023 at 18:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .