75

EmacsWiki: Moving The Ctrl Key

 4 years ago
source link: https://www.emacswiki.org/emacs/MovingTheCtrlKey
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Moving The Ctrl Key

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

But note that some users find that doing this causes Emacs pinky because you’re extending your left pinky to hit Ctrl all the time. Better to keep the standard layout and press the Ctrl keys on either side of the keyboard with either the tip of your pinky (fan out your fingers and rotate your wrist toward the center of your body), your pinky fingernail (curl it under) or the knuckle at the base of the pinky (open your hand and slam it down). Get used to them all; the first word in RepeatedStrainInjury is Repeated.

Introduction

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

When Emacs was first written, most keyboards had Ctrl to the left of A, either immediately to the left or with CapsLock in between. Thus, it was a convenient and logical choice of key for creating Emacs commands. When personal computers went mainstream in the 80’s, Ctrl was moved out of the way, presumably because the target users (office workers) wouldn’t need Ctrl as often as CapsLock.

GNU/Linux

Note that separate keyboard mappings are often used for X (graphical sessions) and for virtual consoles. (On Debian, you may be able to use /etc/default/keyboard file for both.)

GNOME Desktop Environment

This setting is in the GNOME Tweak Tool.

Gnome Tweak Tool Icon

On the “Typing” tab, find the “Ctrl key position” setting and select “Swap Ctrl and Caps Lock” or “Caps Lock as Ctrl”; the latter is also available under “Caps Lock key behavior” as “Make Caps Lock an additional Ctrl”.

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:

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

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

On 2015/03/16, dumbl3d0re said: “This have never occured for me. Doesn’t happen for me on Debian 8-current.”

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

This is not the same as a terminal running in a window under X! It is the text-only mode typically accessed by pressing Ctrl+Alt+F1-F6.

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 and derivatives (Ubuntu, Mint etc.)

To make Caps Lock another Ctrl key, edit the file /etc/default/keyboard and change the line which reads

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

and then run:

 $ sudo dpkg-reconfigure -phigh console-setup

On 2020/03/28, Aju said: “Linux Mint 19.3 Tricia x86_64 Cinnamon 4.4.8 running the dpkg-reconfigure command did not change things for me but logging out and logging back in did.”

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

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”

Open System Preferences → Keyboard and choose Modifier Keys:

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

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: http://www.cs.umb.edu/~wimiller/stuff/caps2ctrl.exe

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

Using Return Key as Ctrl

Another recommendation is to use the return key as Ctrl when it is held down. This makes pressing keys on the left side of the keyboard in conjunction with Ctrl a lot easier. If another key is pressed while the Return key is pressed, the return key acts as a Ctrl. If the Return key is pressed and released alone then it acts as the Return. More details, an AutoHotKey script, and a compiled exe to do the above can be found here.

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.

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’ 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.

SharpKeys

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

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.

PowerToys Keyboard Manager

PowerToys Keyboard Manager for Windows 10 does not work with WSL and Windows Terminal as of August 2020.

With the most straight forward configuration, you can tell it is not working because holding the remapped caps lock key causes the region to activate and deactivate continually.

Android

When using a Bluetooth hardware keyboard attached to a android tablet or phone you might consider changing the SHIFT key also to CTRL. To do this Android wide, you need to have root access obviously. Open a console and gain root access with:

$ su

Then remount /system and change the desired keys in /system/usr/keymaps with the editor of your choice, e.g. nano:

# mount -o rw,remount /system
# nano /system/usr/keylayout/Generic.kl

Again Key 58 is the SHIFT key. Set it to CTRL_LEFT for instance. Save the file and restart your tablet/phone.

Another method

Files placed in /system will be replaced during an Android system update. Instead of overwriting the Generic.kl file, you can copy it as /data/system/devices/keylayout/Vendor_XXXX_Product_XXXX.kl and make the appropriate changes in that file.

You can find out the Vendor and Product id of your external keyboard by running

cat /proc/bus/input/devices

Your keyboard’s info should appear. Ensure that the created .kl file, as well as any directory you create to place it in, has the appropriate permissions i.e. the keylayout file should not be world-writable, or Android will not load it.

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

Install AutoHotKey to a folder in your user account.

Or, get a programmable keyboard such as 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

Another keyboard is the WASD keyboards. They have a switch that allows you to control whether the Caps Lock key is a control or standard caps lock.

– Elias

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK