184

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,426
  • 2
    I put that command in my ~/.bashrc so it persists between system restarts. (ctrl:nocaps for me, though, for tmux) – opyate Dec 19 '14 at 10:11
  • 4
    @opyate I would put that command in ~/.profile so that it will run only once after an interactive login. – suzanshakya Oct 21 '15 at 06:01

18 Answers18

164

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,246
  • 8
    +1 for referencing keyboard-config(7), this was what I was looking for. – ulidtko Nov 02 '13 at 21:49
  • 76
    You can also do this from the command line, without installing dconf-editor: dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:escape']" – Pi Delport Jan 18 '14 at 20:30
  • 6
    Don't run the above command using 'sudo' as this setting seems per user specific. – Bohr Apr 20 '14 at 01:14
  • Yes, these settings are in the user profile and can be different for different users. You need sudo to install dconf-editor if it's not already installed. – chaskes Apr 20 '14 at 02:44
  • 17
    Note that you need to use 'caps:swapescape' if you need to swap the two keys (instead of just assigning Esc to Caps Lock). – thameera Oct 05 '14 at 09:19
  • 3
    or you can do gsettings set org.gnome.desktop.input-sources xkb-options "['caps:escape']" – ACyclic Dec 07 '14 at 19:01
  • 1
    I wrote a small script to toggle this behaviour: handy when a co-worker wants to type something and gets confused by your swapped caps-esc. – berkes Oct 04 '15 at 10:03
  • The only working solution with CentOS 7 I've tried. – LRDPRDX Dec 27 '19 at 04:12
  • 1
    This is dependent on the window manager. @jojo's solution is better because it works even when you change your WM like I did. – James Parker May 02 '20 at 16:28
  • 4
    On Ubuntu 20.04, dconf-tools is not found, but sudo apt install dconf-editor did the trick. – npfoss Jun 01 '20 at 04:01
  • Stops working after automatic idle logout but works after the restart. It's better to have this configuration plus @jojo's answer as alias for the times when this stops working. – Nikhil Bhandarkar Dec 14 '20 at 13:49
  • The solution doesn't work anymore for ubuntu 20.04 as the package dconf-tools has been removed. https://askubuntu.com/questions/1248426/what-has-replaced-dconf-tools-for-ubuntu-20-04 – zdebra Jan 17 '21 at 20:17
  • If someone else can't get this to work at first (like me), your keyboard might be the problem. I have an Freestyle Pro keyboard and this solution didn't work, tried a different keyboard and it works perfectly. – Mr. Frobenius Jun 20 '21 at 12:50
  • @PiDelport How to undo this? – ajinzrathod Jul 11 '21 at 09:01
  • FWIW, the installation of this tool (with package dconf-editor now, instead of dconf-tools) allowed me to edit this OK, but making changes to it seemed to have no effect. Is it something where I need to log out and back in again first, or something? setxkbmap (as mentioned in another answer) had an immediate effect, though I'm unsure whether it will last (a previous attempt with xmodmap would work and then reset within... seconds? Some short amount of time. So far, though, it's been working for a few minutes, at least). – lindes Dec 24 '21 at 21:11
125

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,395
  • 1
  • 11
  • 10
  • 2
    This works in Ubuntu 14.04, Unity. – Leo Nov 27 '16 at 15:32
  • 3
    Worked for me in Ubuntu 16.04 as well – Jesse Chan Apr 20 '18 at 21:21
  • 7
    as of 18.04, putting setxkbmap commands in ~/.xsessionrc and ~/.profile no longer appear to make settings persist between logins/restarts – Conrad.Dean May 05 '18 at 10:58
  • 2
    @Conrad.Dean I'm using this in Mint 19 which is based on 18.04 LTS: No problems here. The settings are not supposed to persist but are loaded at login. – j-i-l Jul 25 '18 at 10:37
  • 7
    ~/.profile is a poor place to put this -- that file is read on every login shell. Put it in a file used when starting X11, like ~/.xinitrc or ~/.xsession. – sarnold Jun 21 '19 at 01:28
  • There seems to be an order of operations that matters as well - this command might have to be run before the application you want to affect is open and things like suspending your computer can reset it – George Mauer Jan 21 '21 at 15:15
  • 1
    This worked for me; thanks! Suggestion for further improvement: among your "Other options are possible" section, maybe also list ctrl:nocaps and ctrl:swapcaps, as those are likely also common use cases. And/or refer to man 7 xkeyboard-config for the full suite of possibilities. Anyway, thanks! – lindes Dec 24 '21 at 21:15
  • VSCode on Fedora is absolutely confused by this. Sometimes it needs escape, sometimes not. – ZenVentzi Sep 16 '22 at 10:05
  • I can only advise against modifying /etc/X11/xorg.conf.d/00-keyboard.conf as documented, it broke my system (22.04 LTS), I couldn't boot into GRUB anymore. Had to use a LIVE usb, chroot into the filesystem and delete the file. – Cyrill Nov 26 '23 at 15:59
57

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)

  • 5
    On Ubuntu 14 the layout of the window is very screwy, but it still works. – uvasal Jul 20 '14 at 10:57
  • 1
    Yes, I think gnome-tweak-tool should not be used for this task because there are default tools to do it. – Léo Léopold Hertz 준영 Dec 31 '15 at 20:44
  • This is by far the best solution. – Jason McVetta Sep 19 '18 at 07:51
  • 10
    For Ubuntu >18.04 it can be found in Keyboard & Mouse > Additional Layout Options > Caps Lock Behavior – Adracus Apr 25 '19 at 19:40
  • This worked for me on 19.04. I couldn't get any of the xmodmap or setxkbmap solutions to persist on 19.04. – Andrew Sep 19 '19 at 18:05
  • 4
    The executable is called gnome-tweaks now. – Claes Mogren Dec 10 '19 at 07:26
  • 2
    Recently this setting from tweaks is ignored on my external keyboard (but respected on my laptop) after resuming from suspend with 20.04. I have to select some other option, then reselect "Swap ESC and Caps Lock". Will go for the setxkbmap solution now – Fred Schoen Dec 27 '20 at 17:22
  • 1
    I don't see this option in gnome-tweaks –  Dec 27 '20 at 17:40
  • If someone else can't get this to work at first (like me), your keyboard might be the problem. I have an Freestyle Pro keyboard and this solution didn't work, tried a different keyboard and it works perfectly. – Mr. Frobenius Jun 20 '21 at 12:51
  • On 22.04 this is still a viable option. Installed gnome-tweaks (name changed?). Keyboard&Mouse -> Additional Layout Options -> Ctrl position. There, in my case I chose Swap Ctrl and Caps Lock. – IsaacS Mar 25 '23 at 16:45
  • Substituted gnome-tweaks and the rest of the instructions worked perfectly on a new install of Ubuntu 22.04.2 LTS. Thanks! – Subfuzion Jul 28 '23 at 01:14
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
  • 169,590
  • 7
    This does not work under Ubuntu 14.04. I also tried a .xsessionrc file, and it also did not work. – miguel.martin Jul 21 '14 at 14:58
  • How do you undo the settings? – hlin117 Feb 02 '15 at 01:17
  • @hlin117 Just clear the content of the ~/.xmodmap file (by running only > ~/.xmodmap in your terminal), then restart your session. – Radu Rădeanu Feb 02 '15 at 09:42
  • 2
    On Ubuntu 14.04, this makes Escape maps to Caps_Lock but Caps_Lock does not map to Escape. – user1691145 Aug 30 '15 at 01:41
  • 2
    Hi, I did this, and it works. However, besides esc being bound to capslock, caps-lock is now bound to both caps-lock and escape. Is it possible that the desktop environment somehow still overrides this? I use KDE – Frido Emans Sep 23 '16 at 12:03
  • If capslock key still enables the capslock in addition to functioning as escape key, use clear Lock on top of .xinitrc file. – Ejaz Sep 05 '17 at 22:06
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
  • 2
  • 5
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
13

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']"
Chad Skeeters
  • 231
  • 2
  • 4
11

For Ubuntu 18.04 and Gnome 3.30 this works for me:

gsettings set org.gnome.desktop.input-sources xkb-options "['caps:swapescape']"
modesto
  • 219
  • 2
  • 4
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,687
  • 2
  • 16
  • 27
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,815
  • it didn't work on ubuntu 16.04 could you please elaborate more? – MaikoID Oct 19 '16 at 13:08
  • @MaikoID: This is pretty much the canonical way unixy way of swapping CapsLock and Esc. I understand that newer Ubuntu versions with Unity break a lot of "canonical unixy" things, that seems to be why all the other answers here address some version-specific or DE-specific (Unity) method. For what it's worth, this does work on Kubuntu, which I personally use. – dotancohen Oct 19 '16 at 13:45
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

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
  • 1
    While this answer may indeed resolve 1/2 the issue that the OP asked about it's showing up in the low quality answer queue due to (I assume) length. You might consider expanding it with further detail. How and why it works perhaps. – Elder Geek Mar 21 '18 at 18:33
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

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
  • 201
  • 1
  • 3
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
  • It is great that you have provided an answer to this question. Could you edit your question to expand a little, possibly with the desired outcome once select "Swap ESC and Caps Lock". is selected. – Phil UK Mar 21 '17 at 20:52
  • Hello and welcome to Ask Ubuntu! In its current state, this answer really isn't complete, and is therefore risking deletion. Please read our [answer] help page, and then come back and [edit] your answer to include more detail, specifically how this solves the OP's question. Additionally, please read our [tour] to get a badge and a better idea of how our site works. See you around! – Kaz Wolfe Mar 21 '17 at 22:56
  • thanks to your advices. i wish this edit is that you want. – farhad goodarzi Mar 23 '17 at 09:42
  • FYI, the link with your domain doesn't seem to work (at least for me), but the previous IP address domain works. – Andrew T. Jun 04 '18 at 03:42
-1

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