Make the shell a friendly place to work in.
Now that youíve had a chance to make friends with the shell, letís use its configuration file to create an environment youíll enjoy working in. Your prompt is an excellent place to start.
Making Your Prompt More Useful
The defaulttcshprompt displays%when youíre logged in as a regular user andhostname#when youíre logged in as the superuser. Thatís a fairly useful way to figure out who youíre logged in as, but we can do much better than that.
Each user on the system, including the superuser, has a .cshrc file in his home directory. Here are my current prompt settings:
dru@~:grep prompt ~/.cshrc
That isnít the defaulttcshprompt, as Iíve been using my favorite customized prompt for the past few years. The possible prompt formatting sequences are easy to understand if you have a list of possibilities in front of you. That list is buried deeply withinman cshrc, so hereís a quick way to zero in on it:
Here Iíve used the / to invoke the manpage search utility. The search string prompt may include brings you to the right section, and is intuitive enough that even my rusty old brain can remember it.
If you compare the formatting sequences shown in the manpage to my prompt string, it reads as follows:
set prompt = "%B%n@%~%b: "
Thatís a little dense. Table 1-1 dissects the options.
Table 1-1. Prompt characters
Table 1-1. Prompt characters (continued)
With this prompt, I always know who I am and where I am. If I also needed to know what machine I was logged into (useful for remote administration), I could also include %M or %m somewhere within the prompt string.
Your prompt is an example of a shell variable. There are dozens of other shell variables you can set in .cshrc. My trick for finding the shell variables section in the manpage is:
As the name implies, shell variables affect only the commands that are built into the shell itself. Donít confuse these with environment variables, which affect your entire working environment and every command you invoke.
If you take a look at your ~/.cshrc file, environment variables are the ones written in uppercase and are preceded with the setenv command. Shell variables are written in lowercase and are preceded with the set command.
You can also enable a shell variable by using thesetcommand at your command prompt. (Useunsetto disable it.) Since the variable affects only your current login session and its children, you can experiment with setting and unsetting variables to your heartís content. If you get into trouble, log out of that session and log in again.
If you find a variable you want to keep permanently, add it to your ~/.cshrc file in the section that contains the defaultsetcommands. Letís take a look at some of the most useful ones.
If you enjoyed Ctrl-d from ďGet the Most Out of the Default ShellĒ [Hack #1], youíll like this even better:
Now whenever you use the Tab key and the shell isnít sure what you want, it wonít beep at you. Instead, the shell will show you the applicable possibilities. You donít even have to press Ctrl-d first!
The next variable might save you from possible future peril:
Iíll test this variable by quickly making a test directory and some files:
Then, Iíll try to remove the files from that test directory:
Since my prompt tells me what directory Iím in, this trick gives me one last chance to double-check that I really am deleting the files I want to delete.
If youíre prone to typos, consider this one:
This is how the shell will respond to typos at the command line:
Pressingywill correct the spelling and execute the command. Pressingnwill execute the misspelled command, resulting in an error message. If I presse, I can edit my command (although, in this case, it would be much quicker for the shell to go with its correct spelling). And if I completely panic at the thought of all of these choices, I can always pressato abort and just get my prompt back.
If you like to save keystrokes, try:
Youíll never have to typecdagain. Instead, simply type the name of the directory and the shell will assume you want to go there.
blog comments powered by Disqus