bash
Here are a few less-known tricks to make in infinitely more useful.
infinite history
Keep your history! Search through your old commands instead of looking for them again and again.
notes
-
You have probably seen HISTSIZE and HISTFILESIZE settings in .bashrc. Good start, you increased the values, but your your history file still gets truncated sometimes.
-
Apparently; bugs, race conditions with programs/sessions or programs such as screen, can cause your history file to be emptied or truncated by not respecting the set limits.
Settings to be put in .bashrc.
Set comment out HISTSIZE and HISTFILESIZE and export them with an empty string (or -1 if you have bash 4.3 or newer), to set unlimited history in memory and unlimited saved lines.
Export HISTFILE and set it to an alternative history file.
Make sure you have bash set up to append new commands and save them after every command.
All this will make sure commands from your current session are written to the history file and eliminate the chance of the history file being emptied or truncated by bugs or external programs.
# Unlimited history ...
# Don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
#HISTCONTROL=ignoreboth:erasedups
# Append to the history file, don't overwrite it
shopt -s histappend
# For setting history length see HISTSIZE and HISTFILESIZE in bash(1)
#HISTSIZE=1000
#HISTFILESIZE=2000
export HISTSIZE=
export HISTFILESIZE=
export HISTFILE=~/.bash_history_infinite
# Save after every executed command
#PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
quick up/down search
while Ctrl-r if fine, sometimes you want to start typing the beginning of a long command and have the rest of it being pulled from history.
Bind history search to the arrow keys.
# Bind history search to "up" and "down" keys
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
note
- Only works with the matched string from the begging of the command.
- Sadly no free string searching (use Ctrl-r), but can quickly segregate through types of commands (eg. ssh + "up" key, to jump through all ssh commands in history).
search trough history (hstr)
While Ctrl-r if fine, sometimes you want more out of a search.
A fantastic plugin for your shell hstr!
notes
-
Default configuration rebinds hstr to Ctrl-r, disabling built in search. With this, you loosed the ability to search (Ctrl-r) and then execute the selected command while inputting the next command from history (Ctrl-o). To avoid that, rebind hstr to other key combination, such as Ctrl-h.
# Set up HSRT alias hh=hstr # hh to be alias for hstr export HSTR_CONFIG=hicolor # get more colors # if this is interactive shell, then bind hstr to Ctrl-h if [[ $- =~ .*i.* ]]; then bind '"\C-h": "\C-a hstr -- \C-j"'; fi # if this is interactive shell, then bind 'kill last command' to Ctrl-x k if [[ $- =~ .*i.* ]]; then bind '"\C-xk": "\C-a hstr -k \C-j"'; fi
-
Newer kernels (6.2 onwards), started to disable the legacy virtual input device for CLI. If you're experiencing issues with hstr, you can enable them back.
sysctl -w dev.tty.legacy_tiocsti=1