Skip to main content

Moving The Ctrl Key



X (using xmodmap)

In the X Windowing System you can use xmodmap to change the keymaping. Some desktop environments, like Gnome and KDE, which use X, have specialized preference menus to make this easier. The advantage of using xmodmap is that it will work with any desktop or Window Manager.

See the man page command xmodmap(1) for more information. I swap their functions throughout my environment, not just for Emacs.

On Unix-like systems, I have a ~/.xmodmap file:

! Swap Caps_Lock and Control_L
remove Lock = Caps_Lock
remove Control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L

which is sourced from my ~/.xsession with the line:

xmodmap ~/.xmodmap

## X (using xkb)

In the X Window System, you can also swap Control and Caps Lock with the xkb option `ctrl:swapcaps`. If you don’t ever need Caps Lock you can instead of swapping the two set Caps Lock to be another Control. This is done with `ctrl:nocaps`. Use one of these from command line:

  setxkbmap -option ctrl:swapcaps     # Swap Left Control and Caps Lock
  setxkbmap -option ctrl:nocaps       # Make Caps Lock a Control key

Note that the above will change your keyboard layout to a us keyboard layout. If it is not a US keyboard layout, you need to add a switch with your layout, like:

  setxkbmap -layout gb -option ctrl:nocaps # Caps Lock is Control on a GB  keyboard

If you don’t know your keyboard layout name, I think it is always the second value inside the xkb_symbols include line if you setxkbmap -print, so this command should preserve it for you:

  setxkbmap -layout "$(setxkbmap -print | awk -F + '/xkb_symbols/ {print $2}')" -option ctrl:nocaps

You can put this command in your `~/.xsession`, `~/.gnomerc` or whatever. If you can edit the X configuration file, you might do it system-wide:

  Section "InputDevice"
    Driver "keyboard"
    Option "XkbRules" [...]
    Option "XkbOptions" "ctrl:nocaps"

If above setting does not work, try this one (nowadays Xorg prefers):

Section "InputClass"
    Identifier            "Keyboard Setting"
    MatchIsKeyboard       "yes"
    Option                "XkbOptions" "ctrl:swapcaps"

In Fedora 15 you can also add a file `~/.Xkbmap` that contains command line arguments to `setxkbmap`. These will be used when you next log in.

-option ctrl:swapcaps

The [CapsKey]( page has an example of moving Ctrl to Caps.

# OS X

Open System Preferences → Keyboard and choose Modifier Keys:

![Mac Keyboard Preferences](/content/images/2015/09/MacKeyboardPreferences-png.png)

Then select which of the four modifier keys should send Control:

![Mac Keyboard Modifier Keys](/content/images/2015/09/MacKeyboardKeys-png.png)

The variable `mac-command-modifier` can be used to interpret the Command key as an additional Control. This can be useful for Linux-like behavior when Caps Lock has already been remapped to Command.

In Emacs 22 (Carbon Port, CVS version) you can use the variable `mac-control-modifier` to remap the Control key.

# Microsoft Windows

## Ctrl2cap

The easiest way to achieve this in Microsoft Windows is to install the Sysinternals program [Ctrl2cap](

## Windows Server 2003 Resource Kit Tools

Use remapkey.exe from [Windows Server 2003 Resource Kit Tools]( to remap windows keyboard layout.

## AutoHotkey

On Windows you can use the [AutoHotkey]( program which uses “scripts” to remap the keyboard.

This section describes how to swap Control and [CapsLock](, but with AutoHotKey it is also possible to use Space both as Space and Control. See [Using space bar as both space and control]( below.

This method has a couple benefits. One is scripts can be compiled to a stand alone .exe file which can be executed on a machine that doesn’t have AutoHotkey. Another is, the key re-mapping can be set to only apply in emacs; the caps lock key will behave normaly in every other program. (If that’s what you want.)

To remap the Caps Lock key to Ctrl save the following to a file named `script.ahk`. Then execute the script with [AutoHotKey]( by double clicking it. See the [AHK2EXE]( documentation to learn how to make scripts into stand alone executables.

    #IfWinActive emacs  ; if in emacs
    +Capslock::Capslock ; make shift+Caps-Lock the Caps Lock toggle
    Capslock::Control   ; make Caps Lock the control button
    #IfWinActive        ; end if in emacs

The original Caps Lock behavior is here mapped to Shift + Caps Lock.

If you want the caps lock to be the control key everywhere remove the lines that begin with "[#IfWinActive](".

If you want this AHK script to remain in effect across windows restarts place the script in your startup folder. See the [startup section of the AHK FAQ]( for more specific directions.

Here is a exe of the [AutoHotKey]( script, so you don’t have to download autohotkey itself. (Note these programs map right control to caps lock.)

The script:
The source:

## Global Registry

* Applies across all user accounts.
* Requires Administrator access to write to the registry.
* More info here:



[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00



[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,1d,00,3a,00,3a,00,1d,00,00,00,00,00

This can also be done by manually editing the registry to accomplish the same thing, see [[1]]( for an explanation.

To undo the changes, you can just delete the Scancode Map value, or run a .reg script with the following:


[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=-

Note: the Synaptics touchpad driver for Windows reads the unmodified scancodes while [PalmTouch]( is active, so if you switch Ctrl and Capslock you will also have to turn off [PalmTouch]( if you want to send ‘Ctrl-Taps’ using the touchpad.

## Current User Registry

You can instead make similar changes to

[HKEY_CURRENT_USER\Keyboard Layout]

which makes the change for just the current user only.

## Keymap

There is a small piece of software called keymap which modifies the registry.

## KeyMapper

It seems that keymap.exe does not work on Windows 7. [KeyMapper]( works fine.

## SharpKeys

[SharpKeys]( works like [Keymap]( and KeyMapper as it will modify the registry for you, and it is know to work with Windows 8.


Comments powered by Disqus