23

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 5

59

You can place this to your .zshrc file

export PS1="%d %% "

%d denotes the CWD

For more details go here for example

2
  • Simple and elegant! Commented Jan 13, 2017 at 19:46
  • My favorite is linux-like (username@host:PATH$) export PS1="%n@%m:%d$ " Commented Apr 10, 2020 at 18:16
12

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

enter image description here

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
  • 1
    The brackets cluttered is up a little but thanks great answer
    – dmgd
    Commented May 6, 2020 at 12:01
11

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.

0
2

If you have agnoster theme for ZSH then you could customize path length inside .zshrc file as shown below

  1. To show the full path

    prompt_dir() {
       prompt_segment blue black '%~'
    }
    

Example: /mnt/c/personal/repo/myprojects/alphagame $

  1. To show only the current directory instead of full path.

    prompt_dir() {
       prompt_segment blue black '%1~'
    }
    

Example: alphagame $

  1. Similarly to show only last 2 directories instead of full path

    prompt_dir() {
       prompt_segment blue black '%2~'
    }
    

Example: myprojects/alphagame $

1
  • Best one is the '%2~' :)
    – Wolfgang
    Commented Dec 27, 2021 at 23:03
1

Add ${PWD/#$HOME/~} to your PROMPT variable. Or just $PWD if you don't want it to show ~ for your home directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.