/bashrc-inputrc

My literate, frameworkless bashrc and inputrc

Primary LanguageShellMIT LicenseMIT

My literate, frameworkless bashrc and inputrc

Table of Contents

Introduction

After configuring my zshrc, I decided to also configure my bashrc and inputrc, as sometimes I need to switch over to bash, and I wanted thus to customize that environment to my tastes as well. I make my config of these literate, as I like commenting on stuff.

How the magic is done

This time I decided to put the configuration for both .bashrc and .inputrc in one place. I found that I could do so by setting the tangle parameters under each heading I wanted to export to a file, like so (see here):

:PROPERTIES:
:header-args: :tangle .bashrc
:END:

Afterwards, all headings that have such parameters are exported to different files upon tangling, depending on the filename defined in the :tangle section.

bashrc

Preliminaries

Disable command-not-found

On my system, in addition to the .bashrc file, also are loaded the contents of /etc/bash.bashrc before the .bashrc file itself, a behavior configured at compile time in Debian and derivatives of it (e.g. Ubuntu), and thus unavoidable. The main thing /etc/bash.bashrc loads is the command-not-found function which proposes names of packages from the repos when it sees a command it doesn’t recognise, which might help others, but I’m not too fond of it. Thus here I disable this by overriding the function command_not_found_handle used by the conditional that detects if the command-not-found script is on the system. In particular, I copied the action it does when this command isn’t found on both locations where it could be, and modified the message to be in my native language, as this was the only positive part of said script, that is that it translated the command-not-found string according to the user’s locale.

function command_not_found_handle {
		   printf "%s: η εντολή δε βρέθηκε\n" "$1" >&2
		   return 127
}

Enable bash completions

Here I enable the code for bash completion of various commands. This conditional was also included in the .bashrc installed by the system, a backup copy of which is found at /etc/skel/.bashrc, as the files from there are deployed in the home directory of each new user (see /etc/skel - LINFO and ’~/.bashrc’ vs ’ etc/skel.bashrc’. Why are there two ‘.bashrcs’? - Ask Ubuntu).

if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

Bootstrap the prompt

I’m using the polyglot prompt, so I need to download and source it. Here I use a conditional to automate the downloading of the code for the prompt (this requires wget). Immediately afterwards the prompt is sourced.

# polyglot prompt
# download it
if [[ ! -f $HOME/polyglot.sh ]]; then
  wget https://raw.githubusercontent.com/agkozak/polyglot/master/polyglot.sh
fi
# source it
source $HOME/polyglot.sh

Don’t show the editing mode on the prompt

The polyglot prompt shows the current editing mode in the beginning of the prompt, a behavior that is useful when someone is using the vi editing mode, but which I didn’t want, as I use the emacs editing mode (and don’t plan to switch to vi mode) and thus having the @ symbol at my prompt was undesired. The culprit for this behavior is this line, which normally belongs in .inputrc without the bind part, but since it doesn’t work when I put it there, I disable the showing of the editing mode here.

bind 'set show-mode-in-prompt off'

Colored man pages

See Russell Parker | Adding Colors to man.

export MANROFFOPT='-c'
export LESS_TERMCAP_mb=$(tput bold; tput setaf 2)
export LESS_TERMCAP_md=$(tput bold; tput setaf 1)
export LESS_TERMCAP_me=$(tput sgr0)
export LESS_TERMCAP_so=$(tput bold; tput setaf 3; tput setab 4)
export LESS_TERMCAP_se=$(tput rmso; tput sgr0)
export LESS_TERMCAP_us=$(tput bold; tput setaf 2)
export LESS_TERMCAP_ue=$(tput rmul; tput sgr0)
export LESS_TERMCAP_mr=$(tput rev)
export LESS_TERMCAP_mh=$(tput dim)

Set terminal title

Setting the terminal title is much harder on bash than on zsh, because bash lacks functions that make this comparatively easy to on zsh. The easiest way I found to implement something like that is here, but the downside is that it then uses the command last used in the previous session as a title when a new terminal session is started.

trap 'echo -ne "\033]2;$(history 1 | sed "s/^[ ]*[0-9]*[ ]*//g")\007"' DEBUG

Custom functions

Custom function to create a directory and cd into it (found from here).

mkcd() { mkdir "$1"; cd "$1"; }

History file size settings

HISTSIZE=10000
HISTFILESIZE=50000

Options

Disable using ctrl-d to exit the shell. Set a high margin just to make sure.

export IGNOREEOF=100

Disable flow control, as to be able to use Ctrl-s to search forward in the history (see here).

stty -ixon

Don’t save in history commands that are the same as the previous command.

HISTCONTROL=ignoredups

Append to the history file, don’t overwrite it.

shopt -s histappend

Automatically cd when the target is a directory without having to precede the target with cd.

shopt -s autocd

Correct minor errors in the spelling of a directory component in a cd command.

shopt -s cdspell

Aliases

alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias ll='ls -lh'
alias la='ls -lah'
alias termclock="tty-clock -b -c -C 6 -f \"%A %d/%m/%y\" -B -a 100000000 -d 0"

Settings for external programs

Setting for less, so that the output of git-log won’t remain printed to my terminal.

export LESS=-R

Enable true color for the micro text editor

export MICRO_TRUECOLOR=1

inputrc

Bash (and other programs that use the Readline library) use the completion and keybinding settings defined in .inputrc, so we need to customize this file as well.
First include the /etc/inputrc/ file, as we don’t want to lose the defaults it defines.
$include /etc/inputrc

Ignore case when using tab completion (found from Bash and Unix tools | Seena Burns)

set completion-ignore-case on

Treat hyphens (‘-’) and underscores (‘_’) as equivalent when performing case-insensitive filename matching and completion (see Readline Init File Syntax (Bash Reference Manual)).

set completion-map-case on

Set the maximum number of characters to be the common prefix to display for completions. More than the defined number are displayed by an ellipsis. This is useful when there are a lot of similar filenames e.g. Screenshot_ (see A ~/.inputrc for Humans - Top Bug Net).

set completion-prefix-display-length 3

Bind tab to menu-complete, in order to cycle through suggestions

"\C-i": menu-complete

Display possible completions using different colors to indicate their file type.

set colored-stats on

Map the up and down arrows to search the history from after the point of the cursor e.g. after writing man pressing up and down will find all history entries beginning with man (found from Bash and Unix tools | Seena Burns).

"\e[A": history-search-backward
"\e[B": history-search-forward