97

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.

10 Answers 10

83

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.

| improve this answer | |
  • 3
    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 '13 at 18:34
  • 4
    For Ubuntu 13.10, you'll need to use gnome-tweak-tool. – Jeremy Bicha Sep 11 '13 at 15:02
  • 1
    Pre 13.10 option works for me in 14.04 – JeremyKun Aug 11 '17 at 17:30
  • Same as @JeremyKun. In my case, using 14.04, the pre 13.10 solution worked but the 13.10+ didn't – matiascelasco Sep 18 '17 at 15:59
  • Thanks for this answer! The gnome-tweak-tool is really nice; you can also use it for niceties like "use Caps Lock for switching keyboard layouts" (a personal favourite) and "switch Alt/Win" (which is useful if you've been a long-time Mac user...) – Per Lundberg Jul 2 '18 at 19:27
70

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

setxkbmap -layout us -option ctrl:nocaps

Source:

| improve this answer | |
  • 3
    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 '15 at 21:12
  • 11
    @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. – Léo Léopold Hertz 준영 May 15 '16 at 16:28
65

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!

| improve this answer | |
  • 1
    Update: gnome-tweak-tool only works inside the gnome shell – Edward Falk Jun 29 '15 at 23:21
  • 2
    no "typing" section in my left-hand column: only Desktop, Fonts, Shell, Shell Extensions, Theme, Windows. – Reb.Cabin Sep 15 '15 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 '17 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. – Pierre Thalamy Sep 25 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 at 18:33
48

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"
| improve this answer | |
  • 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 '15 at 2:18
  • 2
    you have 3 steps (1, 2, 3). Do you mean [(1 and 2) or 3] or [1 and (2 or 3)]? – jgomo3 Apr 27 '16 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. :( – LeOn - Han Li Oct 28 '16 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 at 6:15
25

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']"
| improve this answer | |
  • This one liner can also be added to ~/.profile for portability. – jthetzel Oct 8 '15 at 12:08
  • This did not work for me on Kubuntu 16.04, even after a reboot. – zplizzi Sep 19 '16 at 2:50
  • 3
    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. – Anton Eliasson Sep 30 '16 at 16:59
6

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. See here for more details.

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.

| improve this answer | |
  • 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 '19 at 11:44
  • 1
    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 at 16:50
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 ;-)

| improve this answer | |
  • 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. – James Henstridge Apr 11 '11 at 7:59
  • @James would you like to explain how can we do that? – desgua Apr 11 '11 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. – James Henstridge Apr 11 '11 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. – James Henstridge Apr 12 '11 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 '11 at 0:52
4

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.

Screenshot from tweak-tool

| improve this answer | |
2

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

| improve this answer | |
2

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.

| improve this answer | |
  • 1
    Keyboard& mouse -> additional layout option – Voyager May 25 '19 at 18:56

Your Answer

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