Home arrow Site Administration arrow Customizing the User Environment in BSD

Customizing the User Environment in BSD

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

Users of open source (http://opensource.org) Unix operating systems are an interesting breed. They like to poke under the surface of things, to find out how things work, and to figure out new and interesting ways of accomplishing common computing tasks. In short, they like to ďhack.Ē

While this book concentrates on the BSDs, many of the hacks apply to any open source operating system. Each hack is simply a demonstration of how to examine a common problem from a slightly different angle. Feel free to use any of these hacks as a springboard to your own customized solution. If your particular operating system doesnít contain the tool used in the solution, use a tool that does exist, or invent your own!

This chapter provides many tools for getting the most out of your working environment. Youíll learn how to make friends with your shell and how to perform your most common tasks with just a few keystrokes or mouse clicks. Youíll also uncover tricks that can help prevent command-line disasters. And, above all, youíll discover that hacking BSD is fun. So, pull your chair up to your operating system of choice and letís start hacking.

Hack 1: Get the Most Out of the Default Shell

Become a speed daemon at the command line.

For better or for worse, you spend a lot of time at the command line. If youíre used to administering a Linux system, you may be dismayed to learn thatbashis not the default shell on a BSD system, for either the superuser or regular user accounts.

Take heart; the FreeBSD superuserís defaulttcshshell is also brimming with shortcuts and little tricks designed to let you breeze through even the most tedious of tasks. Spend a few moments learning these tricks and youíll feel right at home. If youíre new to the command line or consider yourself a terrible typist, read on. Unix might be a whole lot easier than you think.

NetBSD and OpenBSD also ship with the C shell as their default shell. However, it is not always the same tcsh, but often its simpler variant, csh, which doesnít support all of the tricks provided in this hack.

However, both NetBSD and OpenBSD provide atcshpackage in their respective package collections.

History and Auto-Completion

I hate to live without three keys: up arrow, down arrow, and Tab. In fact, you can recognize me in a crowd, as Iím the one muttering loudly to myself if Iím on a system that doesnít treat these keys the way I expect to use them.

tcshuses the up and down arrow keys to scroll through your command history. If there is a golden rule to computing, it should be: ďYou should never have to type a command more than once.Ē When you need to repeat a command, simply press your up arrow until you find the desired command. Then, press Enter and think of all the keystrokes you just saved yourself. If your fingers fly faster than your eyes can read and you whiz past the right command, simply use the down arrow to go in the other direction.

The Tab key was specifically designed for both the lazy typist and the terrible speller. It can be painful watching some people type out a long command only to have it fail because of a typo. Itís even worse if they havenít heard about history, as they think their only choice is to try typing out the whole thing all over again. No wonder some people hate the command line!

Tab activates auto-completion. This means that if you type enough letters of a recognizable command or file,tcshwill fill in the rest of the word for you. However, if you instead hear a beep when you press the Tab key, it means that your shell isnít sure what you want. For example, if I want to runsockstat and type:

  % so

then press my Tab key, the system will beep because multiple commands start with so. However, if I add one more letter:

  % soc

and try again, the system will fill in the command for me:

   % sockstat

Editing and Navigating the Command Line

There are many more shortcuts that can save you keystrokes. Suppose Iíve just finished editing a document. If I press my up arrow, my last command will be displayed at the prompt:

  % vi mydocs/today/verylongfilename

Iíd now like to double-check how many words and lines are in that file by running this command:

   % wc mydocs/today/verylongfilename

I could pound on the backspace key until I get to theviportion of the command, but it would be much easier to hold down the Ctrl key and pressa. That would bring me to the very beginning of that command so I could replace theviwithwc. For a mnemonic device, remember that just asais the first letter of the alphabet, it also represents the first letter of the command at atcsh prompt.

I donít have to use my right arrow to go to the end of the command in order to press Enter and execute the command. Once your command looks like it should, you can press Enter. It doesnít matter where your cursor happens to be.

Sometimes you would like your cursor to go to the end of the command. Letís say I want to run the word count command on two files, and right now my cursor is at the firstcin this command:

  % wc mydocs/today/verylongfilename

If I hold down Ctrl and presse, the cursor will jump to the end of the command, so I can type in the rest of the desired command. Remember thateis for end.

Finally, what if youíre in the middle of a long command and decide youíd like to start from scratch, erase what youíve typed, and just get your prompt back? Simply hold down Ctrl and pressufor undo.

If you work in the Cisco or PIX IOS systems, all of the previous tricks work at the IOS command line.

Did you know that thecd command also includes some built-in shortcuts? You may have heard of this one: to return to your home directory quickly, simply type:

  % cd

Thatís very convenient, but what if you want to change to a different previous directory? Letís say that you start out in the /usr/share/doc/en_US. ISO8859-1/books/handbook directory, then use cd to change to the /usr/ X11R6/etc/X11 directory. Now you want to go back to that first directory. If youíre anything like me, you really donít want to type out that long directory path again. Sure, you could pick it out of your history, but chances are you originally navigated into that deep directory structure one directory at a time. If thatís the case, it would probably take you longer to pick each piece out of the history than it would be to just type the command manually.

Fortunately, there is a very quick solution. Simply type:

  % cd -

Repeat that command and watch as your prompt changes between the first and the second directory. What, your prompt isnít changing to indicate your current working directory? Donít worry, ďUseful tcsh Shell Configuration File OptionsĒ [Hack #2] will take care of that.

Learning from Your Command History

Now that you can move around fairly quickly, letís fine-tune some of these hacks. How many times have you found yourself repeating commands just to alter them slightly? The following scenario is one example.

Remember that document I created? Instead of using the history to bring up my previous command so I could edit it, I might have found it quicker to type this:

  % wc !$
  wc mydocs/today/verylongfilename 
       19         97        620 mydocs/today/verylongfilename
 

The!$tells the shell to take the last parameter from the previous command. Since that command was:

  % vi mydocs/today/verylongfilename

it replaced the!$in my new command with the very long filename from my previous command.

The!(or bang!) character has several other useful applications for dealing with previously issued commands. Suppose youíve been extremely busy and have issued several dozen commands in the last hour or so. You now want to repeat something you did half an hour ago. You could keep tapping your up arrow until you come across the command. But why search yourself when!can search for you?

For example, if Iíd like to repeat the commandmailstats, I could give!enough letters to figure out which command to pick out from my history:

  $ !ma

 !will pick out the most recently issued command that begins withma. If I had issued amancommand sometime aftermailstats command,tcshwould find that instead. This would fix it though:

  % !mai

If youíre not into trial and error, you can view your history by simply typing:

  % history

If youíre really lazy, this command will do the same thing:

  % h

Each command in this history will have a number. You can specify a command by giving !the associated number. In this example, Iíll asktcshto reissue themailstatscommand:

  % h
    165  16:51 mailstats
    166  16:51 sockstat
    167  16:52 telnet localhost 25
    168  16:54 man sendmail 
  % !165

Silencing Auto-Complete

The last tip Iíll mention is for those of you who find the system bell irritating. Or perhaps you just find it frustrating typing one letter, tabbing, typing another letter, tabbing, and so on until auto-complete works. If I type:

  % ls -l b

 then hold down the Ctrl key while I pressd:

  backups/ bin/ book/ boring.jpg
  ls -l b

Iíll be shown all of thebpossibilities in my current directory, and then my prompt will return my cursor to what Iíve already typed. In this example, if I want to view the size and permissions of boring.jpg, Iíll need to type up to here:

  % ls -l bor

before I press the Tab key. Iíll leave it up to your own imagination to decide what the d stands for.

See Also

  • man tcsh


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