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
keycodeforEscis9, and the keysym isEscape. - The
keycodefor WinKey is115, usually corresponding toSuper_L. - The
keycodefor right Ctrl is105, corresponding toControl_R. - On some keyboards, the
keycodefor the Fn / WakeUp key is151, corresponding toXF86WakeUp. - The
keycodefor Tab is usually23, corresponding toTab.
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.
