Short Prompt in Bash and ZSH

I strive to keep my files well organized so that it’s easy to find a file when I need it; one consequence of this organization is that sometimes it’s necessary to navigate through several subdirectories to get where I want.

For example, for a simple access to a book, I have to run the following commands:

julio@julio-acer ~  $ cd /media/externo/Documentos/Ebooks/Programacao/Lisp/Peter Seibel/Practical Common Lisp/
julio@julio-acer /media/externo/Documentos/Ebooks/Programacao/Lisp/Peter Seibel/Practical Common Lisp  $ okular Practical Common Lisp.pdf &

Finding and navigating to the correct folder is not complicated at all by combining tab completion, autojump plugins (e.g., fasd), and aliases in my ~/.bashrc that take me directly to the desired folder:

alias docs='cd /media/externo/Documentos'

The issue here is that the command line prompt displays the full path of the folder I’m in; in this case, it’s only 8 directories with short names, but in many cases, it’s necessary to go into even deeper folders with long names, causing the path to take up the entire screen.

Bash

There is a system variable that “trims” the display of this path, limiting it to a maximum number of directories shown. This variable is PROMPT_DIRTRIM. Just export it equal to the maximum number of folders to be displayed (3 is a good number) and the rest of the path will be replaced by ....

Example:

julio@julio-acer ~  $  cd /media/externo/Documentos/Ebooks/Programacao/Lisp/Peter Seibel/Practical Common Lisp/
julio@julio-acer /media/externo/Documentos/Ebooks/Programacao/Lisp/Peter Seibel/Practical Common Lisp $  export PROMPT_DIRTRIM=3
julio@julio-acer .../Lisp/Peter Seibel/Practical Common Lisp  $  pwd
/media/externo/Documentos/Ebooks/Programacao/Lisp/Peter Seibel/Practical Common Lisp
julio@julio-acer .../Lisp/Peter Seibel/Practical Common Lisp  $

To make this change permanent, add the following line to ~/.bashrc:

export PROMPT_DIRTRIM=3

ZSH

For ZSH, there is no $PROMPT_DIRTRIM variable like in BASH, but we can achieve a similar result by editing the $PROMPT variable (=$PS1):

julio@julio-acer /media/externo/Documentos/Programacao/Web/comp/www/projetos/Sites/gera_curriculo> pwd
/media/externo/Documentos/Programacao/Web/comp/www/projetos/Sites/gera_curriculo
julio@julio-acer /media/externo/Documentos/Programacao/Web/comp/www/projetos/Sites/gera_curriculo> PROMPT='%n@%m %{$fg[$user_color]%}%3c%{$reset_color%}%(!.#.>) '
julio@julio-acer projetos/Sites/gera_curriculo> pwd
/media/externo/Documentos/Programacao/Web/comp/www/projetos/Sites/gera_curriculo

The secret is in %3c, which displays only the last 3 directories.

To make this change default, simply export this variable in ~/.zshrc:

export PROMPT='%n@%m %{$fg[$user_color]%}%3c%{$reset_color%}%(!.#.>) '

The file I used as an example is one of the best introductory books to learn Common Lisp and can be consulted for free at http://gigamonkeys.com/book/

Julio Batista Silva
Julio Batista Silva
Data Engineer

I’m a computer engineer passionate about science, technology, photography, and languages. Currently working as a Data Engineer in Germany.

comments powered by Disqus