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.

  • 1
    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
  • 1
    @opyate I would put that command in ~/.profile so that it will run only once after an interactive login. – suzanshakya Oct 21 '15 at 6:01

14 Answers 14

up vote 114 down vote accepted

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.

  • 5
    +1 for referencing keyboard-config(7), this was what I was looking for. – ulidtko Nov 2 '13 at 21:49
  • 56
    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
  • 4
    Don't run the above command using 'sudo' as this setting seems per user specific. – Bohr Apr 20 '14 at 1:14
  • 8
    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 '14 at 9:19
  • 1
    @thameera Thanks for the info. – chaskes Oct 6 '14 at 15:37

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

  • 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 at 7:51

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 need to put it in ~/.profile as this will only run after the interactive login.

  • 1
    This works in Ubuntu 14.04, Unity. – Chad Nov 27 '16 at 15:32
  • 1
    Worked for me in Ubuntu 16.04 as well – Jesse Chan Apr 20 at 21:21
  • 2
    as of 18.04, putting setxkbmap commands in ~/.xsessionrc and ~/.profile no longer appear to make settings persist between logins/restarts – Conrad.Dean May 5 at 10:58
  • 1
    @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 at 10:37

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
    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 2 '15 at 1: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 2 '15 at 9: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 1:41
  • 1
    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 '16 at 12:03

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"
  • Doesn't work in Linux Mint 17.1 (which is based on Ubuntu) :( – Jan Warchoł Nov 20 '15 at 21:02
  • 3
    had to change "remove Lock = Caps_Lock" to "clear Lock" for it to work – Sofia Dec 23 '15 at 12:35

Go to the gears icon in the top right corner of the screen and:

  • click System Settings
  • keyboard layout
  • options...(lower right hand corner)
  • caps lock key behaviour (4th down)
  • Scroll down and choose "Swap ESC and Caps Lock".

Done!

  • Joe: Welcome to Ask Ubuntu! ;-) Could you please review my edits and also review the editing help to improve the readability of your answers in the future... ;-) – Fabby Feb 21 '15 at 21:24
  • 2
    This should be the best answer for ubuntu and mint – prasad Dec 21 '16 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 at 5:29

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']"
  • This solution just tested OK on RedHat Enterprise Linux 7. – Chad Skeeters Nov 9 '16 at 4:18

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

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.

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

  • 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

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

  • 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 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 '17 at 22:56
  • thanks to your advices. i wish this edit is that you want. – farhad goodarzi Mar 23 '17 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 at 3:42

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 at 18:33

The "Keyboard Layout" with "switch Escape and Capslock" answer above is no longer valid on Ubuntu 16.04 and on (was it before? dunno). The answer: in order to avoid the hustle with permissions etc. just switch to root before you start doing this all by su and entering your root password. Then:

    cd /etc/default/
    vim keyboard

There you will see the line:

    XKBOPTIONS=""

Change it to:

    XKBOPTIONS="caps:swapescape"

Exit vim with save:

    :wqa

Reboot.

And that will be permanent, I promise. :)

For Ubuntu 18.04 this works for me:

gsettings set org.gnome.desktop.input-sources xkb-options "['caps:escape']"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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