When using the terminal in a deep folder structure sometimes the prompt can take up most of the line. Is there any way in which I can trim the working directory? I know I can do

PS1="\W >"

to only print the current directory and not the full path, but is there a way to have something like:

/home/smauel/de...ther/folder >

Create a small python script which implements the desired trimming logic.

Example: ~/.short.pwd.py

import os
from socket import gethostname
hostname = gethostname()
username = os.environ['USER']
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)
if len(pwd) > 33:
    pwd = pwd[:10]+'...'+pwd[-20:] # first 10 chars+last 20 chars
print '[%s@%s:%s] ' % (username, hostname, pwd)

Now test it, from a terminal:

export PROMPT_COMMAND='PS1="$(python ~/.short.pwd.py)"'

If you are ok with the result just append the command to your ~/.bashrc.

  • Not to ask the obvious, but how exactly do we append the command to the ~/.bashrc? Would it just be pasting that last line at the bottom of the file? – FloatingRock Jul 4 '14 at 10:31
  • 1
    @FloatingRock correct. Simply add it to the .bashrc file. – Prefix Apr 16 '15 at 16:59
  • This is great! If you edit the python program, it automatically updates: sweet! – N3sh Dec 1 '15 at 12:36
  • cool! immediately stole this. – ferdy Feb 12 '16 at 16:52
  • Update that last print ... to print( ... ) to get this work almost as-is for python 3 too, great tip, thanks for sharing!! – Sean Mar 12 '18 at 15:34

If you are using bash4 (Ubuntu 9.10 and newer has bash4), the easiest option is to just set the PROMPT_DIRTRIM variable. e.g.:


For one similar to João Pinto's example, (that'll work in older bash versions and ensures that the path component is never longer than 30 characters), you could do something like this:

PS1='[\u@\h:$(p=${PWD/#"$HOME"/~};((${#p}>30))&&echo "${p::10}…${p:(-19)}"||echo "\w")]\$ '

Another way around that problem is to include a line break into PS1, so that the working directory and the actual prompt appear on separate lines, for example:

  • Similar to thiis, is to not change your PS1 prompt, but to just start your command with a \ and press Enter. This forces the command to begin on the next line with the PS2 prompt, which is usually > ... (I hadn't thought of it until I saw your suggestion :) – Peter.O Dec 16 '10 at 19:36

Add this to the bottom of your ~/.bashrc

split_pwd() {
        # Only show ellipses for directory trees -gt 3
        # Otherwise use the default pwd as the current \w replacement
        if [ $(pwd | grep -o '/' | wc -l) -gt 3 ]; then
                pwd | cut -d'/' -f1-3 | xargs -I{} echo {}"/../${PWD##*/}"

export PS1="\$(split_pwd) > "

Admittedly this could probably be cleaner, but I wanted to get a crack at it.

Expected output for directories more than three layers deep.

/home/chris/../Node Projects >

Expected output for directories from Desktop and back.

/home/chris/Desktop > 
/home/chris >  

I like this one most, PS1="[\W]\\$ "

  • OP mentioned they're not intersted in that. – wjandrea Mar 10 '17 at 23:41

This small addition to @joão-pinto's excellent answer adds in the virtual environment name when you run the workon command.

import os
from platform import node
hostname = node().split('.')[0]
username = os.environ['USER']
pwd = os.getcwd()
homedir = os.path.expanduser('~')
pwd = pwd.replace(homedir, '~', 1)

# check for the virtualenv
ve = os.getenv('VIRTUAL_ENV')

if ve:
    venv = '(`basename \"$VIRTUAL_ENV\"`)'
    venv = ''

if len(pwd) > 33:
    pwd = pwd[:10]+'...'+pwd[-20:] # first 10 chars+last 20 chars
print '%s[%s@%s:%s] ' % (venv, username, hostname, pwd)

Based on Cris Sullivan's answer, but keeping the ~ for the home folder

get_bash_w() {
  # Returns the same working directory that the \W bash prompt command
  echo $(pwd | sed 's@'"$HOME"'@~@')

split_pwd() {
  # Split pwd into the first element, elipsis (...) and the last subfolder
  # /usr/local/share/doc --> /usr/.../doc
  # ~/project/folder/subfolder --> ~/project/../subfolder
  if [ $(echo $W | grep -o '/' | wc -l) -gt $split ]; then
    echo $W | cut -d'/' -f1-$split | xargs -I{} echo {}"/../${W##*/}"
    echo $W

export PS1="\$(split_pwd) > "
  • Don't you still need to export PS1 here?? Looks great. – Chris Sullivan Jan 2 at 19:25
  • Yes, you still need to export PS1, as you detailed in your answer. I just add the working directory :-) – Manuel Jan 7 at 19:55

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.