General workflow
For example, on a standard Cherry 104-key mechanical keyboard, the right Ctrl key is too faraway to reach. While the right Win key is relative easy to reach by the right little finger. Now we make the right Win act as a Ctrl key interactively:
-
Get the key name with
xev
. Press the right Win key in thexev
window, and from the output we know its name is Super_R; -
List current keymap with
xmodmap -pm
:xmodmap -pm xmodmap: up to 4 keys per modifier, (keycodes in parentheses): shift Shift_L (0x32), Shift_R (0x3e) lock Caps_Lock (0x9) control Control_L (0x25), Control_R (0x69) mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd) mod2 Num_Lock (0x4d) mod3 mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf) mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
So the current group ofSuper_R
ismod4
; -
Detach the right Win from current group:
xmodmap -e 'remove mod4 = Super_R'
; -
Run
xmodmap -pm
again and Super_R disappeared from groupmod4
; -
Add right Win to the
control
group:xmodmap -e 'add control = Super_R'
; -
Run
xmodmap -pm
again and the Super_R does be added to groupcontrol
; -
To verify, start a
vim
and press right Win and z, you can see the vim window is put into background, which means the map works; -
To persist this behavior, add
xmodmap -e 'remove mod4 = Super_R' xmodmap -e 'add control = Super_R'
into ~/.xinitrc (for i3sm) or ~/.profile for graphical DE.
Swap Ctrl and Win key
Add the following lines into ~/.xinitrc:
setxkbmap -option caps:swapescape -option ctrl:swap_lwin_lctl -option ctrl:swap_rwin_rctl
Swap Caps lock and Escape Key
Swap only once
Write the following text into file ~/.swapCapsESC:
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock
Run xmodmap ~/.swapCapsESC
.
Ref: http://vim.wikia.com/wiki/Map_caps_lock_to_escape_in_XWindows
Swap permanently on Ubuntu 14.04
Option 1
Add setxkbmap -option caps:swapescape
into ~/.profile
(verified on Ubuntu 14.04, Unity desktop, 2016.11.27),
or ~/.xinitrc (verified on Ubuntu 16.04, i3wm, 2018.11.26).
Ref: http://askubuntu.com/questions/363346/how-to-permanently-switch-caps-lock-and-esc.
Option 2
When a external keyboard is attach with USB, it may not swap these keys.
Now you need run source ~/.profile
again (with this keyboard).
Install dconf-editor with sudo apt-get install dconf-tools
,
start it with dconf-editor
.
In [org -> gnome >> desktop -> input-sources -> xkb-options],
modify its value to ['caps:swapescape']
.
Option 3
Add XKBOPTIONS="caps:swapescape"
into /etc/default/keyboard.
Ref: http://askubuntu.com/questions/444714/how-do-i-swap-escape-and-caps-lock-in-14-04
Option 4
Add "only once" solution to autostart:
Create file $HOME/.config/autostart/swapCapsESC.desktop
:
[Desktop Entry]
Name=SwapCapsESC
Comment=Swap Caps Lock Key and ESC Key
Exec=xmodmap /home/leo/.swapCapsEsc
Type=Application
Terminal=true
X-GNOME-Autostart-Delay=5
You can also use the GUI equivalent:
Run Startup Applications
(input startup
in dash (press Win key),
add above command in it.
Swap permanently on Windows
For Windows, install AutoHotkey, save the following codes as swapCapsESC.ahk:
Capslock::Esc
ESC::Capslock
Execute it when you want to swap the keys. If you want execute it automatically when system startup, put the file in [Start -> Startup] folder (open it with inputing "shell:startup" in Run). Verified in Windows 7 & 10.
Swap Ctrl and Alt Key
remove mod1 = Alt_L Alt_R
remove control = Control_L Control_R
keycode 64 = Control_L
keycode 37 = Alt_L
keycode 108 = Control_R
keycode 105 = Alt_R
add control = Control_L Control_R
add mod1 = Alt_L Alt_R
64, 37, etc are determined by "xev" command, see below for usage.
Reset Key Map to Initial State
setxkbmap -layout us
Define key swap shortcuts
It's mainly for emacs operation convenience to swap Ctrl and Alt key, and vi operation convenience to swap Caps Lock and Escape key. When you are not in emacs and vi, swap keys always lead confusions. So it's necessary to switch key definition quickly. The following are some basic method to achieve key swap shortcuts
-
Create a file named "swapCapsEsc" in /home/lichao/docs/swapkeys
remove Lock = Caps_Lock keysym Escape = Caps_Lock keysym Caps_Lock = Escape add Lock = Caps_Lock
and a file named "swapCtrlAlt" in /home/lichao/docs/swapkeys:
remove mod1 = Alt_L Alt_R
remove control = Control_L Control_R
keycode 64 = Control_L
keycode 37 = Alt_L
keycode 108 = Control_R
keycode 105 = Alt_R
add control = Control_L Control_R
add mod1 = Alt_L Alt_R
-
Create keyboard shortcuts in "Application Finder (Alt + F3) -> Preferences -> Custom Actions":
Type: Regular Expression; Pattern: swce; Command: xmodmap /home/lichao/docs/swapkeys/swapCapsEsc Type: Regular Expression; Pattern: swca; Command: xmodmap /home/lichao/docs/swapkeys/swapCtrlAlt Type: Regular Expression; Pattern: swres; Command: setxkbmap -layout us
Now you can use [Alt-F2 swce] to swap Caps lock and escape key, use [Alt-F2 swca] to swap Ctrl and Alt key, use [Alt-F2 swres] to make all keys to their original state (If you have swaped ctrl and alt key, the shortcut is [Ctrl-F2 swres]).
Usage of xev
Run "xev" in command line. A little window appear. Make it active window and press some key, the keycode will show in the window.
Usage of xmodmap
Run "xmodmap -pm" we got:
shift Shift_L (0x32), Shift_R (0x3e) lock Caps_Lock (0x42) control Control_L (0x25), Control_R (0x69) mod1 Alt_L (0x40), Alt_R (0x6c), Meta_L (0xcd) mod2 Num_Lock (0x4d) mod3 mod4 Super_L (0x85), Super_R (0x86), Super_L (0xce), Hyper_L (0xcf) mod5 ISO_Level3_Shift (0x5c), Mode_switch (0xcb)
Here the names in the left column (shift, lock, control, etc) are called "Modifier Name",
while the names in the right column (Shift_L, Caps_Lock, Control_R, Meta_L, etc) are called keysym.
Shift_L means left shift key, while Shift_R means right shift key.
Every physical key (on keyboard) has a identity (integer number) called keycode.
You can use "xmodmap -pk" to investigate these relationships.
And use xmodmap -e "keycode <to_be_modified> = <target_key>"
to modify these relationships.
For example, the following command make F2 key print "m" in gedit panel:
xmodmap -e "keycode 68 = m":
Before command:
58 0x006d (m) 68 0xffbf (F2)
(get by "xmodmap -pk"). After command:
58 0x006d (m) 68 0x006d (m)
Use xmodmap -e "keycode 68 = F2"
to modify F2 back to F2 key.
The right hand side of this assignment is keysym name. So
xmodmap -e "keycode 16 = 7"
will map character "7" to physical key which has keycode 16.
"clear xmodmap -e "clear control"
, all control keys (left and right) are disabled.
"remove xmodmap -e "remove Control = Control_R"
makes right control key be disabled.
When you run xmodmap -e "add Control = Control_R"
, the right control key is enabled again.
You can use "xmodmap -pm" to verify.
"keysym name1=name2" means binding the keys (previous binded to name2) to name1;
Auto load xmodmap files when system start
xmodmap ~/.Xmodmap
Ref: http://earthviaradio.wordpress.com/2012/02/06/swapping-the-left-alt-and-ctrl-keys-in-ubuntu-11-10/