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:
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)
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.
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.