Master Your Linux Keyboard (And Fix Caps Lock Forever)
Want to get rid of the evil caps lock key without mutilating your keyboard? Want to give those silly Windows keys useful jobs, or put all those extra multi-media keys to work? Want to become a powerhouse keyboarding commando? Then come along and join the fun, because Linux has all kinds of good tools for taming wayward keyboards and increasing your efficiency. In this two-part series we're going to use xmodmap, XBindKeys, and KeyTouch to create custom keybindings for launching applications and running commands.
Exorcising Caps Lock
The placement of the caps lock key is a demonstration of malicious cunning. It's above the shift key and it's usually oversized, so it's way too easy to hit it when you don't want to, which for me is all the time. On a case-sensitive operating system it's not all that useful anyway. Unhappy users often resort to remedies like prying it off entirely or covering it with duct tape. You can do this if you're careful, but elite geeks resort to more sophisticated measures that do not mangle their nice keyboards. It's not the fault of the keyboards that manufacturers have giant Windows-sized blind spots, and as always, Linux makes lemonade out of lemons and provides useful alternatives.
This command reverses whatever position the caps lock key is in, so first make sure it is not on:
$ xmodmap -e "remove lock = Caps_Lock"
What if you do this when caps lock is on? One remedy is get used to typing like a dork: "dEAR jUPITERMEDIA, i WANT MORE STORIES ABOUT hp, ibm, AND dELL." Or you could fix it. First run this command:
$ xmodmap -e "add lock = Caps_Lock"
Then make sure it is not on, and re-run the "remove lock" command.
This won't survive a reboot, so put it in your ~/.bashrc file to make it permanent.
XBindKeys For Launching Applications
KDE and GNOME, window managers like IceWM and Fluxbox, and many applications come with their own set of prefab keybindings. You should investigate these before going on a customization spree, or you run the risk of creating conflicts. We're going to learn how to use XBindKeys. XBindKeys is not dependent on any particular desktop or window manager, and should run on pretty much any Linux or Unix system."
The easy way is to install xbindkeys-config along with xbindkeys, which gives you a typically plain GTK-based graphical configuration tool, as Figure 1 shows.
xbindkeys-config comes with Debian and all of its descendants. If you want an RPM and can't find one, get the source tarball from the Debian repository.
Launching graphical applications with root privileges is easy when you know a few slick tricks. In this example I have configured the left Windows key to launch a root file manager, and the right Windows key to launch a root text editor. Before you do anything else you must create a default configuration file with this command:
$ xbindkeys --defaults > $HOME/.xbindkeysrc
xbindkeys won't work without this. Take a look at it to get an idea of how to manually configure it; you might even make a copy to study, because xbindkeys-config will overwrite it.
Next, follow these steps to create a new keybinding:
- Hit the "New" button in xbindkeys-config
- Press "Get Key"
- Press a key or key combination on your keyboard
- On the "Action" line, type in your command
- Press "Run Action" to test it
- If it works, hit Apply and do more keybindings, or hit Save&Apply&Exit. Your changes will not be saved until you press the Save&Apply&Exit button
Figuring Out Commands
The tricky bit with xbindkeys-config is knowing the correct application launching commands. The simplest command is the program's name with no options, like kate or gimp or firefox. (Shame on Linux distributions and desktop environments that obscure the real application names - users will not faint at the sight of useful information.) Anything that works on the command line will work in xbindkeys-config. For launching graphical applications with rootly powers, or as any other user, you need gksu or kdesu. Using one of these gives you a graphical login window, like Figure 2.
gksu is a graphical front-end to both su and sudo. Just to add to the fun and confusion, so is gksudo. Making it even more fun is Ubuntu makes gksu behave like gksudo. So let's talk about this first.
Ubuntu, sudo, and Root
su is "switch to a different user". sudo is "execute a single command as another user, as configured in the /etc/sudoers file." Ubuntu's clever use of sudo is one of those "d'oh! Why didn't someone think of this years ago!" deals. Users who need occasional root privileges are simply added to the admin group. sudo protects the root password and logs all activity. Ubuntu doesn't even create a root login during installation. But you really need a root login for a number of reasons: some commands do not know how to handle it when sudo asks for a password, and it's more convenient when you have a lot of administrative chores to perform. Probably the most important reason to have a root login is the Ext2/3 filesystem reserves 5% of the filesystem for the root user. This gives you enough room to manuever and to rescue the system when some user process goes nuts and fills up your hard drive.
How do you make a root login on Ubuntu and all of its Buntu cousins? Easy, with sudo:
$ sudo passwd root
sudo is typically compiled with a 15-minute timeout, which means you can run a number of sudo commands without re-entering your password in that time. You can this change per-user or per-group by editing /etc/sudoers, which must be done with visudo:
$ sudo visudo $ Defaults:carla timestamp_timeout=30
Close and save with Ctrl-X and Y. -1 makes it infinite. Don't do this, for obvious security reasons.
Because Ubuntu makes gksu and gksudo both behave like sudo, how do you configure xbindkeys to run a command as "real" root? Use the -w switch, like this:
$ gksu -w kate
You can cancel the sudo timeout with this command:
$ sudo -k
This is helpful when you're testing your commands and they get stuck in sudo mode.
kdesu is a front-end to su, and by default switches to the "real" root user. So you need a root login on your system to use kdesu, like this:
$ kdesu kate
You may also switch to any user that you know the password for:
$ kdesu -u pinball kate
Come back next week to learn more about using xbindkeys with lots of nice examples, and a quick tour of KeyTouch for additional useful customizations to your multimedia keyboard.
- man 1 xmodmap
- man 5 sudoers
- man 1 gksu
- man 1 kdesu
- man 1 xbindkeys