How do I remap the Caps Lock and Ctrl keys?

140,498

Solution 1

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.

Solution 2

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

setxkbmap -layout us -option ctrl:swapcaps

Source:

Solution 3

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!

Solution 4

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"

Solution 5

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']"
Share:
140,498

Related videos on Youtube

sudcha
Author by

sudcha

Updated on September 18, 2022

Comments

  • sudcha
    sudcha over 1 year

    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.

  • James Henstridge
    James Henstridge about 13 years
    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.
  • desgua
    desgua about 13 years
    @James would you like to explain how can we do that?
  • James Henstridge
    James Henstridge about 13 years
    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.
  • desgua
    desgua about 13 years
    @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?
  • James Henstridge
    James Henstridge about 13 years
    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.
  • desgua
    desgua about 13 years
    @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"?
  • alteraki
    alteraki over 10 years
    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.
  • Jeremy Bicha
    Jeremy Bicha over 10 years
    For Ubuntu 13.10, you'll need to use gnome-tweak-tool.
  • Byte Commander
    Byte Commander about 9 years
    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.
  • skierpage
    skierpage almost 9 years
    The question was "I'd like those settings only for my account." Editing /etc/default/keyboard is going to change it for everyone.
  • Edward Falk
    Edward Falk almost 9 years
    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
    Edward Falk almost 9 years
    Update: gnome-tweak-tool only works inside the gnome shell
  • nic
    nic over 8 years
    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).
  • Reb.Cabin
    Reb.Cabin over 8 years
    no "typing" section in my left-hand column: only Desktop, Fonts, Shell, Shell Extensions, Theme, Windows.
  • jthetzel
    jthetzel over 8 years
    This one liner can also be added to ~/.profile for portability.
  • jgomo3
    jgomo3 almost 8 years
    you have 3 steps (1, 2, 3). Do you mean [(1 and 2) or 3] or [1 and (2 or 3)]?
  • Léo Léopold Hertz 준영
    Léo Léopold Hertz 준영 almost 8 years
    @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.
  • zplizzi
    zplizzi over 7 years
    This did not work for me on Kubuntu 16.04, even after a reboot.
  • Anton Eliasson
    Anton Eliasson over 7 years
    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.
  • LeOn - Han Li
    LeOn - Han Li over 7 years
    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. :(
  • Phil Goetz
    Phil Goetz almost 7 years
    Note the option in gnome-tweak-tool to swap is under "Ctrl key position", not "Caps Lock key behavior".
  • JeremyKun
    JeremyKun over 6 years
    Pre 13.10 option works for me in 14.04
  • matiascelasco
    matiascelasco over 6 years
    Same as @JeremyKun. In my case, using 14.04, the pre 13.10 solution worked but the 13.10+ didn't
  • Per Lundberg
    Per Lundberg almost 6 years
    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...)
  • jamadagni
    jamadagni about 5 years
    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.
  • Joe Bane
    Joe Bane about 5 years
    I found it under "Keyboard & Mouse" -> "Additional Layout Options" -> "Caps Lock Behavior" -> "Caps lock is also a Ctrl".
  • Pramod H G
    Pramod H G about 5 years
    tweak-tool doesn't work if you use e.g. VNC Viewer
  • Voyager
    Voyager almost 5 years
    Keyboard& mouse -> additional layout option
  • relet
    relet over 4 years
    Thank you. My control key broke on one keyboard, so I went for the option "Ctrl is mapped to Win and the usual Ctrl keys", which makes it work perfectly on either keyboard.
  • gfrigon
    gfrigon over 3 years
    This should really be the accepted answer. It sets it system-wide and for both command-line and X11.
  • Marius
    Marius over 3 years
    There's no "Keyboard & Mouse" -> "Additional Layout Options" in gnome 3.34
  • Marius
    Marius over 3 years
    There's no "Keyboard & Mouse" -> "Additional Layout Options" in gnome 3.34
  • Pierre Thalamy
    Pierre Thalamy over 3 years
    @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.
  • Marius
    Marius over 3 years
    Ah, thanks so much @PierreThalamy ! It is there, as a button. I was looking for a section, similar to Mouse, Touchpad...
  • Mike W
    Mike W over 3 years
    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
  • Jaime Agudo
    Jaime Agudo over 3 years
    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
  • ceving
    ceving over 2 years
    It is not feasible to change the layout.
  • Admin
    Admin almost 2 years
    In Ubuntu 22.04, there is also the section `Ctrl position' -> 'Caps Lock as Ctrl'
  • Admin
    Admin almost 2 years
    This also doesn't survive after wake up from suspend.
  • Admin
    Admin almost 2 years
    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.