xarzu
xarzu

Reputation: 9479

How to you configure the command prompt in Linux to show current directory?

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.

Upvotes: 23

Views: 45508

Answers (5)

SridharKritha
SridharKritha

Reputation: 9611

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 $

Upvotes: 2

jdhao
jdhao

Reputation: 28349

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.

Upvotes: 12

Jiri Kremser
Jiri Kremser

Reputation: 12837

You can place this to your .zshrc file

export PS1="%d %% "

%d denotes the CWD

For more details go here for example

Upvotes: 59

vinc17
vinc17

Reputation: 3426

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.

Upvotes: 11

Eugene K
Eugene K

Reputation: 3457

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

Upvotes: 1

Related Questions