14

1. How do I globally map Caps Lock to AltGr?

I don't need CapsLock and rather would like to have an easy access AltGr Key there

2. I want to map all movement keys to vim-like positions

  • AltGr+F = Backspace
  • AltGr+H = LeftArr
  • AltGr+J = DownArr
  • AltGr+L = RightArr
  • AltGr+K = UpArr
  • AltGr+U = PgUp
  • AltGr+D = PgDown
  • AltGr+S = Enter
  • AltGr+3 = Pos1
  • AltGr+$ = End
  • AltGr+X = Del

So I can easily walk through my code without moving the hand away from the 10-Finger-Position (like in vim editor)

I found this answer: How do I remap the caps lock key to the backspace key?
suggesting using

xmodmap -e "keycode [code] = [new key]"

see: http://wiki.linuxquestions.org/wiki/List_of_Keysyms_Recognised_by_Xmodmap

But I cannot figure out, how to add this to a combination of for example AltGr+J

Another start would be to set the "Alternative Character Key" in unity-control-center->Keyboard->Shortcuts->Typing as Caps Lock

Update:
I found some solutions (see below) but all of them don't work in all applications, I guess it is a global setting called "XFree 4" that is used by some apps.

1. How do I set the key bindings for XFree 4 also?

2. How do I make the changes upgrade-save?

10
  • I don't have an AltGr key, but you might find this similar question provides some clues.
    – Elder Geek
    Apr 4, 2017 at 14:12
  • 1
    None of the answers here are good. They all suggest modifying system files that will be overwritten on the next update. The right way would be to create an XKB partial file that can be enabled as an option. That way it can stay separated from files belonging to certain packages. I would post that as an answer, but I’m only just reading into XKB configuration (which is severely lacking in documentation, to put it mildly). Aug 17 at 22:29
  • 1
    @Evi1M4chine that would be great! I already created the partial in my answer, I just couldn't figure out how to serve this partial to the system settings. An answer would be mostly appreciated
    – rubo77
    Aug 19 at 5:03
  • @rubo77: I managed to do it! I can now even toggle my new feature in Mint Cinnamon’s keyboard settings! (Which would allow toggling it via a shortcut too.) … Due to how xkb is designed, it actually need a patch for /usr/share/X11/xkb/rules/{evdev,evdev.lst,evdev.xml} (Ideally, there should be directories like evdev.lst.d, but apparently xkb didn’t think of that.) … Since my solution is not a complete answer, I’ll just add it as comments here, and people can modify their answers to include it. Sep 27 at 14:08
  • How to make your symbols an xkb option: In evdev you add a line like MyOptions:my_option = MyOptions(my_option). In evdev.lst, you add MyOptions = My own Options, MyOptions:my_option My great new option for (mandatory) descriptions, and in evdev.xml you add a new <group allowMultipleSelection="true"> that is analogous to the others in the file, but use MyOptions as group.configItem.name, and MyOptions:my_option as group.option.configItem.name, with the same descriptions as in evdev.lst. Sep 27 at 14:18

4 Answers 4

10

1. add a new XKB partial file

sudo vi /usr/share/X11/xkb/symbols/altgr_vim

with this content:

partial keypad_keys
xkb_symbols "altgr-vim" {
  # replace Caps with AltGr
  key <CAPS> { [ ISO_Level3_Shift            ] };
  # Add vim cursor keys to be accessed with AltGr
  key <AB02> { [ x, X, Delete, Delete        ] };
  key <AC02> { [ s, S, KP_Enter, KP_Enter    ] };
  key <AC03> { [ d, D, Next, Next            ] };
  key <AC04> { [ f, F, BackSpace, BackSpace  ] };
  key <AC06> { [ h, H , Left, Left           ] };
  key <AC07> { [ j, J, Down, Down            ] };
  key <AC08> { [ k, K, Up, Up                ] };
  key <AC09> { [ l, L, Right, Right          ] };
  key <AD07> { [ u, U, Prior, Prior          ] };
  key <AE03> { [ 3, section, Home, Home      ] };
  key <AE04> { [ 4, dollar, End, End         ] };
};

2. include the partial in your language file

for example for german it is /usr/share/X11/xkb/symbols/de (where /de is your language) add this inside the first block (xkb_symbols "basic"):

include "altgr_vim(altgr-vim)"

Note: This part has to be repeated after every distribution update

3. reload the window session

Press ALT+F2, enter "r" to reload the gnome session or just log out and in again. Now the new key compositions will be available

4. solve some problems

  1. Some applications don't accept the settings, for example: sublime which can be replaced by atom.

  2. Some applications need extra settings, e.g. in yakuake the key-bindings are set to "XFree 4", if you set them to "Linux" or "Solaris", then the XKB settings works there also:

remaining problems:

  1. some apps still don't accept those settings
  2. after an upgrade, where the symbols file is updated, you have to repeat step 2
3
  • 1
    i strongly recommend using the XKB option lv3:caps_switch instead of changing the <CAPS> definition as you show. see /usr/share/X11/xkb/symbols/level3 (where that option is defined).
    – quixotic
    Apr 4, 2017 at 13:49
  • @quixotic: can you add a new answer with the configurations that would be needed? I could need a good complete answer here for the bounty ;)
    – rubo77
    Apr 4, 2017 at 16:27
  • This worked perfectly for me. Just a heads up though, might want to change "section" to "numbersign" for shift+3
    – mowwwalker
    Feb 27, 2018 at 23:11
2

OK, I decided to adapt @rubo77’s answer to a easy to use, togglable option.

installer script:

#! /bin/bash
# Automatic sudo-ing followed by setting the option right away,
# so you can just run it as the normal user for convenience.
[[ "$UID" == 0 ]] || {
  sudo "$0"
  setxkbmap -option MyOptions:altgr_vim
  exit
}

# Install actual symbols file (see below).
install -o root -g root -m 644 MyOptions /usr/share/X11/xkb/symbols/

# Patch xkb config to add the symbols file as a config option.
echo -e "MyOptions:altgr_vim\t=\t+MyOptions(altgr_vim)" >> /usr/share/X11/xkb/rules/evdev
echo "MyOptions            My own options" >> /usr/share/X11/xkb/rules/evdev.lst
echo "MyOptions:altgr_vim  Caps Lock as AltGr and Arrows like in vim" >> /usr/share/X11/xkb/rules/evdev.lst
patch /usr/share/X11/xkb/rules/evdev.xml <<EOF
@@ -7687,5 +7687,23 @@
         </configItem>
       </option>
     </group>
+    <group allowMultipleSelection="true">
+      <configItem>
+        <name>MyOptions</name>
+        <description></description>
+      </configItem>
+      <option>
+        <configItem>
+          <name>MyOptions:altgr_vim</name>
+          <description>Caps Lock as AltGr and Arrows like in vim</description>
+        </configItem>
+      </option>
+    </group>
   </optionList>
 </xkbConfigRegistry>
EOF

Ideally, this should be executed as a dpkg trigger, so it gets added each time xkb is updated, but I haven’t looked into that yet. If your distribution supports it, patching the package either beforehand or during installation (using a prepared patch) is an idea too.

MyOptions symbols file:

It’s the same as in @rubo77’s answer, so thank him for that!

# Caps Lock as AltGr and Arrows like in vim
xkb_symbols "altgr_vim" {

  # replace Caps with AltGr
  key <CAPS> { [ ISO_Level3_Shift            ] };

  # Add vim cursor keys to be accessed with AltGr
  key <AB02> { [ x, X, Delete, Delete        ] };
  key <AC02> { [ s, S, KP_Enter, KP_Enter    ] };
  key <AC03> { [ d, D, Next, Next            ] };
  key <AC04> { [ f, F, BackSpace, BackSpace  ] };
  key <AC06> { [ h, H , Left, Left           ] };
  key <AC07> { [ j, J, Down, Down            ] };
  key <AC08> { [ k, K, Up, Up                ] };
  key <AC09> { [ l, L, Right, Right          ] };
  key <AD07> { [ u, U, Prior, Prior          ] };
  key <AE03> { [ 3, section, Home, Home      ] };
  key <AE04> { [ 4, dollar, End, End         ] };

};

Put both files into a directory, make sure the installer is executable with chmod +x installer, run installer, and enter your password.

Now you can launch your favorite keyboard settings tool and enjoy observing it being listed in the options for keyboard layouts!

2
  • NOTE: I adapted this from a different use case, where a line key.type[Group1] = "EIGHT_LEVEL_ALPHABETIC_LEVEL_FIVE_LOCK"; was required inside xkb_symbols "altgr_vim" { … } as I’m using the advanced NEO2 layout. I do not know if a key.type needs to be set for this to work too. I could not find any such setting inside e.g. /usr/share/X11/xkb/symbols/de, so I assumed it’s not necessary here. So in case this doesn’t work, this is what needs to be changed. Feel free to correct my answer too. Sep 27 at 15:27
  • Anyone who knows how to add a dpkg trigger, please edit my answer to add it! Sep 27 at 16:39
1

in order to get AltGr+h, j,k,l or any other key without any application like Autokey to peform you can customize your keyboard layout found in /usr/share/X11/xkb/symbols/. At the moment I use the german layout "de". So first of all I would:

1- copy the standard layout

cp /usr/share/X11/xkb/symbols/de /usr/share/X11/xkb/symbols/de.bak

2- open your layout with your text editor of preference (here: gedit)

sudo gedit /usr/share/X11/xkb/symbols/de 

there you gonna see something like:

key <AD03>  { [         e,          E,     EuroSign,     EuroSign ] };
key <AD06>  { [         z,          Z,    leftarrow,          yen ] };
key <AD11>  { [udiaeresis, Udiaeresis, dead_diaeresis, dead_abovering ] };
....
....
key <AD12>  { [      plus,   asterisk,   dead_tilde,  macron ]  };
key <AC02>  { [               s,               S,          ssharp,          U017F ] }; 
key <AC06>  { [               h,               H,            Left,             Left ]   }; 
key <AC07>  { [               j,               J,            Down,             Down ]   }; 
key <AC08>  { [               k,               K,              Up,               Up ]   }; 
key <AC09>  { [               l,               L,           Right,            Right ]   }; 
....
....
key <AB10>  { [     minus, underscore,               endash,     emdash ] };
key <LSGT>  { [     less,     greater,                  bar,     NoSymbol   ] };

As you can see, AB, AC, AD, AE represents the row, and the number represents its position on the keyboard (there are some special keys, like the less/greater key, which can be remapped to other keys as well..

There you will not find all the keyboard, just the ones that is diferent that the layout the keyboard is based on. The german one is based on "latin(type4)" as you can see on the beggining of the file:

include "latin(type4)"

Then you just need to change according to your needs. It works like:

key { [ key, key+shift, key+AltGr, key+Shift+AltGr ] };

(its also possible the add 5th level modifiers, or more)

if you want to check the changes without restart; (changing "de" for your layout... here is a list with possible layouts )

 setxkbmap -layout de

The arrows are labelled just like "Left, Right, Down, Up", as expeceted. Here is a good list of possible values.

here is a example of my custom layout. (but not in use at the moment)

Then, you would need to remap Capslock and AltGr, or did it work already following the question you posted??

If you also want to keep a "normal" german layout, you can do the changes to another layout you dont normally use, (or change the "german no dead keys" which you can find further down at the same "de" file...)

Source: link to a very comprehensive explanation of xbk and custom layouts.
link to a similar question with a good answer.

1
  • 1
    in modern Ubuntu and derivatives, you can set XKB layouts and options in /etc/default/keyboard once these changes are made. see man 5 keyboard for details. i recommend adding new variants & options rather than changing existing ones.
    – quixotic
    Mar 28, 2017 at 22:37
0

To remap CapsLock to AltGr use

xmodmap -e "keycode 66 = ISO_Level3_Shift"

(source: https://superuser.com/a/138757/160420)

To map the arrow keys to AltGr + h,j,k and l, use xmodmap -pke to find the right settings:

xmodmap -pke|egrep "f F|j J|k K|h H|l L|o O|u U|d D|dollar|BackSpace"

and change the fifth value to the new keys and add this all in a bash script:

# xrandr needs the desktop to be fully loaded. add a delay, to be able to add it to Startup Applications:
sleep 15
# change BackSpace into AltGr
xmodmap -e "keycode 66 = ISO_Level3_Shift"
# set BackSpace on AltGr + F
xmodmap -e "keycode 41 = f F f F BackSpace BackSpace dstroke ordfeminine f F dstroke ordfeminine"
# create arrow keys at h,j,k,l
xmodmap -e "keycode  43 = h H h H Left Left"
xmodmap -e "keycode  44 = j J j J Down Down"
xmodmap -e "keycode  45 = k K k K Up Up"
xmodmap -e "keycode  46 = l L l L Right Right"
xmodmap -e "keycode  30 = u U u U Prior Prior"
xmodmap -e "keycode  40 = d D d D Next Next"
xmodmap -e "keycode  12 = 3 section 3 numbersign Home Home"
xmodmap -e "keycode  13 = 4 dollar 4 dollar End End"
xmodmap -e "keycode  53 = x X x X Delete Delete"

(source: https://askubuntu.com/a/466315/34298)

Put that bash script in your Startup Applications (Choose Dash > Startup Applications > Add, and add the command.)

Note: strangely in gnome-terminal this works fine, but in yakuake it works for BackSpace on AltGr+F but it sets AltGr+h,j,k and l to D,B,A and C unless you set the Key Binding to "Linux" instead of "(Default) XFree 4", and in sublime-text it doesn't work at all

4
  • 1
    xmodmap is deprecated and discouraged. it still works in X11 environments but will not work in Wayland and other X11 replacements. using XKB-based solutions instead is highly encouraged.
    – quixotic
    Mar 28, 2017 at 22:35
  • So I guess, I have to add a new custom keyboard layout then? askubuntu.com/questions/482678/…
    – rubo77
    Mar 28, 2017 at 22:55
  • 1
    adding a custom layout is one way, though you can probably get away with just making a custom option. in GNOME or Unity you may need to disable settings-daemon's keyboard plugin for your xmodmap customization to work, but if you make a new system-wide XKB layout/variant/option you can set them in /etc/default/keyboard.
    – quixotic
    Mar 28, 2017 at 23:48
  • 1
    This seems quite a lot of work, but will it solve the problem, that the new key combinations dont work in some apps?
    – rubo77
    Mar 29, 2017 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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