GNU Readline is a library commonly used to provide interactive prompts in commandline applications. The two prompts that use Readline that I used the most are Bash and the Python interpreter.
Readline may be customized by adding configuration to ~/.inputrc
. This page describes both
customized and default behavior that I have found helpful.
Press tab repeatedly to cycle through completions
TAB:menu-complete
If there are multiple completions with a common prefix, pressing tab the first time will
tab-complete the common prefix, and any subsequent tabs will cycle through the completion. If
you also set colored-completion-prefix on
, then the completions displayed will have the
common prefix highlighted in another color.
set menu-complete-display-prefix on
If completion is ambiguous, show available completions on first TAB press
set show-all-if-ambiguous on
If a completion is a directory or a symlink to a directory, append a trailing slash
set mark-directories on
set mark-symlinked-directories on
Display completions in different colors to indicate their file type. Color definitions are taken from
the LS_COLORS
environment variable. You can also set visible-stats on
to have
a similar effect by appending a symbol to the filename.
set colored-stats on
By default, you can also use alt + * to insert all matching completions.
The following are default keybinds
"\C-f":forward-word
"\C-b":backward-word
When closing a paired parenthesis, curly brace, or square bracket, flash the matching opening delimiter by briefly moving the cursor to the matching character. This only has the appearance of moving the cursor; it's completely safe to continue typing as soon as you enter the closing delimiter.
set blink-matching-paren on
The following are default keybinds
EDITOR
unix-word-rubout
, but I'm unsure how that's different from
backward-kill-word
)
There's also alt + d to kill the next word, but since I find
alt awkward to reach for, I don't use this. Instead, with
ctrl + f bound to forward-word
, I find it easier to use
ctrl + f + w.
You can also use alt + # to comment the current line and start a new
one. This is especially useful for Bash one-liners where you realize you need to do something else
first, but don't want to throw away the current line. But I find it an especially awkward keypress, so I
normally just use ctrl + a to move the cursor to the front and enter a
#
myself.
For reverse history search in Bash, I use
FZF, which is almost equivalent
to Readline's reverse-search-history
(bound to ctrl + r by
default). I find it to be more polished, faster (to use, not necessarily more responsive), but the
Readline shortcuts described above don't work in its prompt. While this is acceptable to me, it was an
immediate impediment to a colleague.
Otherwise, Readline provides some useful variables to access previous lines from its history.
!$
- The last argument of the previous command!^
- The first argument of the previous command!!
- The previous command. Useful if you forgot to add sudo
(e.g.,
sudo !!
)
!n
- The n
th command in the history (counted from the start of the
history)
!cmd
- The previous command that began with cmd
. Appending
:p
will print the command instead of executing it.