33

In bash I have my PROMPT set like so

PS1="$(scutil --get ComputerName) \W\\$ "

Where I only see the computer name and only the name of the current directory that I am in, not the full path and a $ sign.

my-computer my-folder$

My question is how can I set up my zsh prompt to be just like the bash one. I've been googling around, but the solutions I've found are not exactly what I am looking for.

Anton Kastritskiy
  • 445
  • 1
  • 4
  • 7

4 Answers4

46

See the EXPANSION OF PROMPT SEQUENCES and SIMPLE PROMPT ESCAPES sections of the zsh manual page at man zshmisc.

This prompt string, PS1='%m %1d$ ', displays the machine name (%m) and the trailing component of the current path (%1d).

creidhne
  • 1,844
  • 3
  • 18
  • 22
34

The best version of the combinations that works for me is this:

PS1='%n@%m %~$ '

%n is the user logged in

%m is the machine name

%~ gives the path relative to HOME, if path begins with HOME.

This is how it is for me:

This is how it is for me

The man page has more info. I tried to put in simpler terms here.! :D

Hardhik
  • 441
  • 4
  • 3
  • 1
    Thanks! This is the true equivalent to the bash version. The other answer (%1d) only shows the lowest hierarchy in the directory structure. – JJLL Dec 22 '20 at 22:25
1

In my opinion

PS1='%~: '

anywhere in ~/.zshrc is the cleanest / simplest.

PS1='%~ $: '

is also pretty good.

Benji
  • 11
  • 2
  • Welcome to SuperUser! How is this better than the accepted answer from eight years ago? – DarkDiamond Aug 04 '22 at 11:31
  • It's structurally / basically the same, but provides two shortcuts for people who want the most simple / minimal exact code to make the Zsh prompt look like bash – Benji Aug 31 '22 at 19:36
0

To show only the directory followed by a % sign, e.g.:

/usr/local%

Put the following in ~/.zshrc (create the file if it doesn't exist):

PROMPT='%~%# '

%~ displays the current directory, but your HOME directory will be replaced by a ~:

/usr/local% cd
~% pwd
/Users/7stud
% cd erlang_programs 
~/erlang_programs% pwd
/Users/7stud/erlang_programs
~/erlang_programs% cd /opt/local
/opt/local% 

That keeps the prompt short and sweet.

The docs say that %# will display a % sign, but if you are using privileges, e.g. with the su command, then it will display a # sign. When I use sudo, it still displays a % sign.

7stud
  • 127
  • 5