title: Collapse directories in zsh prompt
body: <p>I am using the following function to generate my <code>zsh prompt</code>:</p>
  
  <pre><code>_sudar_collapsed_wd() {
    echo $(pwd | perl -pe "
     BEGIN {
        binmode STDIN,  ':encoding(UTF-8)';
        binmode STDOUT, ':encoding(UTF-8)';
     }; s|^$HOME|~|g; s|/([^/])[^/]*(?=/)|/\$1|g
  ")
  }
  </code></pre>
  
  <p>Basically this converts a path like <code>/Users/sudar/dir1/dir2/dir3/dir4</code> into <code>~/D/c/d/d/d/dir4</code> by collapsing all parent directories except the current one.</p>
  
  <p>But I don't want to collapse the last 3 directories in the path. So <code>/Users/sudar/dir1/dir2/dir3/dir4</code> becomes <code>~/D/c/d/dir2/dir3/dir4</code></p>
  
  <p>The actual substitution happens in this regular expression:  <code>s|/([^/])[^/]*(?=/)|/\$1|g</code>.</p>
  
  <p>How to change this regular expression, so that it doesn't substitute the last 3 directories in the path?</p>