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 the bindkey command, which is built into the tcsh shell. As the name suggests, this command binds certain actions to certain keys. To see your current mappings, simply type bindkey. The output is several pages long, so I've included only a short sample. However, you'll recognize some of these shortcuts from [Hack #1].
Standard key bindings "^A" -> beginning-of-line "^B" -> backward-char "^E" -> end-of-line "^F" -> forward-char "^L" -> clear-screen "^N" -> down-history "^P" -> up-history "^U" -> kill-whole-line Arrow key bindings down -> history-search-forward up -> history-search-backward left -> backward-char right -> forward-char home -> beginning-of-line end -> end-of-line
The ^ means hold down your Ctrl key. For example, press Ctrl and then l, and you'll clear your screen more quickly than by typing clear. Notice that it doesn't matter if you use the uppercase or lowercase letter.
1.4.1 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 "^G" -> is undefined "\305" -> is undefined "\307" -> is undefined <snip>
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 ls -l /etc/X11 ls -l /etc/aliases ls -l /etc/amd.map
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 with a:
ls -l /etc/a ls -l /etc/aliases ls -l /etc/amd.map ls -l /etc/apmd.conf ls -l /etc/auth.conf ls -l /etc/a
Once I've cycled through, the shell will bring me back to the letter a and beep.
If you prefer to cycle backward, starting with words that begin with z instead of a, bind your key to complete-word-back instead.
When you use bindkey, you can bind any command the shell understands to any understood key binding. Here's my trick to list the commands that tcsh understands:
dru@~ man csh /command is bound
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:
dru@~:bindkey "^G" "^G" -> complete-word-fwd
1.4.2 Specifying Strings
What's really cool is that you're not limited to just the commands found in man csh. The s switch to bindkey allows you to specify any string. I like to bind the lynx web browser to Ctrl-w:
dru@~:bindkey -s "^W" "lynx\n"
I chose w because it reminds me of the World Wide Web. But why did I put \n after the lynx? 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 ^J does by default, simply bind your desired command to it.
There are many potential key bindings, so scrolling through the output of bindkeys can 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 using bindkey 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 your bindkey statements to your .cshrc file. Here is an example:
dru@~:cp ~/.cshrc ~/.cshrc.orig dru@~:echo 'bindkey "^G" complete-word-fwd' >> ~/.cshrc
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.
1.4.3 See Also