Customize Your Terminal Prompt - SEIR-59/course-wiki GitHub Wiki

The standard prompt, which shows the logged in user and the computer name, is so much information and wasted space for most of us. So, here is how to change that.

Be sure you are in your home directory when doing this. To get there, just type ‘cd’ and return at the prompt.

Create Your Customization File (.zshrc):

(you’ll do this once, and then edit this file anytime you wish to add or change any terminal customizations).

From your home directory in Terminal, type:

touch .zshrc

Add the PROMPT customization to the .zshrc file

1. Open that newly created file in the editor of your choice

  • if you have installed the ‘code’ command through VSCode, you can type code .zshrc

2. Add the text below to the file*:

PROMPT=" "

*it should live on it's own line, as should all future customizations

3. Between the quotes, type what you want your prompt to display

Accepted input

  • text (WYSIWYG): will output the text as written
  • % flags: insert any combination of the following data (for more, check the source links below)
    • %n - $USERNAME
    • %m - The hostname up to the first ‘.’
    • %. - Current directory (only the folder you are currently in)
    • %~ - displays the full directory, and if you are in your user folder, displays ~ instead of username

4. SAVE your .zshrc file

To see your changes, open a new Terminal window/tab.

To revert to the default prompt, simply delete the PROMPT line in your .zshrc file.

EXAMPLE:

To set your prompt to display the current folder, followed by the prompt /:

The prompt line in the .zshrc file should read

1  PROMPT='%./: '

Which will make your prompt appear as follows:

"Desktop/: "

Advanced Settings

Colors

Wrap the text you wish to color with %F{123} and %f (where '123' is a 3 digit RGB which you can find here

  • Example: to display the current directory as red: %F{088}%~%f

Inserting a newline

This is usually more difficult in most areas of code, but it's so simple, it may feel difficult. Simply do what you want: enter a new line (return) and type your PROMPT code over two (or more) lines. So if you wanted to say "Welcome to Harry's Terminal! []: ", you would do:

1  PROMPT="Welcome to Harry's Terminal!
2  ||: "

Advanced Example Showing your Current Directory with Colors, followed by a new line with the clean prompt

image of terminal prompt with customizations

The Code:

1  PROMPT="
2  You are in: %F{034}%~/%f
3  
4  ||: "

Sources

http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html_

⚠️ **GitHub.com Fallback** ⚠️