How to swap Ctrl and Caps Lock keys in Linux

Linux desktop environments make it easy to set up your keyboard as you want it. Here's how.
167 readers like this.

1995 Sun Workstation keyboard

For many people who've been computer users for (let's just say) "quite some time now," the Ctrl and Caps Lock keys have been in the wrong place since shortly after the first PC keyboards rolled off the production line. For me, the correct positioning appears in this image of a vintage 1995 Sun Workstation keyboard. (Forgive me for the blurriness of the image; it was taken with a Minox spy camera in low light.)

If you're interested, you can read about the history of the Ctrl key location. I'm not going to discuss the various rationales for placing the Ctrl key next to the "a" key versus below the Shift key; I'm not going to comment on the overall uselessness of the Caps Lock key (whoops); and I'm not going to argue with those who advocate using the heel of the hand to activate the Ctrl key, even though it's impossible to do on some laptop keyboards where the keys are inset below the level of the wrist rest (whoops).

Rather, I'm going to assume I'm not the only one who prefers the Ctrl key next to the "a" and describe how to use the wonderful flexibility that comes with Linux to swap the Ctrl and Caps Lock keys on various desktop environments. Note that this kind of advice seems to have a limited shelf life, as tools for tweaking desktop settings change fairly often. But I hope this offers a good place for you to start.

With GNOME 3

GNOME 3 desktop environment users can use the Tweaks tool to swap their Caps Lock and Ctrl keys, as you can see below.

GNOME Tweaks Additional Layout Options

Here's how to do it:

  1. Install the Tweaks tool from your distribution's repositories.
  2. Start the Tweaks application.
  3. Select "Keyboard & Mouse" from the left-hand menu.
  4. Click "Additional Layout Options".
  5. Click "Ctrl position" on the window that opens and choose "Swap Ctrl and Caps Lock."

That's it! By the way, you can do lots of cool stuff with the Tweaks tool. For example, I set my right Ctrl key to be a Compose key, which allows me to type all sorts of characters with keyboard shortcuts—such as ç, é, ô, and ñ and with the keystrokes Compose+c+Comma; Compose+e+Right quote; Compose+o+Circumflex; and Compose+n+Tilde.

With KDE

I don't use KDE, but item 5 in this article about KDE tweaks that will change your life by my colleague Seth Kenlon will show you how to remap your keys.

With Xfce

As far as I can tell, the Xfce desktop environment doesn't have a handy tool for managing these kinds of settings. However, the ctrl:swapcaps option to the setxkbmap command will help you make these changes. This type of modification has two parts:

  1. Figuring out the command's usage;
  2. Figuring out where to invoke the command so it is activated as the desktop comes up.

The first part is pretty straightforward: the command is:

/usr/bin/setxkbmap -option "ctrl:nocaps"

It's worth executing this in a terminal window to make sure the results are what you expect.

Assuming it works, where should you invoke the command? That requires some experimentation; one possibility is in the file .profile in the user's home directory. Another option is to add the command to the autostart facility in Xfce (look for "Session and Startup" in the Settings Manager).

Another possibility is to use the same option in the file /etc/default/keyboard, which might end up looking like this:


# Consult the keyboard(5) manual page.



Note that this kind of change will affect all users, so if you share your computer, be prepared to do some explaining. Also, system updates may overwrite this file, so you'll need to edit it again if your setup stops working. Putting the same information in the file .keyboard in the user's home directory might accomplish the same task on the user's behalf.

Finally, note that these kinds of changes require you to restart Xfce (except when running the command on the command line in the terminal window, but that won't stick past the end of the session).

With LXQt and other desktop environments

I haven't tried LXQt, but if my memory serves from LXDE, I would try the same recipe used above for Xfce. I'd also expect that the Xfce recipe could work for other Linux desktop environments, but, of course, your favorite search engine is always your friend.

The console

I haven't tried this, as I have very few opportunities to interact with the console (what you see on a server or when your window system doesn't come up properly). The recipes presented above affect the terminal window in the way one would hope, i.e., consistently with other applications.

However, if the file /etc/default/keyboard or ~/.keyboard has already been edited (as described above), the utility setupcon is intended to change the console keyboard setup so it functions the same way. This StackExchange articlethis other one, and this third one give some ideas on how to effect these changes from both of these files. The third article also talks about using dumpkeys and loadkeys. It's also worthwhile to read the setupcon man page — it's short and to the point, and combined with the comments from the StackExchange articles, should be enough to get a solution in place.

Finally, it's worth emphasizing here the point mentioned in the StackExchange articles - configuring the console IS NOT THE SAME as configuring terminal windows; the latter are configured through the desktop manager as described previously.

When all else fails

The manual pages for setxkbmap, xkeyboard-config, keyboard, console-setup, and setupcon are all useful references. Or, if you don't like reading manual pages, there's this great article.

Chris Hermansen portrait Temuco Chile
Seldom without a computer of some sort since graduating from the University of British Columbia in 1978, I have been a full-time Linux user since 2005, a full-time Solaris and SunOS user from 1986 through 2005, and UNIX System V user before that.


"Forgive me for the blurriness of the image; it was taken with a Minox spy camera in low light."

I've always wanted a Minox, but having to carry an office desk and a lamp with you, seems a touch inconvenient :)
PS I have a trench coat and a trilby so I'm nearly there.

Thanks for the fun comment, MartyMonroe! As a matter of curiousity WHERE do you get your trilbies? I'm coming up dry on them locally...

In reply to by MartyMonroe

I sometimes do this with Xmodmap.

In theory, you have to use `xev` to find out the correct keycodes for your keyboard, but honestly I'm using the same codes across three systems without a problem.

Put something like this in a file called ~/.xmodmap
remove Lock = Caps_Lock
remove Control = Control_L
keycode 66 = Control_L
keycode 37 = Control_L
add Control = Control_L

This removes the "lock"iness of the CapsLock key,
then removes control from the left control to provide a clean slate.
Then it assigns keycode 66 (caps lock on my keyboard) the Control_L signal.
Then it assign keycode 37 (left control on my keyboard) the Control_L signals.
Then it ensures that Control_L is a Control key.

Strictly speaking, this is overkill, but I have found the added explicitness to be useful within some stubborn environments.

To manifest this in your session, run:

$ xmodmap ~/.xmodmap

To make it happen at login, run the xmodmap command as part of your .bash_rc or .bash_profile or .profile or whatever autostart script you prefer.

This is probably a relevant quote from the archive you linked to:

> In principle, Xkb is supposed to take over. In practice, there are
> only three people in the known universe who understand Xkb, and nobody
> is quite sure who they are.

Of course, that was written in 2008 so there are probably 4 people by now.

Anyway, the URL provided in the archive contains a lot of great info, albeit in French. I'll have to brush up so I can read up.

Thanks for the article and the links.

In reply to by clhermansen

It's a pretty decent article, by the looks of things. I didn't try her suggestions but they're very detailed and don't leave a bunch to the imagination. As you say, some French is required :-)

In reply to by sethkenlon

There is also a wonderful world of programmable keyboards out there. See:

I had to do something like this on a laptop I bought. The laptop had a keyboard that sent the wrong key code for the backslash key (\). The "shift" on this key is the pipe symbol (|).

To fix, I had to use "setkeycodes 56 43" to reset the correct key codes for that key. That's system-wide. To make the fix at every boot, just create a new /etc/rc.d/rc.local file like this:

setkeycodes 56 43
exit 0


sudo chmod 750 /etc/rc.d/rc.local

With that, you don't need to do anything at the user level. Note that this fix doesn't impact backslash or pipe on my external keyboard, which are correct.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.