Ubuntu – Terminal change keys

terminalUbuntu

Is it possible in the Unix terminal to change some of the keyboard keys?

For example: when I press , then _ gets printed?

If this is indeed possible, how do I go about doing that?

I'm on Ubuntu.

Best Answer

To quote from here:

Custom table

You can create your own map and store it in your home directory (i.e. ~/.Xmodmap). Print the current keymap table into a configuration file:

xmodmap -pke > ~/.Xmodmap

Make the desired changes to ~/.Xmodmap and then test the new configuration with:

xmodmap ~/.Xmodmap

To activate your custom table when starting Xorg add the following:

~/.xinitrc
if [ -f $HOME/.Xmodmap ]; then
    /usr/bin/xmodmap $HOME/.Xmodmap
fi

Alternatively, edit the global startup script /etc/X11/xinit/xinitrc.

Test changes

You can also make temporary changes for the current session. For example:

xmodmap -e "keycode  46 = l L l L lstroke Lstroke lstroke"
xmodmap -e "keysym a = e E"

Also:

See man xmodmap for more details.

EDIT:

To clarify: the xmodmap ~/.Xmodmap may be used in many places, not just when starting Xorg. For example, I have these two functions in my ~/.bashrc:

   # map caps to esc

   mapcaps(){
      xmodmap -e "clear lock"
      xmodmap -e "keycode 0x42 = Escape"
      echo mapcaps: caps-lock set to Escape via xmodmap
   }
   unmapcaps(){
      xmodmap -e "keycode 0x42 = Caps_Lock"
      xmodmap -e "add lock = Caps_Lock"
      echo unmapcaps: caps-lock set to caps-lock via xmodmap
   }

This is so I can dynamically map Caps to Esc.

Really there is no limitation. Feel free to call xmodmap from ~/.xinitrc, ~/.bash_profile, from a custom script, etc.

Related Question