Toggle keyboard keys in Linux using terminal commands


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.

if synclient -l | grep "TouchpadOff.*=.*0" ; then
 synclient TouchpadOff=1 ;
 synclient TouchpadOff=0 ;

If you save it to say ~/bin/, 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/

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.

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"
 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"

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.



Filed under Uncategorized

2 responses to “Toggle keyboard keys in Linux using terminal commands

  1. This one-liner is what I use for the touchpad:

    synclient TouchpadOff=$(synclient -l | grep -c ‘TouchpadOff.*=.*0’)

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s