138

I'd like to remap my keys such that Ctrl behaves as the Caps Lock key and vice-versa. Also, if possible I'd like the settings to be available only within the currently logged-in user. How can I achieve this?

I'm a vim user, unlike the other people who use this machine, so I'd like those settings only for my account.

4

14 Answers 14

116

13.10+:

Install and use gnome-tweak-tool > Keyboard & Mouse > Keyboard > Additional Layout Options > Caps Lock behavior.

Pre 13.10:

Open the Keyboard Preferences dialog (System -> Preferences -> Keyboard). On the layout tab, click the Options... button. Expand the Ctrl key position section and select Swap Ctrl and Caps Lock.

Those settings should be applied each time you log in, and will only affect your user account.

9
  • 4
    This option has disappeared in Ubuntu Saucy. I'm not sure how to explain this but it is simply not there. The Keyboard preferences have no "Layout tab", but there is a "layout settings" button, and when I click it it takes me to the "Input Sources" tab of the "Region & Language" panel, which is mysteriously lacking any kind of 'Options' button. The only button there opens up a virtual on-screen keyboard that displays my broken keyboard layout to me with no way of changing which keys behave which way.
    – robru
    Sep 6, 2013 at 18:34
  • 4
    For Ubuntu 13.10, you'll need to use gnome-tweak-tool. Sep 11, 2013 at 15:02
  • 1
    Pre 13.10 option works for me in 14.04
    – JeremyKun
    Aug 11, 2017 at 17:30
  • Same as @JeremyKun. In my case, using 14.04, the pre 13.10 solution worked but the 13.10+ didn't Sep 18, 2017 at 15:59
  • 1
    Install gnome-tweaks, and instead of Caps Lock behavior, pick Ctrl position and select Swap ctrl and Caps Lock. That way we can still use Caps Lock when needed. Jun 18, 2022 at 15:59
91

Here's a way to do it without installing extra software:

setxkbmap -layout us -option ctrl:swapcaps

Source:

6
  • 6
    This doesn't survive after reboot. How can I make this permanent? I'd rather not put this in a startup script (e.g. .bashrc).
    – nic
    Aug 16, 2015 at 21:12
  • 17
    @nic You need to also change the file sudo vi /etc/default/keyboard and change XKBOPTIONS="ctrl:nocaps". Run Jorge's command after that but as setxkbmap -option ctrl:nocaps such that it does not change your default keyboard layout. May 15, 2016 at 16:28
  • This also doesn't survive after wake up from suspend. Jun 18, 2022 at 14:18
  • This some how changed my left windows key to caps lock...
    – Jerinaw
    Nov 28, 2022 at 4:24
  • 1
    The link is broken.
    – alexpanter
    Apr 20, 2023 at 10:35
72

One of the best ways to do that graphically if you are using the GNOME shell is to install Gnome Tweak Tool:

sudo apt-get install gnome-tweak-tool

For version 3.30.0 and later:

  • Open tweak-tool and click on the Keyboard & Mouse section in the left menu bar.
  • Click on the Additional Layout Options button on the left.
  • Under Caps Lock behavior select Caps Lock is also a Ctrl.

For older versions:

  • Open tweak-tool and click on the typing section in the left column.
  • You should now see the line Caps Lock key behavior on the left.
  • Choose Make Caps Lock an additional Ctrl key instead of Disabled in the drop-down list and you should be good.

Enjoy your new Ctrl key!

10
  • 2
    Update: gnome-tweak-tool only works inside the gnome shell Jun 29, 2015 at 23:21
  • 2
    no "typing" section in my left-hand column: only Desktop, Fonts, Shell, Shell Extensions, Theme, Windows.
    – Reb.Cabin
    Sep 15, 2015 at 15:18
  • 2
    Note the option in gnome-tweak-tool to swap is under "Ctrl key position", not "Caps Lock key behavior".
    – Phil Goetz
    Jun 19, 2017 at 19:38
  • 1
    @Marius Strangely, I have just checked in Gnome Tweak Tools 3.34.0 (under GNOME Shell 3.36.6), and the Keyboard & Mouse menu is still there, with an Additional Layout Options button in it. Sep 25, 2020 at 12:34
  • 1
    Ah, thanks so much @PierreThalamy ! It is there, as a button. I was looking for a section, similar to Mouse, Touchpad...
    – Marius
    Sep 26, 2020 at 18:33
61

Open the following for editing:

sudo vi /etc/default/keyboard

And edit XKBOPTIONS="ctrl:swapcaps"

Then, reconfigure:

sudo dpkg-reconfigure keyboard-configuration

or

/usr/bin/setxkbmap -option "ctrl:swapcaps"
5
  • The question was "I'd like those settings only for my account." Editing /etc/default/keyboard is going to change it for everyone.
    – skierpage
    Jun 29, 2015 at 2:18
  • 3
    you have 3 steps (1, 2, 3). Do you mean [(1 and 2) or 3] or [1 and (2 or 3)]?
    – jgomo3
    Apr 27, 2016 at 20:30
  • Great! Ran the sudo dpkg-reconfigure keyboard-configuration and press several enters, and it worked!!! EDIT: however, after i log out and log back in, the setting went away. :( Oct 28, 2016 at 4:05
  • This should really be the accepted answer. It sets it system-wide and for both command-line and X11.
    – penguin359
    Sep 6, 2020 at 6:15
  • How do I turn it back off? I tried simply going back to XKBOPTIONS="" and reboot and then that, reconfigure, and reboot, but no such luck.
    – ruffin
    Feb 21, 2023 at 1:15
30

To permanently change the behaviour:

  1. run dconf-editor

  2. select org.gnome.desktop.input-sources

  3. Change xkb-options to ['ctrl:nocaps'] (or add it to any existing options)

or on the command line (Warning -- this overwrites your existing settings!):

gsettings set org.gnome.desktop.input-sources xkb-options "['ctrl:nocaps']"
3
  • This one liner can also be added to ~/.profile for portability.
    – jthetzel
    Oct 8, 2015 at 12:08
  • 1
    This did not work for me on Kubuntu 16.04, even after a reboot.
    – zplizzi
    Sep 19, 2016 at 2:50
  • 4
    dconf settings apply immediately and are persistent, so it makes no sense to put it in ~/.profile and there is no need for a reboot. This particular setting only applies to Gnome/Unity, not KDE that is used in Kubuntu. Sep 30, 2016 at 16:59
12

This is how to do it manually (without additional tools), via XKB, which is the default keys manager for recent Ubuntus. Modify /usr/share/X11/xkb/symbols/pc , section xkb_symbols "pc105":

//key <CAPS> {  [ Caps_Lock     ]   };
//key <LCTL> {  [ Control_L     ]   };
key <CAPS> {    [ Control_L ]   };
key <LCTL> {    [ Caps_Lock     ]   };

Login/logout or reboot.

Alternatively you can swap at the level of keycodes that are emitted by those buttons. Modify /usr/share/X11/xkb/keycodes/evdev :

<CAPS> = 64; //66;
<LCTL> = 66; //64

You'll need to sudo rm -rf /var/lib/xkb/* to apply the changes.

6
  • 1
    Great point about working at the level of the keycodes. On my laptops running Kubuntu Bionic, merely changing at the symbols level isn't satisfactory because pressing the physical Caps key still toggles the internal Caps state. Changing the keycodes works! BTW I extended this to three keys (Caps, Meta and RAlt) and would like to note that you actually need to write <CAPS> = *orig code of the physical key you want to act **as** CAPS* rather than <CAPS> = *orig code of the key you want physical CAPS to act **as** (I hope the distinction is clear). This distinction is needed in such cases.
    – jamadagni
    Feb 5, 2019 at 11:44
  • 3
    This is very good answer because it changes the map in the lower level, other answer have issues when connecting different keyboards or Bluetooth ones. This one does not suffer form that
    – Mike W
    Oct 15, 2020 at 16:50
  • 1
    If you have lets say a laptop with an extra external bluetooth keyboard the evdev patching is the one that will work for ALL they keyboards, sadly the /etc/default/keyboard didn't for me on ubuntu 20 Nov 9, 2020 at 10:22
  • 2
    Some how this did not work for me. Neither option worked. CAPs is still caps and ctrl no longer works.
    – Jerinaw
    Nov 28, 2022 at 4:41
  • I also have issue on Ubuntu 22, where regular Control_L still works, but Caps_Lock now does nothing.
    – alexpanter
    Apr 20, 2023 at 10:41
7

The accepted answer is confusing because gnome-tweak-tools doesn't show any "typing" section.

Instead, click on "Keyboard & Mouse" section and then choose "Additional Layout Option". There, you will see "Caps Lock behavior" which allows converting caps lock to different keys.

1
  • In Ubuntu 22.04, there is also the section `Ctrl position' -> 'Caps Lock as Ctrl'
    – RAbraham
    May 28, 2022 at 12:38
4

This is an easy task once you know how to do it.

1) Check the keycode of yours key. Run this program at terminal.

xev


At this example, the terminal shows that the keycode for my k is "45".

2) Change them as you like creating this file:

gedit ~/.Xmodmap

It's contents should look like this example:

keycode 37 = Caps_Lock NoSymbol Caps_Lock
keycode 66 = Control_L NoSymbol Control_L

(Change the keycode number as needed - look at step "1")

Ilustration:
(Obs.: if I want to change my k I should use "keycode 45" as showed at step "1").

4) Logout and log back in or reboot or run this:

xmodmap ~/.Xmodmap

Hope you enjoy ;-)

6
  • 2
    Note that this solution is likely to cause problems for anything but the simplest keymaps. The xmodmap compatibility in the newer xkb system is not perfect, so you're probably better off sticking to xkb if possible. Apr 11, 2011 at 7:59
  • @James would you like to explain how can we do that?
    – desgua
    Apr 11, 2011 at 10:35
  • See the other answer to this question. The standard keyboard control panel will construct and xkb keymap from the base layout you pick plus the options you enable. Apr 11, 2011 at 14:47
  • 1
    Well, you can set up the same keymaps using the command line setxkbmap program. Writing new keymaps is possible, but a bit more involved than Xmodmap keycode mappings. It is a good thing that most of the option variants you'd want are already available. Apr 12, 2011 at 7:54
  • 1
    @James. I have been searching and learning about setxkbmap. But I couldn't find an answer for: is it possible to remap a key to anything we want? For example something eccentric like: "keycode 51 = bracketright braceright bracketright braceright masculine bar"?
    – desgua
    Apr 13, 2011 at 0:52
4

On KDE-based distributions (like KDE Neon or Kubuntu) this behavior can be configured in the regular system settings. Open the system settings, select "Input Devices" => "Keyboard" => "Advanced". In the category "Ctrl key position" select "Swap Ctrl and Caps Lock".

Screenshot of KDE's System Setting's Keyboard module

1

gnome-tweaks 3.28.1 has no Typing section.
Thus, setxkbmap -layout us -option ctrl:nocaps or setxkbmap -layout us -option ctrl:swapcaps would be feasible options.

To make it permanent, you can refer to the answer of @name.

2
  • 2
    Keyboard& mouse -> additional layout option
    – Voyager
    May 25, 2019 at 18:56
  • It is not feasible to change the layout.
    – ceving
    Dec 18, 2021 at 18:00
0

If you are using i3, you can add this to your i3 config:

exec_always "setxkbmap -layout au -option ctrl:swapcaps"

This will make it that the keys are always swapped on i3 load.

0

trough tweaks don't work together

  • swap Ctrl and Caps Lock
  • switch to another layout by Caps Lock

find solution:

  • Caps Lock as Ctrl
  • switch to another layout by Left Ctrl
  • both Shift together enable Caps Lock
0

To have capslock behave as a ctrl at all times, I had to do the following.

For the behavior in the gnome session, use gnome-tweaks. For the behavior at the login screen and in text-only sessions, do the following.

Edit /etc/default/keyboard and set:

XKBOPTIONS="ctrl:nocaps"

Then

sudo dpkg-reconfigure -phigh console-setup

And finally, reboot.

0

If you are wanting to make the Caps Lock key an additional Ctrl key, just type this in the terminal as your user:

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

# or you can use dconf
dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:ctrl_modifier']"

To make sure the setting is effective:

gsettings get org.gnome.desktop.input-sources xkb-options

You must log in to answer this question.