Administration
  Home arrow Administration arrow Page 3 - Customizing the User Environment in BS...
The Best Selling PC Migration Utility.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Customizing the User Environment in BSD
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2006-12-07

    Table of Contents:
  • Customizing the User Environment in BSD
  • Hack 2: Useful tcsh Shell Con
  • Hack 3: Create Shell Bindings
  • Hack 4: Use Terminal and X Bindings

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    PCmover - $15 Off with Coupon Code CJPH7Q

    Customizing the User Environment in BSD - Hack 3: Create Shell Bindings
    (Page 3 of 4 )

    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 Administration Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "BSD Hacks," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from the book BSD Hacks, written by Dru Lavigne (O'Reilly; ISBN: 0596006799). Check it out today at your favorite bookstore. Buy this book now.

       

    ADMINISTRATION ARTICLES

    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...
    - Dealing with Files and Filesystems
    - More Hacks for the User Environment in BSD
    - Personalizing the User Environment in BSD
    - Customizing the User Environment in BSD




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway