140

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.

k0pernikus
  • 6,336

15 Answers15

121

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.

94

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

setxkbmap -layout us -option ctrl:swapcaps

Source:

Janne
  • 133
Jorge Castro
  • 73,717
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!

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"
amc
  • 7,292
name
  • 711
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']"
David Foerster
  • 36,890
  • 56
  • 97
  • 151
13

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.

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.

Colin D
  • 180
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

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 ;-)

desgua
  • 33,215
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.

1

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.

chtenb
  • 235
  • 1
  • 3
  • 13
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.

Vinn
  • 123
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

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
GMaster
  • 222
  • 2
  • 13
0

The input remapper ubuntu package can easily remap keys and has a GUI example of swapping in the gui

KhalfaniW
  • 121