Changing Your zsh Prompt


Changing Your zsh Prompt

Your default prompt (the text on the screen in front of the place you type commands) may vary a bit, depending on your Unix system; you might see just a dollar sign ($), a dollar sign and date, or other information as outlined in the Setting Your zsh Prompt Promptly sidebar. You can set your prompt to include information that's handy for you.

You actually have multiple prompts:

  • The main prompt that you usually think of as the shell prompt. This prompt is called PS1 or just PROMPT.

  • A secondary prompt that you see when the system requires additional information to complete a command. Logically, this prompt is called PS2.

You can change either of these prompts using the following steps. You start by finding your prompt statement (Code Listing 8.8), then modifying it in your editor (Figure 8.2).

Figure 8.2. Edit your zsh prompt statement in the editor of your choice.


Code Listing 8.8. Use grep to search your configuration files for a zsh prompt statement.

jdoe@sulley ~ $ grep -i PROMPT ~/.z*; grep -i PS1 ~/.z* /home/jdoe/.zshrc:# configuration for keys umask PROMPT and variable /home/jdoe/.zshrc:# Set prompts /home/jdoe/.zshrc:PROMPT="%n@%m %3~ %(!.#.$) "   # default prompt jdoe@sulley ~ $ 

To change your zsh prompt:

1.

grep -i PROMPT ~/.z*; grep -i PS1  ~/.z* 




To begin, search through the configuration files located in your home directory and, if necessary, in the /etc directory, to find your prompt statement. It will look something like PROMPT="%n@%m %3~ %(!.#.$) "# default prompt, as shown in Code Listing 8.8.

The Setting Your zsh Prompt Promptly sidebar will help translate these symbols.

2.

cp ~/.zshrc ~/.zshrc-backup; vi  ~/.zshrc 




Make a backup copy. The ~/.zshrc file is a likely place for your prompt to be set, as it is only read when your shell is interactive.

Setting Your zsh Prompt Promptly

You can set your prompt to contain all sorts of information. The following list shows you what code to use to add certain kinds of information to your prompt (as well as help you translate the code in your existing prompt):

  • %n shows the userid of the current userthat's you.

  • %~ shows the current working directory with a path, using a ~ notation within your home directory.

  • %c shows the current directory without the path.

  • %t shows the time.

  • %w shows the date without the year.

  • %W shows the date with the year.

  • \n forces a new line, making the prompt appear split on two lines (you need single quotes around the prompt).

  • %m shows the host name of the computer (like the frazz and hobbes examples in this book).

  • %M shows the complete host name of the computer, including the domain name.


3.

PROMPT="%n %d $" 




For example, we often set our prompt to include two tidbits of information: the userid (as we have many different accounts, we can always use a reminder!) and the date (time flies when you're having fun, right?!). We're adding these bits of information instead of the existing default prompt, but saving the default with a # sign at the beginning, just in case (Figure 8.2).

4.

Save the file and exit from the editor.

5.

su - ejr

Log in again with your changed prompt to try it out.

Tips

  • Note the trailing space in the prompt code: PROMPT="%n %d $ ". This space can help make it easier to use the prompt because it keeps your commands from bumping into your prompt.

  • You can also set your prompt so that the information you set appears on one line and your actual prompt appears on the next (Code Listing 8.9). To do so, use single quotes ('') and a $ in the environment variable setting then a \n for the new line, as in PROMPT=$'%n %W\n $'. This forces the shell to treat the \n as a new line, not just as random characters in the prompt string.


Code Listing 8.9. Testing after you update your prompt is always a good idea.

jdoe@sulley ~ $ vi .zshrc jdoe@sulley ~ $ su - jdoe Password: jdoe /home/jdoe $ PROMPT = $ 'Top line  \nNext line Top line Next line $pwd /home/jdoe Top line Next line $ 




Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net