How can I shorten my command line (bash) prompt?

249,170

Solution 1

To change it for the current terminal instance only

Just enter PS1='\u:\W\$ ' and press enter.


To change it "permanently"

In your ~/.bashrc, find the following section:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Remove the @\h, and replace the \w with an uppercase \W, so that it becomes:

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u:\W\$ '
fi

Save, exit, close terminal and start another to see the result.


Tons more options!

  • See here for a more extensive howto, with many more options
  • See this answer for using up a tiny Python script to set the prompt so that the shortening only occurs when you are deep in a directory structure.

Solution 2

Run this code in the current terminal

PROMPT_DIRTRIM=3

Now the bash prompt will show only the last 3 directory names. You can choose 1 to show only current directory. More information is available in the GNU documentation.

The effect:

/var/lib/apt/lists# PROMPT_DIRTRIM=3
/.../lib/apt/lists# 

If you want to make it permanently, add the following line to ~/.bashrc in the beginning:

PROMPT_DIRTRIM=3

or another number greater than zero.

Solution 3

This is my preferred prompt setting:

added in ~/.bashrc

PS1='[\u@\h \W]\$ '    

it looks like this:

[user@hostname dirname]$

(with a space after the $ sign)

Solution 4

Personally I prefer to see only current folder in the bash prompt. I can do this with the following command:

PS1='\W\$ '

If you want it to take effect after each start then add the above command into your ~/.bashrc.

Solution 5

I realize this is super old but since nobody suggested creating an alias I figured I'd post. Using Bash Prompt Escape Sequences I made an alias shorten

In ~/.bash_aliases here you will notice the $Blue var to set the prompt colour which you can omit or change based on preference I also clear the terminal when calling shorten.

alias c='clear'

alias shorten='PS1="$Blue$USER:\W$ "&& c'

To achieve the OP's desired prompt string:

alias shorten='PS1="$USER:\W$ "'

I have colours defined in ~/.bashrccopy and pasted from https://wiki.archlinux.org/index.php/Color_Bash_Prompt. On a side note what's with ansi code colours? I'm confused just looking at it.

Blue='\e[0;34m'         # Blue
Share:
249,170

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant over 1 year

    Currently it is:

    michael@Castle2012-Ubuntu-laptop01:~/Dropnot/webs/rails_v3/linker/spec/controllers$
    

    Outside of renaming my machine and directory structure...

    How could I make it be something more like:

    michael:controllers$
    
  • Michael Durrant
    Michael Durrant almost 10 years
    You can also have a lot of information... and then a carriage return at the end as in unix.stackexchange.com/q/88780/10043
  • yop83
    yop83 almost 10 years
    Just a sidenote: This requires Bash 4.
  • ctote
    ctote about 9 years
    Is there a way to make this global? In other words, if I sudo to another user, have this setting carry over, but only for myself (i.e., not for the user when they normally use their account)?
  • Michael Durrant
    Michael Durrant almost 9 years
    In order to have a shared .bashrc that works on both Linux and OSX I've since switched to unix.stackexchange.com/a/127800/10043
  • Michael Durrant
    Michael Durrant almost 9 years
    i.e. HOST='\033[02;36m\]\h' HOST=' '$HOST parse_git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; } TIME='\033[01;31m\]\t \033[01;32m\]' LOCATION=' \033[01;34m\]pwd | sed "s#(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/).*(/[^/]\‌​{1,\}/[^/]\{1,\})‌​;/\{0,1\}#\1_\2#g"' BRANCH=' \033[00;33m\]$(parse_git_branch)\[\033[00m\]\n\$ ' PS1=$TIME$USER$HOST$LOCATION$BRANCH PS2='\[\033[01;36m\]>'
  • Michael Durrant
    Michael Durrant almost 9 years
    but see the answer for actual code to use.
  • Kayote
    Kayote over 8 years
    A step by step guide for this would be so useful as I have no idea how to get to .bash_aliases. Thanks
  • Allie Carver
    Allie Carver over 8 years
    "." prefix indicates a hidden directory or file. The tilde "~" is short form of $HOME variable. So, "~/.bash_aliases" is just short form of "/home/$USER/.bash_aliases". To open ".bash_aliases" you can either open a terminal and type "gedit /home/$USER/.bash_aliases" or "gedit ~/.bash_aliases" or in your home directory type ctrl-h to show hidden files and open file directly. Hope that helps. You may want to do a Google search for useful aliases as well.
  • Andi Jay
    Andi Jay over 7 years
    How would you undo this? Let's say I run PS1='\u:\W\$ ' in the shell. What could I enter to restore the longer command prompt line?
  • Atul Khanduri
    Atul Khanduri over 7 years
  • Karim Samir
    Karim Samir about 7 years
    This didn't work for me on Ubuntu 16.04
  • Daniel Springer
    Daniel Springer about 7 years
    How would I add a space between each word? Also, can I color it?
  • Daniel Springer
    Daniel Springer about 7 years
    Why do I need the 'else' part? Also, "here" link appears to be broken. By the way, how can I choose the colors of the prompt's words?
  • Mayeenul Islam
    Mayeenul Islam over 6 years
    Worked for me perfectly on Ubuntu 16.04
  • phil294
    phil294 over 6 years
    You'd probably want to not use \[ or \]. Using \001 and \002 seems to prevent breaking the prompt on longer lines: stackoverflow.com/questions/24839271/…
  • dgoosens
    dgoosens almost 6 years
    Nice... I added a bach_alias for this (with a function) promptdir() { PROMPT_DIRTRIM=$1; } just to make live easier...
  • Mig82
    Mig82 over 5 years
    FYI, this bit PS1='\u:\W\$ ' also worked for me on Mac OS.
  • Prashant Adlinge
    Prashant Adlinge over 3 years
    Is there way to trim intermediate directory only i.e. keep the first directory and last directory ? e.g. /var/.../lists#
  • Ng Sek Long
    Ng Sek Long over 2 years
    How about this PS1='\[\033[01;34m\][\u @ \h \W]\[\033[00m\]\$ ', but adding space between each word looks ugly tho...