title: How can I shortern my command line prompt's current directory?
body: <p>I am using Ubuntu and I am tired of this long prompts in bash when I am working with some deep directory hierarchy. So, I would like to tweak my PS1 to shorten the working directory part the following way:</p>
  
  <p>Currently I have:</p>
  
  <pre><code>pajton@dragon:~/workspace/projects/project1/folder1/test$
  </code></pre>
  
  <p>and would like to have:</p>
  
  <pre><code>pajton@dragon:~/workspace/.../folder1/test$
  </code></pre>
  
  <p>The truncating would occur if len($PWD) passes given threshold. I want to always keep the first path component and at least one last path component. Then as space permits, add more components taking from the right.</p>
  
  <p>This is what I have currently. It works, but: 1) doesn't keep first path component, 2) doesn't respect cutting path at boundaries:</p>
  
  <pre><code>pwd_length=14
  pwd_symbol="..."
  newPWD="${PWD/#$HOME/~}"
  
  if [ $(echo -n $newPWD | wc -c | tr -d " ") -gt $pwd_length ]
  then
     newPWD="...$(echo -n $PWD | sed -e "s/.*\(.\{$pwd_length\}\)/\1/")"
  else
     newPWD="$(echo -n $PWD)"
  fi
  </code></pre>
  
  <p>And the result:</p>
  
  <pre><code>pajton@dragon:...sth/folder1/sample$ 
  </code></pre>
  
  <p>Thanks in advance!</p>