Readline also provides you with the ability to map your own custom keybindings by editing the .inputrc configuration file in your home directory.
The following are two types of custom keybindings you can define:
- Macros
- Functions
Macros
You can define a keybinding that will, when executed, fill in a string of characters at the cursor’s current position. To do this add a line with the following format to your .inputrc config file (or create the file if it doesn’t exist yet):
<key combination>: "<string of characters>"
For example, say I find myself repeatedly redirecting output to the file “~/debug_output.txt”. I could define a macro in my .inputrc by adding the following line:
Control-g: " > ~/debug_output.txt"
Now, whenever I type the key combination Control-g, the redirect statement will be added after my cursor, allowing me to easily append it to the end of other commands.
Keep the following in mind when you are creating macros:
- I can spell out the key combinations in english (For more information on this, see the man page for bash. Search for the “Readline Key Bindings” section)
- The sequence of characters to be inserted by the macro must be wrapped in double quotes (“)
- The choice of Control-g was arbitrary, you could choose a combination that is more meaningful to you.
- It is possible to override the default keybindings (keep this in mind when you define your own)
- For the keybinding to take effect, you must start a new shell instance (.inputrc config file is read on shell startup)
Functions
You can also create a keybinding that will, when executed, call a function. Readline provides a large number of pre-defined functions to choose from. To map a key combination to a pre-defined function:
<key combination>: <function-name>
For a list of available pre-defined functions, see the GNU Readline manual.
It is also possible to define you own custom functions, but this requires writing them in C and is outside of the scope of this tutorial. We might cover that as a separate tutorial later.
One useful pre-defined function is menu-complete. It provides an alternate auto-completion behaviour.
The default behaviour for auto-completion (mapped to the TAB key) is to either:
- fill in the partial command/directory/filename if a single match exists
- display a list of possible matches when more than 1 exist
menu-complete will instead cycle through the list of possible matches, filling the next possible match in each time the function is called. This behaviour can be nice, especially when auto-completing longer filenames. To remap the TAB key to use this behaviour, add the following line to your .inputrc file.
TAB: menu-complete
Note that this new keybinding will not take effect in the current shell, you must start a new instance to reload the .inputrc configuration file
In this tutorial we’ve learned that the Linux command line keybindings are powered by a tool called Readline. A keybinding can be 1 of 2 types:
- Macro: expands a sequence of characters
- Function: performs an action
Readline provides us a set of default keybindings, but also provides us with the flexibility to create our own in the .inputrc configuration file. Finally, to see all of the current keybindings, we can use the ‘bind’ command.
bind -p
Comments on this entry are closed.
Hi,
Tiny and useful article
Thank a lot
Always interesting, forgot about .inputrc Will be using it more in the future.
You do not need to start a new shell to re-read .inputrc; use ^X^R
my inputrc
“\e[1~”: beginning-of-line # home
“\e[2~”: yank-last-arg # insert
“\e[3~”: delete-char # delete
“\e[4~”: end-of-line # end
“\e[5~”: backward-kill-word # page up
“\e[6~”: kill-word # page down
#set bell-style visible
set bell-style none
set meta-flag On
set convert-meta Off
set input-meta on
set output-meta On
set horizontal-scroll-mode On
set show-all-if-ambiguous On
“\e[[A”: backward-word # F1
“\e[[B”: forward-word # F2
“\e[[C”: backward-kill-line # F3
“\e[[D”: kill-line # F4
“\e[[E”: yank # F5
“\e[17~”: undo # F6
“\e[18~”: history-search-forward # F7
“\e[19~”: history-search-backward # F8
“\e[20~”: character-search # F9
“\e[21~”: character-search-backward # F10
“\e[23~”: “\eb’\ef'” # F11
“\e[24~”: kill-whole-line # F12
Thank you so much! I’ve seen the menu-complete functionality in zsh and have always wanted a similar capability in bash. Never thought of using readline.