Home arrow Site Administration arrow Page 3 - Customizing the User Environment in BSD

Hack 3: Create Shell Bindings - Administration

If you use an open source operating system, you probably have a few favorite hacks that you like to apply to make things run more smoothly. This article, the first of three parts, focuses on some good hacks for customizing the user environment. It is excerpted from chapter one of the book BSD Hacks, written by Dru Lavigne (Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media; ISBN: 0596006799).

TABLE OF CONTENTS:
  1. Customizing the User Environment in BSD
  2. Hack 2: Useful tcsh Shell Con
  3. Hack 3: Create Shell Bindings
  4. Hack 4: Use Terminal and X Bindings
By: O'Reilly Media
Rating: starstarstarstarstar / 4
December 07, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
certain keys. To see your current mappings, simply typebindkey. The output is several pages long, so I’ve included only a short sample. However, you’ll recognize some of these shortcuts from “Get the Most Out of the Default Shell” [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 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
 
"^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 witha:

  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 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
  /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

Specifying Strings

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  
  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:

  set noclobber

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.

See Also

  • man tcsh 
  •  “Useful tcsh Shell Configuration File Options”
     [Hack #2]



 
 
>>> More Site Administration Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: