70

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

54

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.

  • 2
    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
60

One of the best ways to do that graphically is to install Gnome Tweak Tool:

sudo apt-get install gnome-tweak-tool

  • 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 additionnal Ctrl key instead of Disabled in the drop down list and you should be good.

Besides the the setting panel does not take a lot of time to come up on my machine, so I have no idea what the problem is.

Enjoy your new Ctrl key!

  • I wanted to use my CAPS key to turn my screen off. I set this up before in the System Settings > keyboard window, but the CAPS still toggled CapsLock state and LED. So I disabled the key with the tool you recommended (which has some display errors btw. - transparent window background), which also stopped my custom shortcut from working. After disabling, I had to set the shortcut again to have it work. It got set to the value 0xff or something like this instead of CAPS. I thought this procedure might help some future readers with similar wishes. – Byte Commander Feb 17 '15 at 13:13
  • When I run gnome-tweak-tool from the command line, there's an error message about shell not running DBus and then any attempts I make to change settings have no effect. – Edward Falk Jun 29 '15 at 23:14
  • 1
    Update: gnome-tweak-tool only works inside the gnome shell – Edward Falk Jun 29 '15 at 23:21
  • 1
    no "typing" section in my left-hand column: only Desktop, Fonts, Shell, Shell Extensions, Theme, Windows. – Reb.Cabin Sep 15 '15 at 15:18
  • 1
    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
56

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

setxkbmap -layout us -option ctrl:nocaps

Source:

  • 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
  • 8
    @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
41

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"
  • 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
  • 1
    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
23

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']"
  • 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
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 ;-)

  • 1
    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
  • @James Thank you very much. So I guess we should only play with keymaps when 'System -> Preferences -> Keyboard' doesn't have a solution for what we want, right? – desgua Apr 11 '11 at 19:57
  • 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
4

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.

  • 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 at 11:44
3

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

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

0

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.

  • Keyboard& mouse -> additional layout option – Voyager May 25 at 18:56

Your Answer

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