2015-04-13 20:20:11 +01:00
---
created: 2008-08-05 17:48:29 +0200
2022-01-23 17:14:59 +00:00
layout: redirect
layout_old: default
redirect_to: https://blog.mbirth.de/archives/2008/08/05/prompt-with-2-directories.html
2015-04-13 20:20:11 +01:00
tags:
2022-01-23 17:14:59 +00:00
- know-how
- software
- linux
- shell
title: Prompt with 2 directories
toc: false
updated: 2009-02-20 00:32:43 +0100
2015-04-13 20:20:11 +01:00
---
2022-01-23 17:14:59 +00:00
2015-04-13 20:20:11 +01:00
bash
====
To only show the last 2 directories in prompt, there are two scripts at the [Muffin Research Labs ](http://muffinresearch.co.uk/archives/2007/09/25/showing-last-two-directories-of-pwd-in-bash-prompt/ ).
The best one would be this:
{% highlight bash %}
function PWD {
tmp=${PWD%/*/*};
[ ${#tmp} -gt 0 -a "$tmp" != "$PWD" ] && echo \<${PWD:${#tmp}+1} || echo $PWD;
}
{% endhighlight %}
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.
{% highlight bash %}
function PWD2 {
xpwd=${PWD/$HOME/"~"};
tmp=${xpwd%/*/*};
[ ${#tmp} -gt 0 -a "$tmp" != "$xpwd" ] & & echo «${xpwd:${#tmp}+1} || echo $xpwd;
}
{% endhighlight %}
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 `«` .
2022-01-23 17:14:59 +00:00
After that show 2 parts of `%~` .