Innehållsförteckning RecentChanges News ElispArea HowTo Problems Suggestions

MovingTheCtrlKey

Last edit

Sammanfattning: Updated with info for Ubuntu Raring Ringtail (13.04)

Tillagd:

> On Ubuntu Raring Ringtail (13.04) (and possibly on other versions), it is posible to simply edit the file /etc/default/keyboard, modify the XKBOPTIONS setting (for example XKBOPTIONS="ctrl:nocaps") and do:
> $ sudo dpkg-reconfigure keyboard-configuration
> Which will apply the change to both virtual consoles and X.org.


This page explains how to move the Control key. Some users find that this can help to avoid “Emacs pinky” (RepeatedStrainInjury).

First several sections are devoted for Linux solutions. Note that the keyboard mappings are separate for X (graphical) and for virtual consoles. (Sidenote: This problem may be alleviated soon as Debian’s /etc/default/keyboard file appears designed to work for both virtual consoles and X.)

Introduction

For some, swapping Control and CapsLock is very important; the virtually useless CapsLock is far easier to press Control, crucial to Emacs yet really awkwardly located.

Some history: Old Unix keyboards had these keys’ positions inverted, and Sun keyboards always have. It seems the current layout became standard about the same time cheap x86 hardware became ubiquitous, although why is beyond me. [Note that the original IBM PC keyboards also had the Control key in the right place; it was only with the 101-key “enhanced” keyboards that it was moved to its currently useless position.]

It is possible to embrace this “new” keyboard layout, however, and go a step further than just swapping Control and CapsLock keys - and SwapControlAltAndCapsLock for an old Unix or Sun keyboard-like experience. It’s a mindblowing change for the first few days, but it makes keyboard usage much simpler and easier.

Gnome Desktop Environment

Recent versions of the Gnome desktop have a Keyboard Preferences dialog that allows you to configure the Ctrl key position. On Ubuntu this is found under System → Preferences → Keyboard. Under the “Layout Options” tab choose “Ctrl key position” and it gives you a number of choices.

Under Gnome Shell, the same settings can be found under System Settings → Keyboard Layout. Choose the Layouts tab, then click on “Options…”. Expand the “Ctrl key position” section and you’ll see a similar list of of options.

Gnome Keyboard Preferences Gnome Keyboard Layout Options

KDE Desktop Environment

In KDE 4.x there’s a “Keyboard Layout” dialog in the Systems settings that allow you to modify the Caps Lock behavior. First be sure to allow KDE to control keyboard layouts. Do this by selecting “Enable keyboard layouts” here:

KDE Keyboard Layout

Then turn on the desired options and press the apply button:

KDE Keyboard Layout Advanced Options

X (using xmodmap)

In the X Windowing System you can use xmodmap(1) 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(1x) 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 page has an example of moving Ctrl to Caps.

Virtual consoles

A virtual console is NOT an [agkux]term inside of X. It is the white-on-black text-only mode that scares some people who have been traumatized by Microsoft’s COMMAND.COM shell. If you do not use virtual consoles, there is no reason for you to read this section.

Generic GNU/Linux Virtual Console (any distribution)

Note: while these instructions should work under any distribution of GNU/Linux, you may wish to use a distribution-specific method which may be simpler.

Press Ctrl-Alt-F1 to enter the first virtual console.

Now run:

 $ sudo dumpkeys | head -1 > /usr/share/keymaps/Caps2Ctrl.map

Then add the following lines to that file:

 keycode 58 = Control #This makes Caps act as Ctrl
 # keycode 29 = Caps_Lock #This makes Ctrl act as Caps
 # alt_is_meta #This fixes the Alt key

Then run loadkeys on that file:

 $ sudo loadkeys /usr/share/keymaps/Caps2Ctrl.map

To revert run:

 $ sudo loadkeys -d #load default keymap file

For more extensive editing try:

 $ dumpkeys > my_keymap
 $ cp my_keymap trial_keymap
 $ edit trial_keymap
 $ sudo loadkeys trial_keymap
 $ man keymaps
  It should be noted that using numeric notation for the
  keysyms is highly unportable as the key action numbers
  To find out what keysyms there are available  for  use
  in keymaps files, use the command
        dumpkeys --long-info
  Unfortunately,  there  is  currently no description of
  what each symbol does. It has to be guessed  from  the
  name or figured out from the kernel sources.

On Debian GNU/Linux (console-setup method, newer)

To make CAPS LOCK another control key, edit the file /etc/default/keyboard and change the line which reads

 XKBOPTIONS=""

to

 XKBOPTIONS="ctrl:nocaps"		# Some people prefer "ctrl:swapcaps"

To make the change effective:

 $ sudo dpkg-reconfigure -phigh console-setup

On Debian GNU/Linux (console-tools method, older)

To make Caps Lock another control, add/uncomment the following line in /etc/console-tools/remap

 s/keycode  58 = Caps_Lock/keycode  58 = Control/;

To make the change effective:

 $ sudo invoke-rc.d console-screen.sh restart

On Ubuntu GNU/Linux virtual console

Ubuntu Maverick

Click on System > Preferences > Keyboard

This will open the Keyboard preferences. In “Layout” tab find “options…” then locate “Ctrl key position”

select “Swap Ctrl and CapsLock

or also you can select “Make CapsLock an additional Ctrl”

this also work for virtual consoles

1. Edit /etc/default/console-setup and add an X11 style caps lock modification:

 $ diff -U0 /etc/default/{console-setup.orig,console-setup}
 --- /etc/default/console-setup.orig     2009-02-09 03:05:27.000000000 -0500
 +++ /etc/default/console-setup  2009-02-09 04:11:20.000000000 -0500
 @@ -40 +40 @@
 -XKBOPTIONS=""
 +XKBOPTIONS="ctrl:swapcaps"

you can do “ctrl:nocaps” instead if you want to two control keys.

2. Trick dpkg into integrating your changes:

 $ sudo dpkg-reconfigure console-setup

and choose all the defaults.

(The above is a brief version of a bug comment)

On Ubuntu Raring Ringtail (13.04) (and possibly on other versions), it is posible to simply edit the file /etc/default/keyboard, modify the XKBOPTIONS setting (for example XKBOPTIONS=“ctrl:nocaps”) and do:

 $ sudo dpkg-reconfigure keyboard-configuration

Which will apply the change to both virtual consoles and X.org.

On Gentoo Linux

For i386 systems, to make Caps Lock another control: In /etc/conf.d/keymaps set

  KEYMAP="emacs"

Then run # /etc/init.d/keymaps restart (or reboot). See /usr/share/keymaps/i386/qwerty/emacs.map.gz for more information.

On Slackware GNU/Linux

Similar to Gentoo, you can add Caps Lock as another control key:

As super-user:

  $ loadkeys emacs
  $ echo "loadkeys emacs" >> /etc/rc.d/rc.local

You can just issue the second command and re-run rc.local, however that will initiate any commands in rc.local. This differs from my X setup where the menu key is used as Caps Lock. Currently for the virtual console there is no Caps Lock (any suggestions on this?) --JoelAdamson

I no longer have Caps Lock key too but I have never used it before so I do not see the need now it has been remapped to something else ;) – XavierMaillard

The main thing I use the CapsLock for is environment variables; I have started getting out of using it by using AbbrevMode, however I still find some instances where it’s useful.

After seeing how a friend of mine types (with her wrists flat on the table, and not moving her hands but straining to reach keys, also while holding modifier keys and depressing letters with the same hand), I’m seriously investigating whether the quirks of heavy keyboard use (dislike of CapsLock, accidentally hitting keys, RSI) are due to poor hand position instead of heavy typing use. I’ve been typing heavily for years, and other than moving the CtrlKey?, I’ve not developed any problems or developed the strong preferences indicated on some webpages. I’d like to get a HappyHacking? or Sun-style keyboard, but I’m concerned that nobody thinks it’s a problem to only have one control key: the rationalization I’ve seen some people make is that I should use one hand to accomplish certain keystrokes (e.g., “\C-x\C-f”, which I do rather more swiftly with two hands).

If you don’t have a CapsLock key, it’s still easy to type environment variables. Just use M-b M-u to uppercase the last word. – HackerB9

NetBSD console

To /etc/wscons.conf add:

mapfile /usr/share/wscons/keymaps/pckbd.c2c

then create the file /usr/share/wscons/keymaps/pckbd.c2c with the single line:

keysym Caps_Lock = Control_L

This can also be tested immediately (doesn’t require reboot) and temporarily (won’t survive a reboot) with: wsconsctl -w map+=“keysym Caps_Lock = Control_L”

OS X

Open System Preferences → Keyboard and choose Modifier Keys:

Mac Keyboard Preferences

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

Mac Keyboard Modifier Keys

For Mac OS older than 10.4, uControl allows switching keys.

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

AutoHotkey

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

This section describes ho 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.

Registry

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] for an explanation. 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.

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.

In Lisp?

I know this is fairly simple to do for emacs only, but I’ve never looked into how exactly, and Lisp is not my forte. Anyone?

That is doubtful.
I am able to get the xorg under Cygwin on windows to switch the keys via a command line argument to xwin. Surely then it should be possible to patch emacs to do this at least at the source level. Anyone?
Also, what do you mean by “last method?” I don’t see a method on here that doesn’t require Administrator access. I’d love to know one.
I think that when you run emacs through cygwin, it uses its own x server. When you give a command lin e switch to remap keys, you are effectively doing it across all of your X server. It just happens that your X server is only running emacs.
As far as I know, X does not allow per-application custom keybindings.
EmacsW32 , with w32-enable-caps-lock set to nil , reads that key as <capslock>. It should be possible to tell Emacs to treat it as a control. (define-key function-key-map [(capslock)] ‘event-apply-control-modifier) gets close.

Drivers

There are also these drivers: ctrl2cap.sys or ctrl2cap.vxd.

http://www.gnu.org/software/emacs/windows/faq3.html#capscontrol has more info about this.

Microsoft also provides an utility Ctrl2Cap to replace the caps lock with the ctrl key. (Note that you will lose the caps lock.) It allows you to reverse it as well.

If you use the AutoHotKey (www.autohotkey.com) Win32 macro program, the following line in autohotkey.ini will effectively remap Caps to Ctrl:

Capslock::Ctrl

Here is a exe of the AutoHotKey script, so you don’t have to download autohotkey itself.

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

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

Notice that it also maps right control to caps lock, maybe someone cap upload one that does not do that.

Using space bar as both space and control

An alternative to moving the control key, to avoid Emacs pinky, is to use the space bar as both space and control.

If you press the space bar alone, it inserts a space. If you press it together with another key, it acts as a control key.

You can achieve this by using at-home-modifier in X (Linux only). And for MS Windows, AutoHotKey with BigCtrl script. And for Mac, keyremap4macbook.

This is not limited to ctrl – shift, alt, or any other keys can be pressed with thumbs. See also the following Japanese keyboard section.

History: This trick was invented by K. Kimura in 2001, and has been known as “SandS”, standing for “Space and Shift” (the original idea was to use the space as shift) to some users in Japan. See the README of at-home-modifier for the full history.

Japanese keyboard

Yet another alternative to moving the control key, to avoid Emacs pinky, is to use Japanese keyboard.

Japanese keyboard (Wikipedia article) has many keys that you can press with thumbs, to which you can bind modifiers. For example, the bottom row of Japanese keyboard can work as [alt][shift][ctrl][shift][alt] and [Esc][Bs][Space][Enter][Tab], all pressed by thumbs, by using “at-home-modifier” or AutoHotKey mentioned in the previous section.

For experiences with Emacs and particular keyboards, see GoodKeyboards and Wiki:ErgonomicKeyboard (keyboards, pedals, etc.)

Foot switch

Another option is to use FootSwitches for Control key and other frequently used keys. For example I use two Usb pedals: one for Ctrl key, the other for Shift key, Shift+Space is bound to RET, and CapsLock for Backspace. – SewerynKokot

Keyboard and no admin privileges in MS Windows

I was unable to find an answer to the question below. All the remap solutions above assume that the user has Admin privileges under Windows, which is not the case for company-issued laptops. I am looking for an additional USB keyboard I could use at work…

Question: Where can I find a keyboard where CAPS LOCK can be programmed to be a CONTROL key “directly on the keyboard”?

My configuration: company-issued Laptop with Windows 7 no administration privileges (cannot change Windows Registry) normal QWERTY laptop with annoying CAPS LOCK key instead of the useful CONTROL key

All the keyboards I have googled have programmable function keys (gamers’ keyboards…), but usual keys such as CAPS LOCK cannot be changed.

IanSinkrad

One such keyboard is Topre’s Realforce 87U, which is sold at elitekeyboards.com. It features a DIP switch setting for swapping the control and caps lock keys, and includes a keypuller and replacement keys so that the labeling will also be correct. Though made by the same company that produces the Happy Hacking keyboards recommended on the GoodKeyboards page, the Realforce is a much more traditional PC keyboard and the one I most prefer to use with Emacs.

– Tuffy

You could also install AutoHotKey to a folder in your user account and do it that way. I just did that and it’s working perfectly.

– Aiguofer

You could also make your own keyboard and hardware mappings, there is a libre keyboard design at http://www.key64.org/ which have been designed for emacs users in mind, it’s design is ergonomic so is very comfortable to type with.

– Nesiax


CategoryKeys CategoryAccessibility