Train your shell to run a command for you whenever you press a mapped key.
Have you ever listened to a Windows power user expound on the joys of hotkeys? Perhaps you yourself have been known to gaze wistfully at the extra buttons found on a Microsoft keyboard. Did you know that it’s easy to configure your keyboard to launch your most commonly used applications with a keystroke or two?
One way to do this is with thebindkeycommand, which is built into thetcshshell. As the name suggests, this command binds certain actions to
Standard key bindings
The^means hold down your Ctrl key. For example, press Ctrl and thenl, and you’ll clear your screen more quickly than by typingclear. Notice that it doesn’t matter if you use the uppercase or lowercase letter.
Creating a Binding
One of my favorite shortcuts isn’t bound to a key by default:complete-word-fwd. Before I do the actual binding, I’ll first check which keys are available:
dru@~:bindkey | grep undefined
Although it is possible to bind keys to numerical escape sequences, I don’t find that very convenient. However, I can very easily use that available Ctrl g. Let’s see what happens when I bind it:dru@~:bindkey "^G" complete-word-fwd
When I typed in that command, I knew something worked because my prompt returned silently. Here’s what happens if I now type ls -l /etc/, hold down the Ctrl key, and repeatedly press g:
ls -l /etc/COPYRIGHT
I now have a quick way of cycling through the files in a directory until I find the exact one I want. Even better, if I know what letter the file starts with, I can specify it. Here I’ll cycle through the files that start witha:
ls -l /etc/a
Once I’ve cycled through, the shell will bring me back to the letteraand beep.
If you prefer to cycle backward, starting with words that begin withzinstead ofa, bind your key tocomplete-word-backinstead.
When you usebindkey, you can bind any command the shell understands to any understood key binding. Here’s my trick to list the commands thattcshunderstands:
dru@~ man csh
And, of course, use bindkey alone to see the understood key bindings. If you just want to see the binding for a particular key, specify it. Here’s how to see the current binding for Ctrl-g:
What’s really cool is that you’re not limited to just the commands found inman csh. Thesswitch tobindkeyallows you to specify any string. I like to bind thelynxweb browser to Ctrl-w:
dru@~:bindkey -s "^W" "lynx\n"
I chose wbecause it reminds me of the World Wide Web. But why did I put\nafter thelynx? Because that tells the shell to press Enter for me. That means by simply pressing Ctrl-w, I have instant access to the Web.
Note that I overwrite the default binding for Ctrl-w. This permits you to make bindings that are more intuitive and useful for your own purposes. For example, if you never plan on doing whatever^Jdoes by default, simply bind your desired command to it.
There are many potential key bindings, so scrolling through the output ofbindkeyscan be tedious. If you only stick with “Ctrl letter” bindings, though, it’s easy to view your customizations with the following command:
dru@~:bindkey | head -n 28
As with all shell modifications, experiment with your bindings first by usingbindkey at the command prompt. If you get into real trouble, you can always log out to go back to the defaults. However, if you find some bindings you want to keep, make them permanent by adding yourbindkeystatements to your .cshrc file. Here is an example:
dru@~:cp ~/.cshrc ~/.cshrc.orig
Notice that I backed up my original .cshrc file first, just in case my fingers slip on the next part. I then used >> to append the echoed text to the end of .cshrc. If I’d used > instead, it would have replaced my entire .cshrc file with just that one line. I don’t recommend testing this on any file you want to keep.
Along those lines, setting:
will prevent the shell from clobbering an existing file if you forget that extra>in your redirector. You’ll know you just prevented a nasty accident if you get this error message after trying to redirect output to a file:
.cshrc: File exists.
blog comments powered by Disqus