Tweaking the Bash Shell
The Bourne Again
fish
and
zsh
are popular alternative shells.
bash
configuration for
Conditional Prompt Colors
The color root
privilege, blue of the standard user, and
white (an unmodified prompt) of a foreign system.
This is achieved by short circuiting the $PS1
(the primary
PS1
is not particularly
verbose.
~/.bash_profile
.
bash
PS1_USER='\[\e[0;34m\]\W\[\e[0m\] \[\e[0;34m\]\$\[\e[0m\] '
PS1_ROOT='\[\e[0;31m\]\W\[\e[0m\] \[\e[0;31m\]\$\[\e[0m\] '
PS1_SSHD='\[\e[0;32m\]\W\[\e[0m\] \[\e[0;32m\]\$\[\e[0m\] '
[ "$EUID" != 0 ] && export PS1="$PS1_USER";
[ "$EUID" = 0 ] && export PS1="$PS1_ROOT";
[ -n "$SSH_CLIENT" ] && [ "$EUID" != 0 ] && export PS1="$PS1_SSHD";
Exit Codes
Often there is a need to know the return or exit status of the last command.
This is extremely useful in varied situations. The variable $?
contains the
exit status of the last command.
In ~/.bash_profile
, the logic $(E=$? && [ "$E" != 0 ] && echo "$E ")
is
prefixed to the $PS1
. The exit status will be printed on a carriage return.
bash
PS1_USER='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;34m\]\W\[\e[0m\] \[\e[0;34m\]\$\[\e[0m\] '
PS1_ROOT='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;31m\]\W\[\e[0m\] \[\e[0;31m\]\$\[\e[0m\] '
PS1_SSHD='$(E=$? && [ "$E" != 0 ] && echo "$E ")\[\e[0;32m\]\W\[\e[0m\] \[\e[0;32m\]\$\[\e[0m\] '
[ "$EUID" != 0 ] && export PS1="$PS1_USER";
[ "$EUID" = 0 ] && export PS1="$PS1_ROOT";
[ -n "$SSH_CLIENT" ] && [ "$EUID" != 0 ] && export PS1="$PS1_SSHD";
Fuzzy Finder
The command line fuzzy finder fzf has
keybindings for
common operations in bash
and zsh
. This gives the
urxvt
. Simple Terminal (st
) is also rather
delightful and popular among the purists.
CTRL+R
) workflow.
Source the fuzzy finder’s keybindings in the ~/.bashrc
configuration. This
only works if the command fzf
has been installed on the
bash
[ -f '/usr/share/fzf/completion.bash' ] && . /usr/share/fzf/completion.bash
[ -f '/usr/share/fzf/key-bindings.bash' ] && . /usr/share/fzf/key-bindings.bash
[ -f '/usr/share/doc/fzf/examples/completion.bash' ] && . /usr/share/doc/fzf/examples/completion.bash
[ -f '/usr/share/doc/fzf/examples/key-bindings.bash' ] && . /usr/share/doc/fzf/examples/key-bindings.bash
Vi Mode
Bash has a built in readline vi
mode. This removes the tempting dependency on
the mouse for precise text manipulation. The prefixed +
and :
insert
and normal
modes respectively.
Pressing v
in normal mode invokes vim
on the current command.
Use the set
command in the ~/.inputrc
vi
mode prompt.
bash
set show-mode-in-prompt on
set vi-ins-mode-string "+ "
set vi-cmd-mode-string ": "
Use the set
command to enable vi
emacs
mode.
~/.bashrc
.
bash
set -o vi;
Readline Configuration
The shell’s readline
can be configured in the ~/.inputrc
file. Here’s a few
other readline
tweaks for a sane setup.
bash
# Set colors on completion results.
set colored-stats on
# Ignore case when using tab completion.
set completion-ignore-case on
# Show completion results on the first tab press.
set show-all-if-ambiguous on
# Avoid inserting tab completions in the middle of a word.
set skip-completed-text on
# Up and down reverse search will consider the currently typed command.
"\e[A": history-search-backward
"\e[B": history-search-forward