Changing the prompt in the terminal is a quick way to personalize your workspace, making it easier to read and distinguish from the default appearance. The zsh shell, which became the default shell in macOS Catalina and later, allows for a highly customizable prompt using themes and plugins.

The zsh prompt, also known as the PS1 (Prompt String 1), is what you see at the beginning of each line in your terminal. By default, it displays the name of the user, the name of the machine, and the current directory. Customizing it can provide quick access to more information, reduce clutter, or simply add some fun visual aesthetics to your terminal sessions.

On macOS, the zsh configuration file is stored at ~/.zshrc, which is where you'd make changes to personalize the prompt. Before customizing, it's a good idea to backup this file.

Steps to change zsh prompt in macOS Terminal:

  1. Open the Terminal application.
  2. Check current Prompt setting.
    % echo $PROMPT
    %n@%m %1~ %# 
  3. Change the prompt by setting a new value for the PROMPT variable.
    % export PROMPT="[%*] %n@%m$ "
    [9:39:40] shakir@host$ 
    Character Explanation
    %n Username
    %m Hostname (up to the first dot)
    %M Full hostname
    %~ Current working directory
    %# Root indicator (#for root, $for a regular user)
    %% A literal %
    %D Current date
    %T Current time
    %t Current time in 24-hour format
    %* Current time in 12-hour format
    %d Current directory path
    %c The trailing component of the current directory
    %1~ Truncate directory to the last 1 part
    %2~ Truncate directory to the last 2 parts
    %3~ Truncate directory to the last 3 parts
    %L The current shell level
    %j The number of jobs managed by the shell
  4. Add the export command to your ~/.zshrc file to make it permanent.
    $ echo 'export PROMPT="[%*] %n@%m$ "' >> ~/.zshrc

    Manually edit the PROMPT declaration in ~/.zshrc if it already exist and for greater control.

  5. Apply the changes without restarting the terminal.
    $ source ~/.zshrc
  6. Verify the change by opening a new terminal session or window, and your prompt should reflect the new structure.
Discuss the article:

Comment anonymously. Login not required.