Home > Mobile >  How to render directory basename in ZSH terminal?
How to render directory basename in ZSH terminal?

Time:01-11

Per the Mac OS update to Monterey it indicated the new shell was ZSH:

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.

After updating I realize that .bash_profile is no longer used. After research I found I can reference .bash_profile so I can still use my aliases with adding the line:

source ~/.bash_profile

The difference I've found for the terminal is it doesn't use PS1 but uses PROMPT. After changing:

export PS1='vader ~/${PWD/*\/} '

to

PROMPT='vader %~%# '

it renders the full path. In the .zshrc file how can I specify just the ability to render just the current directory or basename, so if I have the path of github/project/node_modules the terminal would be:

vader ~/node_modules%

CodePudding user response:

Try this:

PROMPT='vader %1~%# '

Now when the current directory is /Users/vader/github/project/node_modules, the prompt will display as vader node_modules% ; go up a directory and it will become vader project% .

Note that this does not include the ~/ from your prior PS1 prompt. In the shell, tilde is used as a shortcut for the home directory (e.g. /Users/vader), and is probably not the best thing to use as an indication that the leading part of the path has been omitted. Square braces are often used for that: PROMPT='vader []%# '

There's a list of available percent substitutions in the EXPANSION OF PROMPT SEQUENCES section of the zshmisc man page.

  •  Tags:  
  • Related