user3166042
user3166042

Reputation:

How to change prompt in iTerm2 in Mac OS?

I have installed oh-my-zsh and iterm2 on my Mac. Currently the command prompt shows up as currentdirectory@currenthost ~ How can I change it to just show current directory. I tried changing PS1 variable in .bashrc and .bash_profile file to export PS1 = " " but still the prompt remains the same. Any help is appreciated.

Upvotes: 12

Views: 21537

Answers (3)

T. Al Rashid
T. Al Rashid

Reputation: 913

Since the update to version 10.15 Catalina, macOS includes Z shell (zsh) as default instead of Bash in the Terminal app, therefore when you install iterm2, it will use any Zsh settings stored on the Zsh profile(dotfile).

Here are the steps to follow; paste the command, save and quit. Reopen iterm2 to view the changes.

By default in MacOS, the dotfile is not there so you'll have to create one.

  • Create the Zsh profile(dot file), I use nano editor but you can use any other of your choice
nano ~/.zshrc

The default Zsh prompt carries information like the username, machine name, and location starting in the user's home directory, so you can customize what to output on prompt:

  • To view only your username
PROMPT="%n:~$ "
  • To view only the working directory:
PROMPT="%1d:~$ "
  • To view only Time ( %T - 24hrs, %t - 12hrs or %* 24hrs+seconds )
PROMPT="%T:~$ "
  • To view only Date ( %D: yy-mm-dd format or %W: mm-dd-yy format )
PROMPT="%W:~$ "
  • To view only the $ symbol
PROMPT="~$ "
  • To view your Username and Date
PROMPT="%n:%W:~$ "
  • To view your Username and Time
PROMPT="%n:%T:~$ "

You can even add coloring to the relevant text. Ensure to enclose in the %F & %f color variables.

  • Cyan color
PROMPT="%F{cyan}%T%f:~$ "
  • Lastly, this is my choice, To view Time and working directory
PROMPT="%F{cyan}%T@%1d%f:~$ "

Upvotes: 16

Sahar Pk
Sahar Pk

Reputation: 531

I have tried the following in the iTerm2 + agnoster theme.By default the command prompt shows up as user@hostname.

You should modify the zsh's configuration file:

if you want to show the Home directopry as prompt name, then add these lines to ~/.zshrc:

prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$HOME"
  fi
}

if you want to show the user as prompt name, then add these lines to ~/.zshrc:

prompt_context() {
  if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
    prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
  fi
}

if you want to show the hostname as prompt name, then add these lines to ~/.zshrc:

 prompt_context() {
      if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
        prompt_segment black default "%(!.%{%F{yellow}%}.)%m"
      fi
    }

if you want to completely remove the name then add:

prompt_context() {}

Upvotes: 0

Rupert
Rupert

Reputation: 309

Add this line to ~/.zshrc and it will assume that user on iTerm startup. Then it won't display your username in the prompt.

DEFAULT_USER="your_user_name"

Upvotes: 5

Related Questions