Prompt with 2 directories

By | 5 Aug 2008

bash

To only show the last 2 directories in prompt, there are two scripts at the Muffin Research Labs.
The best one would be this:

function PWD {
    tmp=${PWD%/*/*};
    [ ${#tmp} -gt 0 -a "$tmp" != "$PWD" ] && echo \<${PWD:${#tmp}+1} || echo $PWD;
}Code language: JavaScript (javascript)

Add this to .bash_profile and replace \w by $(PWD) (note the parents!) in your PS1 line in that file.

A more comfort version which replaces the home directory by a tilde, is the following. I changed the function name to
PWD2 to highlight the difference to the normal PWD variable.

function PWD2 {
    xpwd=${PWD/$HOME/"~"};
    tmp=${xpwd%/*/*};
    [ ${#tmp} -gt 0 -a "$tmp" != "$xpwd" ] && echo «${xpwd:${#tmp}+1} || echo $xpwd;
}Code language: PHP (php)

zsh

The zsh has this feature built-in. Use this to show only 2 parts of the PWD:

%3(~.«.)%2~

This says: If %~ (the prompt incl. replacements such as ~ for homedir) has 3 or more parts, show «.
After that show 2 parts of %~.

One thought on “Prompt with 2 directories”

  1. Pingback: zsh – blog.mbirth.uk

Leave a Reply

Your email address will not be published. Required fields are marked *

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.