191

How can I permanently switch the Caps Lock and Esc key functions in Saucy? I can use this command to do it temporarily (until reboot):

/usr/bin/setxkbmap -option "caps:swapescape"

I tried adding that command as a Startup Application, but it doesn't seem to do anything.

zx485
  • 2,865

19 Answers19

171

Another way to do this is through the dconf-editor. This method has a few extra steps from gnome-tweak-tool, but is useful if you don't want to pull in the dependencies from the tweak tool.

This will allow you to use the caps:swapescape syntax and automatically make the change permanent.

sudo apt-get install dconf-tools

On Ubuntu >20.04, you'll need to run the following - see this post:

sudo apt install dconf-cli dconf-editor

After starting the dconf-editor, navigate to org >> gnome >> desktop >> input-sources

Add the options that you need in xkb-options. The option strings are surrounded by single quotes and separated by commas. Be careful not to delete the brackets on the ends.

xkb-options in dconf-editor

You can use this method to enter most of the traditional xkb options that are no longer available in System Settings >> Text Entry. The exception are the settings for switching the keyboard layouts, which currently do not work because of a bug.

For a list of the options and the syntax, use man 7 xkeyboard-config in a terminal.

Another common option that could be used is terminate:ctrl_alt_bksp to allow ctrl+alt+backspace to end the X-session.

chaskes
  • 15,536
131

A solution that should work for most linux distros:

setxkbmap -option caps:swapescape

Other options are possible:

  • caps:none to deactivate
  • caps:escape to make it an additional escape
  • caps:super to make it an additional super (windows) key.

To make this work at startup, you can put it in ~/.profile as this will only run after the interactive login.

If the switch does not persist between logins (e.g. when suspending your machine) you can also create a .xinitrc file in your home directory and put it there. Linux mint 20 on a laptop works impeccable with this option.


If above does not persist use /etc/X11/xorg.conf.d/00-keyboard.conf (see man xorg.conf DESCRIPTION for all available paths). It should work with Bluetooth keyboards that reconnect after sleep.

Section "InputClass"
        MatchIsKeyboard "on"
        Option "XkbOptions" "caps:escape"
EndSection

See man xkeyboard-config

j-i-l
  • 1,455
60

Okay, found a way to do this using gnome-tweak-tool.

From a terminal, run

sudo apt-get install gnome-tweak-tool -y && gnome-tweak-tool

You can find an option to swap Caps Lock and Esc in "Typing -> Caps Lock key behavior".

Update: In Ubuntu 18.04 and 20.04 it can be found in Keyboard & Mouse > Additional Layout Options > Caps Lock Behavior (Thanks to Adracus's comment below)

23

You can use xmodmap in terminal to swap Caps Lock with Esc:

xmodmap -e "keycode 9 = Caps_Lock NoSymbol Caps_Lock"   #this will make Esc to act as Caps Lock
xmodmap -e "keycode 66 = Escape NoSymbol Escape"        #this will make Caps Lock to act as Esc

To get this change for every session, after you have run the ​​previous commands create a file called .xmodmap with the new keymaps, using the following command:

xmodmap -pke > ~/.xmodmap

Then, create a file called .xinitrc in your home directory, containing the following line/command:

xmodmap .xmodmap
Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
21

Similar to @Radu's answer, but compatible with 14.04 (see also this answer).

xmodmap -e "remove Lock = Caps_Lock"
xmodmap -e "keycode 9 = Caps_Lock NoSymbol Caps_Lock"
xmodmap -e "keycode 66 = Escape NoSymbol Escape"
xmodmap -pke > ~/.xmodmap

If it does not work, replace:

xmodmap -e "remove Lock = Caps_Lock"

with:

xmodmap -e "clear Lock"
Gilly
  • 311
17

Go to the gears icon at the top right corner of the screen and do the following:

  • Click System Settings → Keyboard layoutOptions... (lower right hand corner) → Caps lock key behaviour (4th down).

  • Then scroll down and select Swap ESC and Caps Lock.

Done!

Joe
  • 189
14

The Keyboard Layout with Switch Escape and Capslock answer above is no longer valid on Ubuntu 16.04 and later (was it before? dunno).

In order to avoid the hustle with permissions etc., just switch to root before you start doing the following steps by running su in a terminal and entering your root password. Then:

  1. Run:

    cd /etc/default/
    vim keyboard
    
  2. There you will see the line:

    XKBOPTIONS=""
    

    Change it to:

    XKBOPTIONS="caps:swapescape"
    
  3. Exit Vim with save:

    :wqa
    
  4. Reboot.

And that will be permanent, I promise. :)

13

Run this command once in a terminal. dconf should already be installed with gnome 3.

dconf write "/org/gnome/desktop/input-sources/xkb-options" "['caps:swapescape']"
12

For Ubuntu 18.04 and Gnome 3.30 this works for me:

gsettings set org.gnome.desktop.input-sources xkb-options "['caps:swapescape']"
modesto
  • 229
6

I've built a tool in C specially for this purpose that overcome many of the issues with the xcape/xmodmap solution:

It does a bit more since it also turn CAPSLOCK as both ESC and CTRL.

oblitum
  • 1,727
3

I'm late to answer, but the usual way to swap Esc and Ctrl is with the following lines in the ~/.Xmodmap file:

clear Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock

This should work with all desktop environments (KDE, Gnome) on most common distros (Ubuntu and Debian family, Fedora and RH family).

dotancohen
  • 2,864
2

This can be done from the "Keyboard Layout" system setting. Click on "options" for the layout you are using, then under "Caps Lock key behavior" choose "switch Escape and Capslock".

1

Assuming you're using the us layout with the default model and variant, you can use this command:
sudo localectl --no-convert set-x11-keymap us "" "" "caps:swapescape"

Unlike ~/.Xmodmap, which applies after login, this command changes the default system layout and applies to the login screen, which is useful if you use an alternative layout like dvorak.

For more info/examples of the command see this arch wiki article: https://wiki.archlinux.org/title/Xorg/Keyboard_configuration#Using_localectl

1

I anyone is looking for the KUbuntu equivalent it's under:

System Setting -> Input Devices -> Keyboard -> Advanced Tab -> Caps Lock behavior -> Make Caps Lock an additional Esc

enter image description here

PeterT
  • 231
  • 1
  • 3
1

Another approach is to change the mapping from scancodes to keycodes with udev. I needed this more low-level change to get my capslock-escape working in VS Code. This answer is based on https://wiki.archlinux.org/title/Map_scancodes_to_keycodes

Gathering keyboard information

First, identify the keyboard device. I've used cat /proc/bus/input/devices which outputs:

I: Bus=0011 Vendor=0001 Product=0001 Version=ab83
N: Name="AT Translated Set 2 keyboard"
P: Phys=isa0060/serio0/input0
S: Sysfs=/devices/platform/i8042/serio0/input/input3
U: Uniq=
H: Handlers=sysrq kbd event3 leds 
B: PROP=0
B: EV=120013
B: KEY=402000000 3803078f800d001 feffffdfffefffff fbfffffffffffffe
B: MSC=10
B: LED=7

Note the eventX here. Then we can get the modalias of the device with cat /sys/class/input/event3/device/modalias:

input:b0011v0001p0001eAB83-e0,1,4,11,14,k71,72,73,74,75,76,77,79,7A,7B,7C,7D,7E,7F,80,8C,8E,8F,9B,9C,9D,9E,9F,A3,A4,A5,A6,AC,AD,B7,B8,B9,D9,E2,ram4,l0,1,2,sfw

We will later use the input:b0011v0001p0001eAB83 identifier.

Now, we want to find out which scancode we want to map to which keycode. My example will map the capslock to an escape (scancode may depend on the keyboard hardware). Run evtest /dev/input/event3 and then press the key (eg capslock):

Event: time 1732003836.156077, type 4 (EV_MSC), code 4 (MSC_SCAN), value 3a
Event: time 1732003836.156077, type 1 (EV_KEY), code 58 (KEY_CAPSLOCK), value 0
Event: time 1732003836.156077, -------------- SYN_REPORT ------------

We note the MSC_SCAN value, in this case the scancode is 3a.

In my example, our target keycode is esc, here is a list of other keycodes.

Update scancode to keycode mapping

There is a default mapping file at /usr/lib/udev/hwdb.d/60-keyboard.hwdb. Create a new file with a higher number, eg /usr/lib/udev/hwdb.d/70-keyboard-custom.hwdb:

# evdev:<modalias identifier>*
#   KEYBOARD_KEY_<scancode>=<target keycode>

evdev:input:b0011v0001p0001eAB83* KEYBOARD_KEY_3a=esc

After saving the file run systemd-hwdb update and udevadm trigger to recompile and reload the hardware database index.

Now, when the pressed capslock yields the 3a scancode, udev will translate it to an esc keycode. Essentially, we have two escape keys now. To achieve the reverse mapping (escape to capslock), follow the same steps to identify the escape scancode and map it to the capslock keycode.

A_A
  • 131
0

Note: This does not swap, only maps caps lock to esc. (This was good enough for my vim usecase.)

For ubuntu 16:04 (with i3 environment): created file ~/.xessionrc with content

xmodmap -e "clear lock" #disable caps lock switch
xmodmap -e "keysym Caps_Lock = Escape" #set caps_lock as escape
0

For Ubuntu 18.04:

Create file ~/.xmodmaprc

xmodmap -e "clear lock"
xmodmap -e "keycode 9 = Caps_Lock NoSymbol Caps_Lock"
xmodmap -e "keycode 66 = Escape NoSymbol Escape"

Source this file to bind the keys in your current session:

$ . "$HOME/.xmodmaprc"

Append this command to ~/.bashrc for the keys to be bound every time you log in.

This answer is similar to Gilly's answer but with a lowercase "l" in "clear lock".

0

On KDE GUI (graphical user interface), This can be done from Keyboard Layout on System Setting. Click on "Input Devices". On the top left corner, click Keyboard. Then click "Advanced" tab.and under "Caps Lock key behavior" choose "switch Escape and Capslock".

Makyen
  • 105
-1

In Linux Mint 19.3 from the Menu: Preferences Keyboard Layouts Options Caps lock behaviour Swap ESC and Caps Lock