How to Replace the Esc Key with WinKey / Fn / Ctrl / Tab on Ubuntu

How to Replace the Esc Key with WinKey / Fn / Ctrl / Tab on Ubuntu

On Ubuntu, you can use xmodmap to remap keys, mapping rarely used keys to Esc. This is especially useful for Vim users: Esc is rather far away, while WinKey, Fn, right Ctrl, or Tab are often easier to reach.

First Confirm the keycode

First, you need to know the keycode for each key. You can use xev on a computer with the same keyboard layout to check:

xev

After running it, press the target key and look in the terminal output for something like this:

keycode 9 (keysym 0xff1b, Escape)

In my case, I found:

  • The keycode for Esc is 9, and the keysym is Escape.
  • The keycode for WinKey is 115, usually corresponding to Super_L.
  • The keycode for right Ctrl is 105, corresponding to Control_R.
  • On some keyboards, the keycode for the Fn / WakeUp key is 151, corresponding to XF86WakeUp.
  • The keycode for Tab is usually 23, corresponding to Tab.

Different keyboards and different system versions may not be exactly the same, so use the actual output from xev as the source of truth.

Write to .Xmodmap

Create or edit the .Xmodmap file in your home directory:

nano ~/.Xmodmap

Add the mappings you need, save the file, and log in again. You can also manually load it first for testing:

xmodmap ~/.Xmodmap

Replace Esc with WinKey

If you want WinKey to become Esc, and the original Esc to become WinKey, write:

keycode 9 = Super_L
keycode 115 = Escape

If you only want WinKey to become Esc without changing the original Esc, keep only the second line:

keycode 115 = Escape

Replace Esc with the Fn / WakeUp Key

Some keyboards' Fn keys are not directly recognized by the system. If xev can see it outputting XF86WakeUp or a similar key value, you can set it like this:

keycode 9 = XF86WakeUp
keycode 151 = Escape

If you only want to remap that key to Esc, write only:

keycode 151 = Escape

Replace Esc with Right Ctrl

Right Ctrl belongs to the Control modifier, so before and after remapping it, it is best to remove it from the Control group first, then add back the key you need to keep. To remap right Ctrl to Esc, write:

remove Control = Control_R
keycode 105 = Escape

If you also want to remap the original Esc to right Ctrl, add:

keycode 9 = Control_R
add Control = Control_R

The complete version is:

remove Control = Control_R
keycode 9 = Control_R
keycode 105 = Escape
add Control = Control_R

Replace Esc with Tab

If you want to remap Tab to Esc:

keycode 23 = Escape

If you want to swap Tab and Esc:

keycode 9 = Tab
keycode 23 = Escape

Restore the Default Settings

If you make a mistake, you can temporarily clear the current user's custom mapping:

mv ~/.Xmodmap ~/.Xmodmap.bak

Then log in again, or restart the X session. Before modifying it again, it is best to confirm the keycodes with xev once more.

Leave a Reply