Customizing the zsh prompt on macOS allows for a more personalized and efficient terminal experience. By modifying the prompt, you can display key information such as your username, machine name, or current directory in a way that suits your workflow. This can help reduce screen clutter and improve the clarity of your terminal sessions.

The default zsh prompt, also known as PS1, typically shows basic details. However, it is highly customizable, allowing you to add or remove elements as needed. Adjusting the prompt can make it easier to access important information or create a simpler, more aesthetically pleasing terminal layout.

All customizations are made in the ~/.zshrc file, where you can set the desired prompt structure. Back up this file before making changes to avoid losing your current setup. Understanding the different placeholders and variables used in zsh will help you achieve the desired customization.

Steps to change zsh prompt in macOS Terminal:

  1. Open the Terminal application.
  2. Display the current prompt settings.
    % 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 immediately by sourcing the ~/.zshrc file.
    $ source ~/.zshrc
  6. Open a new terminal session to verify the updated prompt structure.
Discuss the article:

Comment anonymously. Login not required.