Difference between revisions of "CapsLock Remap Howto"

From Noah.org
Jump to navigationJump to search
 
(37 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Engineering]]
 +
 +
'''Caps Lock''' is stupid.
 +
 
== CapsLock is Satan ==
 
== CapsLock is Satan ==
On most PC keyboards the Caps Lock key is where the Ctrl key should be.
 
Whenever I get a new keyboard or laptop, I always pop off the key
 
then take a razor blade and scrape off the "Caps Lock" print.
 
I then tweak my operating system so that it thinks this key is a Ctrl key.
 
Some people like to make it another Esc key.
 
  
There are three remaps that you have to worry about:
+
How often do you need to type in all caps? The '''CapsLock''' key may not be the most useless key on the keyboard -- I'd vote for the '''Print Screen''' key, but the '''CapsLock''' is certainly the most annoying key. Plus it is a big key and in prime real estate. This is a valuable set of attributes that should be reserved for a key that you hit often (Enter, Shift, Control, Delete, etc.).
1. [[#Windows|MS Windows]]
+
 
2. [[#Console|Linux Console]]
+
On most PC keyboards the '''CapsLock''' key is where the '''Ctrl''' key ''should'' be. Whenever I get a new keyboard or laptop, I always pop off the key then take a razor blade and scrape off the "CapsLock" print. I then tweak my operating system so that it thinks this key is a '''Ctrl''' key. Some people like to make it an additional Esc key.
3. [[#X11|UNIX X11]]
+
 
 +
Some people believe that IBM took the original computer terminal keyboards and replaced the '''Ctrl''' key with the '''CapsLock key''' in order to make their first home computer, the IBM PC, appear more familiar to people who were accustomed to typewriters. This is false. In fact, the first IBM PC's had the '''Ctrl''' key to the left of the '''A'''. Later on, IBM released the IBM PC/AT with a CapsLock favored keyboard. Other people blame Microsoft because they pushed all those other useless keys on the world. There is also a bit of a myth that the old mainframe keyboards were consistent in putting the '''Ctrl''' key to the left of the '''A'''. Actually, the original PC keyboards were the anomaly. Prior to the PC most printing and display terminals had the '''CapsLock''' in the position where it is today. This includes classics such as the DEC VT52 and VT100 terminals; IBM data display terminals such as the IBM2741 and IBM5253. Many of IBM's early small computers that predate the IBM PC had a mix of keyboard styles. Many non-PC personal computers of the time had the '''CapsLock''' to the left of the '''A''': TRS-80, BBC Micro, Osborne 1, Commodore Vic-20, and Commodore C=64. The Apple ][ series had the '''Ctrl''' key in the proper location to the left of the '''A''' key, but sadly, the original Macintosh did not -- it was afflicted with the '''CapsLock''' (and this was before the IBM PC/AT went to the Enhanced keyboard). The Atari computers had the proper '''Ctrl''' key, but their keyboards were horrible membrane things. The BeBox (BeOS) had '''CapsLock'''; Next had '''Ctrl'''.
 +
 
 +
== NoCaps! ==
 +
 
 +
The following sections explains ways to turn the CapsLock key into something useful. I like to turn it into a Control key, so these examples foxus on that. Other reasonable keys that people redefine '''CapsLock''' as are '''Escape''', and '''Backspace'''.
 +
 
 +
=== X.org X11 X-Windows ===
 +
 
 +
This is pretty much all you need in X to turns the CapsLock key into a Ctrl key. Note that this map is not persistent between logins, so you may want to add this to your '''~/.bashrc''' or '''~/.profile'''. In theory, there are a bunch of places you could put this that would seem more appropriate, such as '''/etc/X11/xorg.conf''' or '''/etc/X11/xorg.conf.d/00-keyboard.conf''', but these are all handled inconsistently by the various display managers and Xserver startup sequences. It's just easier to put it in one of your dotfiles. Of course, this may cause confusion on multi-user shared systems, but how many people really run X11 that way?
 +
<pre>
 +
setxkbmap -layout us -option ctrl:nocaps
 +
</pre>
 +
 
 +
=== Linux script ===
  
== Windows ==
+
This '''nocaps''' script is part of my personal ~/bin/ directory.
Go here [http://www.sysinternals.com/utilities/ctrl2cap.html Sysinternals].
+
<pre>
 +
#!/bin/sh
  
== Console ==
+
# This temporarily remaps the CapsLock key to a Control key.
You just need to remap keycode 58 from "Caps_Lock" to "Control"
+
# The keyboard will return to the previous settings after a
in your keymap file, then load the keymap using loadkeys.
+
# reboot. The Linux console and the X Window system each
 +
# handles keypresses separately, so each must be remapped
 +
# separately. First remap the X keyboard since this does not
 +
# require root access.
 +
 
 +
# Remap the CapsLock key to a Control key for
 +
# the X Window system.
 +
if type setxkbmap >/dev/null 2>&1; then
 +
        setxkbmap -layout us -option ctrl:nocaps 2>/dev/null
 +
fi
 +
 
 +
# You have to be root to remap the console keyboard.
 +
if [ "$(id -u)" != "0" ]; then
 +
        echo "This script is not running as root so"
 +
        echo "the console CapsLock cannot be remapped."
 +
        echo "Perhaps you forgot to run this under sudo."
 +
        echo "Note that this problem does not effect X."
 +
        echo "This only effects the consoles running on"
 +
        echo "Alt-f1 through Alt-f6."
 +
        exit 2
 +
fi
 +
# Remap the CapsLock key to a Control key for the console.
 +
(dumpkeys | grep keymaps; echo "keycode 58 = Control") | loadkeys
 +
</pre>
 +
 
 +
=== Windows ===
 +
 
 +
Go here [http://technet.microsoft.com/en-us/sysinternals/bb897578.aspx Sysinternals ctrl2cap].
 +
 
 +
This was the old URL: [http://www.microsoft.com/technet/sysinternals/Miscellaneous/Ctrl2Cap.mspx Sysinternals ctrl2cap].
 +
 
 +
=== Linux Console ===
 +
 
 +
Now it seems that everything is in '''/etc/default/keyboard''' or in '''/etc/default/console-setup'''. Edit one of these files, and find and modify the '''XKBOPTIONS''' line. It also seems that the console now uses the same X Window configuration.
 +
<pre>
 +
XKBOPTIONS="ctrl:swapcaps"
 +
</pre>
 +
 
 +
You might need to run:
 +
<pre>
 +
sudo dpkg-reconfigure -phigh console-setup
 +
</pre>
 +
 
 +
All the stuff under '''/etc/kbd/''' is ignored if you have console-setup installed... It's all very confusing.
 +
 
 +
==== Older stuff ====
 +
 
 +
You just need to remap keycode 58 from "Caps_Lock" to "Control" then load using loadkeys. Many Linux distros actually have hooks in place to do this for you. This is the command that will do the trick -- '''if''' you run as root and '''if''' you are at a console.
 +
<pre>
 +
(dumpkeys | grep keymaps; echo "keycode 58 = Control") | loadkeys
 +
</pre>
 +
Or this equivalent command:
 +
<pre>
 +
dumpkeys | sed 's/\s*58\s*=\s*Caps_Lock/ 58 = Control/' | loadkeys
 +
</pre>
 +
 
 +
If you get the following message then it means you didn't run `dumpkeys`  or `loadkeys` as root:
 +
<pre>
 +
Couldn't get a file descriptor referring to the console
 +
</pre>
 +
 
 +
On some systems I am seeing this now printed as '''CtrlL_Lock'''.
 +
 
 +
I put this in my Bash alias file:
 +
<pre>
 +
alias nocaps='sudo dumpkeys | sed "s/\s*58\s*=\s*Caps_Lock/ 58 = Control/" | sudo loadkeys'
 +
</pre>
 +
 
 +
Most distros put these types of commands into a keymap file. The keymap files are stored in different places depending on your version of Linux. It is sometimes called '''defkeymap''' or '''remap'''.
 +
 
 +
==== Newer Ubuntu 8.10 Intrepid ====
 +
 
 +
Simply uncomment the line '''s/keycode  58 = Caps_Lock/keycode  58 = Control/;''' in this file:
 +
 
 +
<pre>
 +
/etc/kbd/remap
 +
</pre>
 +
 
 +
==== Ubuntu before 8.10 Intrepid ====
  
The keymap files are stored in different places depending on your
 
version of Linux. Just do a locate for "defkeymap".
 
 
The keyboard maps in Ubuntu are stored here:
 
The keyboard maps in Ubuntu are stored here:
    /usr/share/keymaps/i386/qwerty/defkeymap.kmap.gz
+
 
On Red Hat Enterprise 4 this file is stored here:
+
<pre>
    /lib/kbd/keymaps/i386/qwerty/defkeymap.map.gz
+
/usr/share/keymaps/i386/qwerty/defkeymap.kmap.gz
 +
</pre>
 +
 
 +
==== Red Hat Enterprise 4 ====
 +
 
 +
On Red Hat Enterprise 4 the file is stored here:
 +
 
 +
<pre>
 +
/lib/kbd/keymaps/i386/qwerty/defkeymap.map.gz
 +
</pre>
 +
 
 +
=== old script to fix defkeymap on Ubuntu ===
  
 
The following script takes care of this for Ubuntu Linux:
 
The following script takes care of this for Ubuntu Linux:
 +
 
<pre>
 
<pre>
 
#!/bin/sh
 
#!/bin/sh
Line 36: Line 138:
  
 
=== console-tools ===
 
=== console-tools ===
This can also be done through [http://lct.sourceforge.net/ console-tools].
 
Edit '/etc/console-tools/remap' and uncomment 'keycode  58'.
 
  
== remap the X11 keyboard ==
+
This can also be done through [http://lct.sourceforge.net/ console-tools]. Edit '/etc/console-tools/remap' and uncomment 'keycode  58'.
When running XWindows you need to modify the X11 key map using xmodmap.
+
 
It is not sufficient to just modify the console keyboard mapping.
+
== X11 ==
Use xmodmap to load the following keymap file (save in ~/.Xmodmap):
+
 
 +
When running XWindows you need to modify the X11 key map using xmodmap. It is not sufficient to just modify the console keyboard mapping. Use xmodmap to load the following keymap file (save in ~/.Xmodmap):
 +
 
 
<pre>
 
<pre>
 
remove Lock = Caps_Lock
 
remove Lock = Caps_Lock
Line 50: Line 152:
  
 
=== make these changes persistent ===
 
=== make these changes persistent ===
These keyboard settings are not persistent after a reboot.
+
 
Most Linuxes will load your ~/.Xmodmap file when you login
+
These keyboard settings are not persistent after a reboot. Most Linuxes will load your ~/.Xmodmap file when you login with xdm or gdm. Some don't. If not then you should add this line to your ~/.xsession and ~/.xinitrc files (after the shebang #!/bin/sh line):
with xdm or gdm. Some don't.
+
 
If not then you should add this line to your  
+
<pre>
~/.xsession and ~/.xinitrc files
+
[ -f ~/.Xmodmap ] && xmodmap ~/.Xmodmap
(after the shebang #!/bin/sh line):
+
</pre>
    [ -f ~/.Xmodmap ] && xmodmap ~/.Xmodmap
+
 
 +
== X11 Gnome/Ubuntu ==
 +
 
 +
Go to you main panel menu and select the following to get to the CapsLock as Ctrl option:
 +
 
 +
<pre>
 +
System
 +
  Preferences
 +
    Keyboard  --> ''opens "Keyboard Preferences" dialog''
 +
      Layouts tab
 +
        Layout Options...
 +
          Ctrl key position
 +
            Make CapsLock an additional Ctrl.
 +
</pre>

Latest revision as of 23:55, 14 August 2015


Caps Lock is stupid.

CapsLock is Satan

How often do you need to type in all caps? The CapsLock key may not be the most useless key on the keyboard -- I'd vote for the Print Screen key, but the CapsLock is certainly the most annoying key. Plus it is a big key and in prime real estate. This is a valuable set of attributes that should be reserved for a key that you hit often (Enter, Shift, Control, Delete, etc.).

On most PC keyboards the CapsLock key is where the Ctrl key should be. Whenever I get a new keyboard or laptop, I always pop off the key then take a razor blade and scrape off the "CapsLock" print. I then tweak my operating system so that it thinks this key is a Ctrl key. Some people like to make it an additional Esc key.

Some people believe that IBM took the original computer terminal keyboards and replaced the Ctrl key with the CapsLock key in order to make their first home computer, the IBM PC, appear more familiar to people who were accustomed to typewriters. This is false. In fact, the first IBM PC's had the Ctrl key to the left of the A. Later on, IBM released the IBM PC/AT with a CapsLock favored keyboard. Other people blame Microsoft because they pushed all those other useless keys on the world. There is also a bit of a myth that the old mainframe keyboards were consistent in putting the Ctrl key to the left of the A. Actually, the original PC keyboards were the anomaly. Prior to the PC most printing and display terminals had the CapsLock in the position where it is today. This includes classics such as the DEC VT52 and VT100 terminals; IBM data display terminals such as the IBM2741 and IBM5253. Many of IBM's early small computers that predate the IBM PC had a mix of keyboard styles. Many non-PC personal computers of the time had the CapsLock to the left of the A: TRS-80, BBC Micro, Osborne 1, Commodore Vic-20, and Commodore C=64. The Apple ][ series had the Ctrl key in the proper location to the left of the A key, but sadly, the original Macintosh did not -- it was afflicted with the CapsLock (and this was before the IBM PC/AT went to the Enhanced keyboard). The Atari computers had the proper Ctrl key, but their keyboards were horrible membrane things. The BeBox (BeOS) had CapsLock; Next had Ctrl.

NoCaps!

The following sections explains ways to turn the CapsLock key into something useful. I like to turn it into a Control key, so these examples foxus on that. Other reasonable keys that people redefine CapsLock as are Escape, and Backspace.

X.org X11 X-Windows

This is pretty much all you need in X to turns the CapsLock key into a Ctrl key. Note that this map is not persistent between logins, so you may want to add this to your ~/.bashrc or ~/.profile. In theory, there are a bunch of places you could put this that would seem more appropriate, such as /etc/X11/xorg.conf or /etc/X11/xorg.conf.d/00-keyboard.conf, but these are all handled inconsistently by the various display managers and Xserver startup sequences. It's just easier to put it in one of your dotfiles. Of course, this may cause confusion on multi-user shared systems, but how many people really run X11 that way?

setxkbmap -layout us -option ctrl:nocaps

Linux script

This nocaps script is part of my personal ~/bin/ directory.

#!/bin/sh

# This temporarily remaps the CapsLock key to a Control key.
# The keyboard will return to the previous settings after a
# reboot. The Linux console and the X Window system each
# handles keypresses separately, so each must be remapped
# separately. First remap the X keyboard since this does not
# require root access.

# Remap the CapsLock key to a Control key for
# the X Window system.
if type setxkbmap >/dev/null 2>&1; then
        setxkbmap -layout us -option ctrl:nocaps 2>/dev/null
fi

# You have to be root to remap the console keyboard.
if [ "$(id -u)" != "0" ]; then
        echo "This script is not running as root so"
        echo "the console CapsLock cannot be remapped."
        echo "Perhaps you forgot to run this under sudo."
        echo "Note that this problem does not effect X."
        echo "This only effects the consoles running on"
        echo "Alt-f1 through Alt-f6."
        exit 2
fi
# Remap the CapsLock key to a Control key for the console.
(dumpkeys | grep keymaps; echo "keycode 58 = Control") | loadkeys

Windows

Go here Sysinternals ctrl2cap.

This was the old URL: Sysinternals ctrl2cap.

Linux Console

Now it seems that everything is in /etc/default/keyboard or in /etc/default/console-setup. Edit one of these files, and find and modify the XKBOPTIONS line. It also seems that the console now uses the same X Window configuration.

XKBOPTIONS="ctrl:swapcaps"

You might need to run:

sudo dpkg-reconfigure -phigh console-setup

All the stuff under /etc/kbd/ is ignored if you have console-setup installed... It's all very confusing.

Older stuff

You just need to remap keycode 58 from "Caps_Lock" to "Control" then load using loadkeys. Many Linux distros actually have hooks in place to do this for you. This is the command that will do the trick -- if you run as root and if you are at a console.

(dumpkeys | grep keymaps; echo "keycode 58 = Control") | loadkeys

Or this equivalent command:

dumpkeys | sed 's/\s*58\s*=\s*Caps_Lock/ 58 = Control/' | loadkeys

If you get the following message then it means you didn't run `dumpkeys` or `loadkeys` as root:

Couldn't get a file descriptor referring to the console

On some systems I am seeing this now printed as CtrlL_Lock.

I put this in my Bash alias file:

alias nocaps='sudo dumpkeys | sed "s/\s*58\s*=\s*Caps_Lock/ 58 = Control/" | sudo loadkeys'

Most distros put these types of commands into a keymap file. The keymap files are stored in different places depending on your version of Linux. It is sometimes called defkeymap or remap.

Newer Ubuntu 8.10 Intrepid

Simply uncomment the line s/keycode 58 = Caps_Lock/keycode 58 = Control/; in this file:

/etc/kbd/remap

Ubuntu before 8.10 Intrepid

The keyboard maps in Ubuntu are stored here:

/usr/share/keymaps/i386/qwerty/defkeymap.kmap.gz

Red Hat Enterprise 4

On Red Hat Enterprise 4 the file is stored here:

/lib/kbd/keymaps/i386/qwerty/defkeymap.map.gz

old script to fix defkeymap on Ubuntu

The following script takes care of this for Ubuntu Linux:

#!/bin/sh
echo "transmogrify the Caps_Lock key into another Control key"
gunzip /usr/share/keymaps/i386/qwerty/defkeymap.kmap.gz
sed -i -e "s/Caps_Lock/Control/" /usr/share/keymaps/i386/qwerty/defkeymap.kmap
gzip /usr/share/keymaps/i386/qwerty/defkeymap.kmap
loadkeys -d

console-tools

This can also be done through console-tools. Edit '/etc/console-tools/remap' and uncomment 'keycode 58'.

X11

When running XWindows you need to modify the X11 key map using xmodmap. It is not sufficient to just modify the console keyboard mapping. Use xmodmap to load the following keymap file (save in ~/.Xmodmap):

remove Lock = Caps_Lock
keycode 0x42 = Control_L
add Control = Control_L

make these changes persistent

These keyboard settings are not persistent after a reboot. Most Linuxes will load your ~/.Xmodmap file when you login with xdm or gdm. Some don't. If not then you should add this line to your ~/.xsession and ~/.xinitrc files (after the shebang #!/bin/sh line):

[ -f ~/.Xmodmap ] && xmodmap ~/.Xmodmap

X11 Gnome/Ubuntu

Go to you main panel menu and select the following to get to the CapsLock as Ctrl option:

System
  Preferences
    Keyboard  --> ''opens "Keyboard Preferences" dialog''
      Layouts tab
        Layout Options...
          Ctrl key position
            Make CapsLock an additional Ctrl.