Skip to main content

Moving The Ctrl Key

Ref: http://www.emacswiki.org/emacs/MovingTheCtrlKey

Linux

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"
EndSection
```

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](http://www.emacswiki.org/emacs/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](https://technet.microsoft.com/en-us/sysinternals/bb897578.aspx?f=255&MSPPError=-2147217396).

## Windows Server 2003 Resource Kit Tools

Use remapkey.exe from [Windows Server 2003 Resource Kit Tools](http://www.microsoft.com/en-us/download/details.aspx?id=17657) to remap windows keyboard layout.

## AutoHotkey

On Windows you can use the [AutoHotkey](http://www.autohotkey.com/) program which uses “scripts” to remap the keyboard.

This section describes how to swap Control and [CapsLock](http://www.emacswiki.org/emacs/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](http://www.emacswiki.org/emacs/MovingTheCtrlKey#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](http://www.emacswiki.org/emacs/AutoHotKey) by double clicking it. See the [AHK2EXE](http://autohotkey.free.fr/docs/Scripts.htm#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](http://www.emacswiki.org/emacs/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](http://www.autohotkey.com/docs/FAQ.htm#Startup) for more specific directions.

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

The script: http://www.cs.umb.edu/~wimiller/stuff/caps2ctrl.exe
The source: http://www.cs.umb.edu/~wimiller/stuff/caps2ctrl.ahk

## Global Registry

* Applies across all user accounts.
* Requires Administrator access to write to the registry.
* More info here: http://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Swap-CapsLock-and-Control.html#Swap-CapsLock-and-Control

**NoCaps**

```
REGEDIT4

[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
```

**SwapCaps**

```
REGEDIT4

[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]](http://geekquanta.blogspot.com/2007/07/remapping-ctrl-and-caps-lock-in-windows.html) for an explanation.

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

```
REGEDIT4

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

Note: the Synaptics touchpad driver for Windows reads the unmodified scancodes while [PalmTouch](http://www.emacswiki.org/emacs/PalmTouch) is active, so if you switch Ctrl and Capslock you will also have to turn off [PalmTouch](http://www.emacswiki.org/emacs/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](http://code.google.com/p/keymapper/) works fine.

## SharpKeys

[SharpKeys](http://sharpkeys.codeplex.com/) works like [Keymap](http://www.emacswiki.org/emacs/KeyMapper) and KeyMapper as it will modify the registry for you, and it is know to work with Windows 8.

Comments

Comments powered by Disqus