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).
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 ~ $
|
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.
| |
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 for the new line, as in PROMPT=$’%n %W $’. This forces the shell to treat the as a new line, not just as random characters in the prompt string.
jdoe@sulley ~ $ vi .zshrc jdoe@sulley ~ $ su - jdoe Password: jdoe /home/jdoe $ PROMPT = $ 'Top line → Next line' Top line Next line $pwd /home/jdoe Top line Next line $ |