It is easier to use GUI tools to setup keyboard shortcuts, but sometimes you need to map certain keys for typical purpose (e.g swapping caps and esc when using vim). In this tutorial I will try to list different linux tools and scripts that you can use to do these tasks (you might have to install them from your distribution’s repository).
1. Toggle Touchpad
If you are using a keyboard centric environment like i3 or awesome, you might want to disable the touchpad once in a while. You can use following script to do so.
#!/bin/bash if synclient -l | grep "TouchpadOff.*=.*0" ; then synclient TouchpadOff=1 ; else synclient TouchpadOff=0 ; fi
If you save it to say ~/bin/toggletouchpad.sh, you can add a shortcut to it from your desktop environment. In case of i3 you can add a shortcut like this in ~/.i3/config file
bindsym $mod+Control+p exec ~/bin/toggletouchpad.sh
2. Toggle keyboard language
If English is not your first language or you are learning foreign language, you can create a shortcut to toggle between two languages in your keyboard using following code. To know what code and keyboard options to use for different languages see /usr/share/X11/xkb/rules/base.lst and add the respective language package from your package manager (if it already does not exist). Fore example if I wanted to toggle between Japanese and English, I would use following code.
setxkbmap -option grp:alt_caps_toggle "us,jp"
After this all you have to do is add the respective script file as startup while login into your working environment.
3. Toggle Caps Lock with Esc
If you are a heavy vim user you will hear many people suggesting swapping out Caps Lock and Esc key. I didn’t want to permanently swap out the keys, therefore I created following script to toggle it. Therefore, I can swap these keys when I am using vim and swap back to original once I exit vim. I could use VimEnter and VimLeave to call this file, but I like to set it up manually. Here is the script.
#!/bin/bash if xmodmap -pke | egrep -i "escape" | egrep -i "keycode.*9.*"; then echo "Changing from original to mapped" xmodmap -e "clear lock" xmodmap -e "keycode 66 = Escape" xmodmap -e "keycode 9 = Caps_Lock" xmodmap -e "add Lock = Caps_Lock" else echo "Changing back from mapped to original" xmodmap -e "clear lock" xmodmap -e "keycode 9 = Escape" xmodmap -e "keycode 66 = Caps_Lock" xmodmap -e "add Lock = Caps_Lock" fi
In your keyboard Esc and Caps Lock might be mapped to different keyboard code, use “xmodmap -pke|egrep -i “escape|caps”” to find out. If you want to permanently (for your environment) change it, you can use the code inside the if block and add it at startup.
In this tutorial, I mentioned different commands and code that you can use to play with keyboard shortcuts. Hope it helps.