The Computer Oracle

Zsh prompt with current working directory

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 Zsh Prompt With Current Working Directory
00:35 Accepted Answer Score 49
00:58 Answer 2 Score 36
01:27 Answer 3 Score 1
02:08 Answer 4 Score 1
02:22 Thank you

--

Full question
https://superuser.com/questions/1108413/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#bash #terminal #zsh #iterm2 #ohmyzsh

#avk47



ACCEPTED ANSWER

Score 49


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).




ANSWER 2

Score 36


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




ANSWER 3

Score 1


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.




ANSWER 4

Score 1


In my opinion

PS1='%~: '

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

PS1='%~ $: '

is also pretty good.