Before making another key act as Esc, decide which layer should change: one application, the current GNOME user, an X11 session, one input device, the Linux virtual console, or the whole machine. Broader scope raises the risk to password entry, screen locking, accessibility navigation, and recovery. The safest default is to keep the physical Esc, test one additional Esc in an ordinary user session, and make nothing persistent until rollback works.
Table of Contents
1. Choose the narrowest scope first
| Need | Preferred layer | Do not start with |
|---|---|---|
| Leave Insert mode more easily only in Vim | Built-in application key or application setting | Changing the whole machine keyboard |
| Caps Lock as an additional Esc | GNOME per-user standard XKB option | Swapping Esc/Caps or replacing existing XKB options |
| Right Ctrl, Super, or Tab on one keyboard as Esc | One-device, one-preset input-remapper test | Changing every keyboard and enabling autoload at once |
| Support an old X11 application only | X11/XKB session configuration | Assuming xmodmap controls native Wayland applications |
| Linux text console or login screen | Administrator-managed console/systemd configuration | A global change without a second input path |
| Fn emits no independent event | Keyboard firmware/vendor setting | Guessing a keycode or calling the combination event Fn |
Do not remap Caps, Super, Ctrl, and Tab together. Test one candidate at a time, and retain the other Ctrl, Tab, Super, physical Esc, or a second keyboard as a recovery path.
2. These keys already have important duties
- Caps Lock: Often the lowest-risk candidate, but Orca's laptop layout can use Caps Lock as the Orca modifier by default. Check first if you need Caps Lock or a screen reader.
- Super/Win: GNOME uses it to open Activities;
Super+Spacecommonly changes input sources. A remap changes desktop navigation. - Ctrl: Terminals, editors, browsers, and accessibility functions rely heavily on it. If testing is essential, consider only right Ctrl and retain left Ctrl.
- Tab: GNOME accessibility documentation uses Tab and Ctrl+Tab for keyboard focus movement. Do not take Tab from users who depend on keyboard navigation, forms, or screen readers.
- Fn: Most laptops handle Fn in the keyboard controller or firmware. Linux may see only the resulting brightness, volume, or function-key event—or no separate Fn event at all.
Characters are usually hidden in password fields. A wrongly scoped remap may not become apparent until the lock screen, sudo, disk unlock, or a remote console. Do not enter a real password while testing.
3. Establish a recovery path before changing anything
Before any persistent operation:
- Keep physical
Esc; do not begin with a swap. - Connect and test a second keyboard, or confirm that an on-screen keyboard, controlled remote session, or TTY can log in. A remote path must not depend on the same input stack being changed.
- Close tools capturing raw keys before entering a password.
- Record the session type, input sources, XKB options, console keymap, assistive technology, and input-remapper autoload state.
- Make a read-only backup of user configuration. System-wide work additionally needs an administrator, a maintenance window, and console rollback.
Do not treat “logging out will restore it” as a guarantee: GNOME gsettings, input-remapper autoload, console keymaps, and hwdb can persist across login or reboot.
4. Inventory Wayland, X11, and configuration sources read-only
Run these commands first. Some will fail outside GNOME or X11, or when the relevant package is absent. Record the failure; do not interpret empty output as a default.
echo "$XDG_SESSION_TYPE"
echo "$XDG_CURRENT_DESKTOP"
loginctl show-session "$XDG_SESSION_ID" -p Type -p Remote
gsettings writable org.gnome.desktop.input-sources xkb-options
gsettings get org.gnome.desktop.input-sources xkb-options
localectl status
localectl list-x11-keymap-options | grep -E '^caps:(escape|swapescape|escape_shifted_capslock)$'
command -v xev setxkbmap xmodmap libinput input-remapper-control
setxkbmap -query
xmodmap -pm
systemctl status input-remapper --no-pager
When $XDG_SESSION_TYPE=wayland, setxkbmap, xmodmap, and xev reflect at most the Xwayland boundary; they do not prove that native Wayland applications use the same map. The desktop session applies GNOME's per-user xkb-options; test native Wayland, Xwayland, and the lock screen separately.
localectl reports system console/X11 defaults, not necessarily the current GNOME user's final composition. Do not overwrite machine settings with localectl set-keymap or set-x11-keymap to solve a personal Vim preference.
5. First prove that the system sees the target key
Userspace can remap only events such as EV_KEY that reach the Linux input subsystem. List devices first, then press only the target key and Esc in a dedicated test window:
libinput list-devices
grep -E 'Name=|Handlers=|EV=' /proc/bus/input/devices
An X11 session can observe a small test window with:
xev -event keyboard
For Wayland or lower-level diagnosis, the libinput documentation describes libinput debug-events --show-keycodes; it generally needs access to protected input devices. It sees raw keys: pause chat, password-manager, and login activity, press only test keys, exit immediately, and do not share a full log containing private input. Do not use grab mode on the only keyboard.
A keycode is not a permanent identifier across keyboards, protocols, sessions, or tools. The original 2014 values 9, 115, and 151 describe one historical X11 environment and are not modern configuration constants.
6. The hardware and firmware boundary of Fn
If pressing Fn alone produces no independent event in the kernel, libinput, or input-remapper, XKB and userspace have no “Fn key” to remap. Some devices turn Fn plus another key into KEY_BRIGHTNESSUP, KEY_VOLUMEUP, XF86WakeUp, or a normal F key. That is the combination's result, not an independent Fn event.
Check the keyboard or laptop vendor's firmware setting, Fn Lock, or official utility. Do not guess that XF86WakeUp is Fn, and do not alter ACPI, a kernel driver, or hwdb to manufacture an unverified mapping. If the hardware exposes no event, software cannot capture it reliably.
7. Consider an application-local alternative first
Vim's official help lists Ctrl-[ as equivalent to Esc in Insert mode. It changes no keyboard map and does not affect passwords, the desktop, Tab navigation, or other applications, so try it first.
Other editors, terminal multiplexers, and IDEs may offer their own shortcut settings. Prefer the application's own interface and check conflicts, macros, plugins, and remote-application boundaries. Do not assume Vim's Ctrl-[ meaning applies to every program, and do not globally map an ordinary text sequence to Esc.
8. GNOME: safely make Caps Lock an additional Esc
Current xkeyboard-config provides caps:escape, meaning Caps Lock becomes an additional Esc while physical Esc remains. It is easier to recover from than caps:swapescape and lowers the chance of losing Esc.
This script proceeds only when the current XKB option list is empty, so it does not overwrite Compose, layout switching, or another existing option. It saves the original GVariant value, refuses to overwrite its backup, and reads the result back immediately.
CurrentOptions="$(gsettings get org.gnome.desktop.input-sources xkb-options)"
BackupPath="gnome-xkb-options-$(date +%Y%m%d-%H%M%S).txt"
umask 077
test ! -e "$BackupPath" || exit 1
echo "$CurrentOptions" | tee "$BackupPath"
case "$CurrentOptions" in
"@as []"|"[]") ;;
*) echo "Stop: merge existing XKB options deliberately"; exit 1 ;;
esac
gsettings set org.gnome.desktop.input-sources xkb-options "['caps:escape']"
gsettings get org.gnome.desktop.input-sources xkb-options
If options already exist, stop and merge through GNOME's keyboard settings or a reviewed GVariant list; do not paste a command that replaces the whole array. Test Caps and physical Esc in an ordinary editor, terminal, native Wayland application, and Xwayland application. Do not lock the screen immediately.
If Caps Lock behavior must remain available, first confirm that the local system lists caps:escape_shifted_capslock, then read its current xkeyboard-config description and evaluate it separately. Do not assume every distribution version offers the same option.
9. Restore GNOME XKB options
In the same shell, use the existing $BackupPath. In a new shell, replace the placeholder with the actual trusted backup path:
BackupPath="PASTE_THE_BACKUP_PATH"
test -r "$BackupPath" || exit 1
SavedOptions="$(cat "$BackupPath")"
gsettings set org.gnome.desktop.input-sources xkb-options "$SavedOptions"
gsettings get org.gnome.desktop.input-sources xkb-options
Confirm the read-back exactly matches the backup, then test Caps, Esc, Compose, input-source switching, and previous shortcuts. Do not substitute gsettings reset for restoring an unknown prior list: reset returns to the schema default, which need not be the user's previous value.
10. Super, right Ctrl, or Tab: test one device with input-remapper
Standard XKB options may not provide an arbitrary desired map. The input-remapper project says it supports X11 and Wayland through virtual input and stores maps by device and preset. It needs access to /dev/input and injects events, so its privileges and effect are broader than an ordinary application setting.
Install only a distribution package or an official project release, and read the Usage document matching the installed version. Recommended order:
- Open the GUI, select the specific keyboard, and create a new preset.
- Record only one candidate key. Choose
Escapeor the equivalent name offered by GUI autocomplete; do not type an old keycode. - Select Apply for temporary injection, but do not enable Autoload.
- Keep physical Esc. If testing right Ctrl, keep left Ctrl. Do not test Tab when keyboard navigation is required.
- Test press and release in ordinary text, desktop shortcuts, and required applications. Do not type a password or lock the screen.
- Use GUI Stop to end injection, verify the original key returns, and only then decide whether persistence is justified.
Inspect help, devices, available symbols, and service state read-only:
input-remapper-control --help
input-remapper-control --list-devices
input-remapper-control --symbol-names | grep -Ei 'esc|escape'
systemctl status input-remapper --no-pager
The project documentation warns that debug logs can contain private input and that a bad autoload can make a device unusable. On a stuck key or disabled device, use GUI Stop first; the project also documents unplugging and reconnecting a device twice to stop injection. Do not enable autoload without a second input path.
11. X11 xmodmap is a legacy, session-local method
xmodmap changes X11 modifier and key maps. It does not control native Wayland clients, and a mixed Xwayland session can therefore behave inconsistently across applications. Removing Ctrl, Super, or Caps from a modifier can also leave stuck or missing modifier state.
Do not copy historical keycodes. When maintaining legacy X11, save xmodmap -pke and xmodmap -pm, change only an isolated test session based on current xev evidence, and be ready to terminate that X session from another terminal. Prefer a standard XKB option on modern GNOME; prefer a stoppable input-remapper preset for arbitrary per-device mapping.
12. Console, login screen, and systemd hwdb are system-wide
The Linux virtual-console keymap is separate from a GNOME user session. localectl sets console/X11 system defaults and can affect TTY login and password entry. systemd's keyboard hwdb can rewrite KEYBOARD_KEY_ by device match and scan code before an event reaches the desktop, so it can cross Wayland, X11, and user boundaries—and can lock out the whole machine more easily.
These approaches are for administrator-managed fixed hardware only. Require an exact hardware match, original scan-code evidence, a configuration backup, a second keyboard, a physical or out-of-band console, a maintenance window, and staged rollout. Do not add a broad hwdb rule for every keyboard to satisfy a personal Esc preference, and do not edit vendor files in /usr/lib; libxkbcommon likewise warns that updates overwrite the system XKB root.
The display manager controls the login screen and system settings. A working post-login GNOME map does not prove password login is safe. Assign login-screen or kiosk mapping to an administrator and test it first on a non-production machine.
13. Verification matrix
| Check | Expected evidence | Action on failure |
|---|---|---|
| Original physical Esc | Still closes menus/dialogs and leaves Vim Insert mode | Roll back immediately; do not lock |
| Additional Esc | Candidate press and release produce one Esc, with no repeat or stuck state | Stop/restore XKB; inspect events |
| Original key duties | Planned paths for Super overview, left Ctrl, Tab navigation remain | Narrow the scope or choose Caps |
| Wayland and Xwayland | Both application classes match the plan | Do not layer an xmodmap patch |
| Accessibility | Orca modifier, Tab focus, and on-screen keyboard still work | Restore first; let the user evaluate |
| Input source/Compose | Switching and Compose options were not overwritten | Restore the complete array from backup |
| Screen lock/login | Tested only with a second input and rollback | Stop persistence without rollback |
| Reconnect/reboot | Only approved mapping persists | Disable autoload or restore configuration |
Change one variable at a time. Test press, hold, repeat, and release—not only whether one tap produces Esc.
14. Password, privacy, and accessibility boundaries
- Raw-event tools and input-remapper debug can record every key. Do not enter passwords, chats, or recovery codes while they run.
- Do not grant every user access to
/dev/input, and do not run an unknown script permanently as root. - Caps Lock may be an Orca modifier; Tab drives keyboard focus; Super and Ctrl carry desktop and application shortcuts.
- A per-user GNOME map is not a global guarantee for the console, display manager, containers, remote desktops, or every application.
- A remapped keycap no longer describes behavior. Label shared, public, or accessibility equipment and document rollback.
For an occupational-health or accessibility need, include the actual user in acceptance testing. Convenience must not remove reliable login, focus navigation, or assistive technology.
15. Stop and escalate when
- Fn has no independent kernel/input event, or tools disagree about what occurred.
- The only Esc, Ctrl, Tab, Super, or keyboard is about to be replaced.
- Existing XKB options, xmodmap, input-remapper autoload, hwdb, or vendor layers cannot be explained.
- Continuing requires a persistent root script, broad hwdb match, editing
/usr/shareor/usr/lib, or opening/dev/inputpermissions. - A screen reader, kiosk, full-disk unlock, production login, or remote maintenance depends on the device, without user acceptance and physical rollback.
- Testing creates duplicate events, missing releases, stuck modifiers, changed behavior after suspend, or inconsistent sessions.
Save the session type, device name, observed event, XKB options, tool versions, and a minimal reproduction for desktop/distribution, input-remapper, or hardware maintainers. Never publish raw private keystroke logs.
16. Official and project references
- libxkbcommon: XKB introduction and RMLVO options
- libxkbcommon: user configuration and search paths
- GNOME: keyboard and input sources
- GNOME: keyboard navigation with Tab, Super, and Esc
- GNOME Orca: Caps Lock modifier
- GNOME gsettings-desktop-schemas: input-sources schema
- systemd: official localectl source documentation
- systemd: official keyboard hwdb data and KEYBOARD_KEY
- Linux kernel: input event interface
- Linux kernel: event codes including EV_KEY
- Linux kernel: uinput virtual devices
- libinput: debugging tools
- input-remapper: project and installation boundaries
- input-remapper: Usage, Stop, autoload, and privacy warning
- Vim: Escape-equivalent command in Insert mode
References checked on 2026-09-01. Desktops, XKB data, firmware, and the input-remapper interface change; follow local version help and documentation for the corresponding installed version.
17. Historical 2014 source archive (provenance only)
The fence below preserves the complete visible source_export body byte for byte. No trailing whitespace was normalized and no private value was redacted. Its fixed X11 keycodes, WinKey keysym assumption, claim that rebooting applies the file, and equivalence between Fn and XF86WakeUp are inert historical evidence, not current configuration instructions.
~~~~markdown
Ubuntu下键盘按键替换
首先要知道键盘每个击键的Keycode, 这个可以使用xev在一台键盘layout相同的电脑上查到。
得知ESC的keycode是9,Symbol为”Escape” ,准备替换为WinKey(就是那个Windows logo的按键),WinKey 的 keycode是115。
具体的做法是在用户的家目录下建立.Xmodmap 文件,里面输入以下代码,重启即可:
keycode 9 =WinKey
keycode 115 = Escape
用Fn键替换:
keycode 9 = XF86WakeUp
keycode 151 = Escape
用右Control键替换:
remove Control = Control_R
keycode 9 = Control_R
keycode 105 = Escape
add Control = Control_R
~~~~
