How to you configure the command prompt in Linux to show current directory? I want to be able to put settings for this in the .zshrc file.
5 Answers
You can place this to your .zshrc
file
export PS1="%d %% "
%d
denotes the CWD
For more details go here for example
-
-
My favorite is linux-like (username@host:PATH$) export PS1="%n@%m:%d$ " Commented Apr 10, 2020 at 18:16
Like Jiri Kremser said, you can change the prompt using PS1
variable. For example, if you want to change prompt to something like this (show the current path relative to HOME dir):
use the following setting in .zshrc
,
export PS1="[%~]$ "
Then source .zshrc
to make the change take effect.
The official zsh doc on prompt variables can be found here.
-
1
I use "%20<...<%~%<<"
in my $PS1
, so that if a prefix is found in the current working directory, it is replaced by ~
: it works with $HOME
(replaced by ~
), home directories of users (replaced by ~user
), and directories defined by hash -d
(e.g., if one has hash -d foo=$HOME/path/to/dir
, then this directory is replaced by ~foo
). The %20<...<
and %<<
allows zsh to truncate the directory on the left side if it is too long, in order to avoid a too long prompt.
If you have agnoster
theme for ZSH
then you could customize path length inside .zshrc
file as shown below
To show the
full path
prompt_dir() { prompt_segment blue black '%~' }
Example: /mnt/c/personal/repo/myprojects/alphagame $
To show only the
current directory
instead offull path
.prompt_dir() { prompt_segment blue black '%1~' }
Example: alphagame $
Similarly to show only
last 2 directories
instead offull path
prompt_dir() { prompt_segment blue black '%2~' }
Example: myprojects/alphagame $
Add ${PWD/#$HOME/~}
to your PROMPT variable. Or just $PWD
if you don't want it to show ~ for your home directory.