159

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.

2
  • 1
    I put that command in my ~/.bashrc so it persists between system restarts. (ctrl:nocaps for me, though, for tmux)
    – opyate
    Dec 19, 2014 at 10:11
  • 3
    @opyate I would put that command in ~/.profile so that it will run only once after an interactive login. Oct 21, 2015 at 6:01

17 Answers 17

155

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

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.

15
  • 7
    +1 for referencing keyboard-config(7), this was what I was looking for.
    – ulidtko
    Nov 2, 2013 at 21:49
  • 72
    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, 2014 at 20:30
  • 6
    Don't run the above command using 'sudo' as this setting seems per user specific.
    – Bohr
    Apr 20, 2014 at 1:14
  • 16
    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 5, 2014 at 9:19
  • 4
    On Ubuntu 20.04, dconf-tools is not found, but sudo apt install dconf-editor did the trick.
    – npfoss
    Jun 1, 2020 at 4:01
99

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.

7
  • 1
    This works in Ubuntu 14.04, Unity.
    – Leo
    Nov 27, 2016 at 15:32
  • 2
    Worked for me in Ubuntu 16.04 as well
    – Jesse Chan
    Apr 20, 2018 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 May 5, 2018 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.
    – jojo
    Jul 25, 2018 at 10:37
  • 6
    ~/.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, 2019 at 1:28
54

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)

9
  • 5
    On Ubuntu 14 the layout of the window is very screwy, but it still works.
    – uvasal
    Jul 20, 2014 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. Dec 31, 2015 at 20:44
  • 8
    For Ubuntu >18.04 it can be found in Keyboard & Mouse > Additional Layout Options > Caps Lock Behavior
    – Adracus
    Apr 25, 2019 at 19:40
  • 4
    The executable is called gnome-tweaks now. Dec 10, 2019 at 7: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 Dec 27, 2020 at 17:22
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
6
  • 7
    This does not work under Ubuntu 14.04. I also tried a .xsessionrc file, and it also did not work. Jul 21, 2014 at 14:58
  • How do you undo the settings?
    – hlin117
    Feb 2, 2015 at 1:17
  • @hlin117 Just clear the content of the ~/.xmodmap file (by running only > ~/.xmodmap in your terminal), then restart your session. Feb 2, 2015 at 9:42
  • 2
    On Ubuntu 14.04, this makes Escape maps to Caps_Lock but Caps_Lock does not map to Escape. Aug 30, 2015 at 1: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
    – xor
    Sep 23, 2016 at 12:03
20

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"
2
  • Doesn't work in Linux Mint 17.1 (which is based on Ubuntu) :( Nov 20, 2015 at 21:02
  • 4
    had to change "remove Lock = Caps_Lock" to "clear Lock" for it to work
    – scc
    Dec 23, 2015 at 12:35
16

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!

2
  • 3
    This should be the best answer for ubuntu and mint
    – prasad
    Dec 21, 2016 at 22:01
  • Does this do anything in addition to the scripted updates mentioned here? Prefer to do this in a script.
    – Ben Creasy
    Oct 22, 2018 at 5:29
12

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']"
2
  • This solution just tested OK on RedHat Enterprise Linux 7. Nov 9, 2016 at 4:18
  • It works on Ubuntu 21.04 Sep 8, 2021 at 18:05
11

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. :)

7
  • run sudo dpkg-reconfigure keyboard-configuration to make it permanent.
    – bagustris
    Feb 5, 2020 at 3:18
  • you don't need to do that if you do what I said. Mar 11, 2020 at 22:22
  • In Debian systems, changes in /etc/default/keyboard do not become immediately visible to X. You should either reboot the system, or use sudo udevadm trigger --subsystem-match=input --action=change. In order to activate the changes on the console, run setupcon(1). Oct 6, 2020 at 14:57
  • Did you read what I wrote? The "Reboot" word in particular? Oct 8, 2020 at 21:34
  • Can you make capslock behave like Ctrl if you hold it down?
    – user341493
    Nov 14, 2020 at 21:15
8

For Ubuntu 18.04 and Gnome 3.30 this works for me:

gsettings set org.gnome.desktop.input-sources xkb-options "['caps:swapescape']"
4
  • still working on 19.04. Jul 29, 2019 at 12:40
  • works for 19.10 Jan 22, 2020 at 10:10
  • 2
    I'm on Pop!_OS 19.10; this is the first thing I've tried that works and survives reboot. Mar 28, 2020 at 16:50
  • 2
    gsettings set org.gnome.desktop.input-sources xkb-options "['caps:escape']" to keep the regular ESC key (as in: no swapping) Jan 23, 2021 at 12:09
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.

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).

2
  • it didn't work on ubuntu 16.04 could you please elaborate more?
    – MaikoID
    Oct 19, 2016 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, 2016 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
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
  • 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, 2018 at 18:33
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".

4
  • 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, 2017 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 How to 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, 2017 at 22:56
  • thanks to your advices. i wish this edit is that you want. Mar 23, 2017 at 9: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 4, 2018 at 3:42
0

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

1
  • 3
    Unfortunately, Linux Mint is not on-topic for Ask Ubuntu. Mar 2, 2021 at 17:29
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.