Thursday, March 5, 2020

Swapping CapsLock to Esc key in openSUSE 15.+

Being a vim user I have adopted my keyboard to swap the caps key from the esc key. In openSUSE the file /etc/vconsole.conf needs to be edited in order to make the changes permanent.

1.  First thing we need to do is create a new directory somewhere in /usr

  mkdir -p /usr/local/share/kbd/keymaps/ 

2.  Next change directory to the newly created directory.

  cd /usr/local/share/kbd/keymaps/  

3.  Next copy the keyboard layout/setup that you're using from /usr/share/kbd/keymaps/xkbd.

  cp -v /usr/share/kbd/keymaps/xkb/us.map.gz /usr/local/share/kbd/keymaps/  

5.   ls 

You should see the output.

  us.map.gz  

6. unzip it.

  gunzip us.map.gz  

7. Check the default value of the keycodes for Escape and Caps_Lock.

  grep -noE '^keycode[[:space:]]*(1|58)[[:space:]]*=[[:space:]][^ ]*' us.map  

   The output should be like this.

  2:keycode 1 = Escape  
  59:keycode 58 = CtrlL_Lock 


8. Change value of key code 1 to 58 and 58 to 1.

printf '%s\n' '/^\(keycode[[:space:]]*\)1\([[:space:]]*=.*$\)/s//\158\2/' \
'/^\(keycode[[:space:]]*\)58\([[:space:]]*=.*$\)/s//\11\2/' ,p Q | ed -s us.map \ 
| grep -noE '^keycode[[:space:]]*(1|58)[[:space:]]*=[[:space:]][^ ]*'

Should print the output below, since that ed code does not edit the file in-place but just prints the output to stdout.

  2:keycode 58 = Escape  
  59:keycode 1 = CtrlL_Lock  


To actually edit the file you have two options using ed.

Using printf with ed.

 printf '%s\n' '/^\(keycode[[:space:]]*\)1\([[:space:]]*=.*$\)/s//\158\2/' \
'/^\(keycode[[:space:]]*\)58\([[:space:]]*=.*$\)/s//\11\2/' w | ed -s us.map

Using a  heredoc with ed.

ed -s us.map << 'EOF'  
/^\(keycode[[:space:]]*\)1\([[:space:]]*=.*$\)/s//\158\2/
/^\(keycode[[:space:]]*\)58\([[:space:]]*=.*$\)/s//\11\2/
w
q
EOF

9. Check the new value the keycodes Escape and Caps_Lock

   grep -noE '^keycode[[:space:]]*(1|58)[[:space:]]*=[[:space:]][^ ]*' us.map  

 The output should be like this.

  2:keycode 58 = Escape  
  59:keycode 1 = CtrlL_Lock 


10. Save it to /usr/local/share/kbd/keymaps/personal.map

  mv -v us.map personal.map  

11. To load during the session

  loadkeys /usr/local/share/kbd/keymaps/personal.map 

12. To make permanent In order to load the keymap at boot, specify the full path to the file in the KEYMAP variable in /etc/vconsole.conf. The file does not have to be gzipped as the official keymaps provided by kbd.

   Check the value of /etc/vconsole.conf

  cat /etc/vconsole.conf 

You should see what is the default setup in that config file.

13 .Back up first vconsole.conf before editing that file.

  cp -v /etc/vconsole.conf /etc/vconsole.conf.original  

14. Replace the value of KEYMAP to the absolute path of the new key mappings.

  printf '%s\n' '/^\(KEYMAP\).*/s//\1=\/usr\/local\/share\/kbd\/keymaps\/personal.map/' w | ed -s /etc/vconsole.conf  

15. Check the value of the newly edited /etc/vconsole.conf file.

  cat /etc/vconsole.conf