Blog Post Publish Date: 2026/10/25
Hacking Your Bash Terminal with Custom Widgets#
This blog post is an extension of GNU Readline: The Magic Behind Terminal Autocomplete. Here, I explain how to create custom Bash widgets by registering Bash functions to keyboard shortcuts powered by the GNU Readline library.
Understand the Basics#
The idea is simple: you create a Bash function and register it to a custom keyboard shortcut using the bind command. There are some Bash Reserved Variables that are made available to Bash functions when they are called using the bind -x command. The two main variables in this case are READLINE_LINE that is the entire contents of the command line, and READLINE_POINT that is the cursor position, represented as an integer offset into READLINE_LINE.
To understand these variables in practice, let’s create a function called debug-hook-variables and bind it to the Ctrl+x shortcut. This function will print the values of the two variables:
# bash function for debugging reserved variables
debug-hook-variables() {
echo ">>> READLINE_LINE: $READLINE_LINE"
echo ">>> READLINE_POINT: $READLINE_POINT"
}
# "\C-x" is the code for `Ctrl+x` shortcut
bind -x '"\C-x": debug-hook-variables'
Now, type anything at your prompt and press Ctrl+x. In this example, with your cursor positioned after definitely and pressed Ctrl+x, the output will be like:
definitely maybe # press `Ctrl+x`
>>> READLINE_LINE: definitely maybe
>>> READLINE_POINT: 11
There are other variables, such as READLINE_ARGUMENT and READLINE_MARK, that are also made available to Bash functions. However, their usage is less straightforward and is typically relevant to more advanced use cases.
Practical Example: Repeated Command Execution#
Now, let’s implement an alternative to the $ watch command. The watch command can behave unexpectedly when executing aliases such as kgp (more details in this Stack Overflow thread). The goal of this widget is to execute a command repeatedly every two seconds when you press Ctrl+x, while supporting aliases.
repeat-command-widget() {
while true; do
clear
eval "$READLINE_LINE"
sleep 2
done
}
bind -x '"\C-x": repeat-command-widget'
You can test the widget by typing the following command and pressing Ctrl+x:
kgp # press `Ctrl+x` and stop execution by `Ctrl+c`
The kgp command is executed in a loop every two seconds until you press Ctrl+c, just like watch "kubectl get pods".
How to Register the Widget#
Built-in Readline widgets can be configured in the ~/.inputrc file. For custom Bash widgets, we need to register the function and its keyboard shortcut in the ~/.bashrc file.
# ~/.bashrc
repeat-command-widget() {
while true; do
clear
eval "$READLINE_LINE"
sleep 2
done
}
bind -x '"\C-x": repeat-command-widget'
The custom Ctrl+x shortcut will be available in new Bash sessions.
Conclusion#
It is important to understand that Bash also supports features that are available in Zsh and Fish. I’m not saying Bash is better or that you shouldn’t use Fish or Zsh. My point is that you don’t need to switch shells just because another shell offers features you like. Understanding how these mechanisms work behind the scenes expands your possibilities and helps you customize your terminal to suit your needs.
Links#
GNU Readline foundation: https://carlosneto.dev/blog/2026/2026-10-18-gnu-readline-foundation
Bash
bindbuiltin: https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.htmlBash variables (
READLINE_LINE,READLINE_POINT): https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html