How-to customize the Bash prompt

In order to adapt a bit more my Debian Stable installation to my workflow, I’ve been tweaking the bash prompt. Simplicity and small line width are key here, because I often have tmux running with several panes in the same window and small panes with large one-liner prompts suck a lot! Everything feels crammed and hard to read. Just take a look at the image below to get an idea.

crammed bash prompt

After running a few commands in each pane with this prompt configuration, everything gets really crowded and confuse. For sanity safeguarding reasons and workflow improvement, the only thing to do is customize the prompt.

The Debian Stable bash prompt, shown on the image above, default value is:

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

To make it more useful, I changed the second line to this:

PS1="[3[00;32m]\u@\h[3[00m]:\w[3[00m]\n└─ [$(tput bold)]$(__git_ps1 '[%s] ')$: [$(tput sgr0)]"

All put together:

if [ "$color_prompt" = yes ]; then
		PS1="\[3[00;32m\]\u@\h\[3[00m\]:\w\[3[00m\]\n└─ \[$(tput bold)\]$(__git_ps1 '[%s] ')$: \[$(tput sgr0)\]"
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
fi
unset color_prompt force_color_prompt

And this is the result:

readable bash prompt

Not only I get a more readable prompt (and with “more room to breathe”, if you may), but I get the name of the current branch if I’m in a Git repository folder. This is a convenient feature to have if you work with this version control system.

There are a lot more ways one can configure the prompt. Both How-To Geek and Boolean World websites have nice introductory guides to get you started. The Arch Linux wiki entry about this is also a good read. Oh, and RTFM (Read The … Fine … Manual).

Bash: how-to improve history manipulation

By default, up and down keys allow you to navigate your bash history. Another option is the history built-in command and bash expansions (ex.: !2 runs the second command, oldest to newest, from your bash history).

There are also tools, like bash-it, that allow for better history manipulation, but this also adds a lot of other stuff, so it might make your .bashrc load slower. It will make your bash look good as hell too.

Another option for an awesome way to access your bash history is the following snippet, based on bash-it‘s history plugin:

if [ -t 1 ]
then
    bind '"\e[A": history-search-backward'
    bind '"\e[B": history-search-forward'
fi

With this, you only need to write part of a command, press the up arrow and it will complete it with the commands in bash history file that match to what you’ve written.

I’ve add it to the end of my .bashrc. Together with bash completion, it improves my workflow by a lot.

How to get good font rendering in Void Linux

This post lacks an example image because I’ve recovered it from a backup (with only text and no image) and I’m not using Void Linux at the moment. If you follow this guide and decide to share an image, I’ll gladly use it here and give you credit.

Read More