Relentless Coding

A Developer’s Blog

Readline .inputrc Configuration Examples

These are some of my favorite readline tweaks.

To make changes to readline’s behavior, you have to put configuration in a file in your home directory called .inputrc. To include commands from /etc/inputrc, put the following first:

$include /etc/inputrc

Variables to customize readline behavior can be set in this file by statements of the form:

set variable-name value

Restart your terminal emulator for the changes to take effect, or execute in current window:

$ bind -f ~/.inputrc

Briefly Move to Opening Parenthesis When Closing Parenthesis Is Typed

You want to make sure you have an even amount of opening and closing parentheses in your statements. By setting

set blink-matching-paren On

the cursor will briefly move to the opening parenthesis.

Display Possible Completions With Different Colors to Indicate File Type

Say, you’re looking for file starting with .X, so you type:

$ ls .X

and then press Tab. Setting

set colored-stats On

will display the possible completions with a different color:

Show different colors for file types on completion

Display Completions One Per Line

Instead of showing completions in columns, you can display completions one per line by setting:

set completion-display-width 0

The default is -1, which means the value is ignored and completions are shown in columns.

Ignore Case on Completion

You want to cd into the folder Documents, so you type:

$ cd docu

and press Tab. Nothing happens. You want readline to complete it for you ignoring the case:

set completion-ignore-case On

Treat Hyphens and Underscores as Equivalent

If you enabled completion-ignore-case (see above), you may also want readline to treat hyphens - and underscores _ as equivalent when performing completion:

set completion-map-case On

Replace Common Prefixes of Certain Length with Ellipsis

Instead of seeing the beginning of file names that you have already typed, you want to focus on what differentiates the files:

$ ls
file1 file2 file3 file4
$ ls file<TAB>

When you type ls file followed by Tab, you will see the common prefixes replaces by three dots.

Disable the “Display all 768 possibilities? (y or no)” on Completion

Sometimes, when the amount of possible completions is greater than 100 (default), readline will show a question whether or not you want to see all possibilities. If you want to increase the threshold (default is 100), you can set:

set completion-query-items 1000

Turn Current Command Into a Comment

You’re typing a command and halfway realize you need to do something else first. But you don’t want to throw away what you typed away. Then you can type M + # in emacs mode or # in vi command mode (in vi insert mode you can still type M + # for the same effect).

You can change the string that is inserted when you press the insert-comment key combo with set comment-begin #.

Safe Pasting with Bracketed Paste

If you copy commands from the web, you might have noticed that when you copy a newline, Bash will interpret that newline as an Enter and execute whatever it is in your input buffer at the moment. This is unsafe, because what you see might not be what you copy. You can be avoided by setting:

set enable-bracketed-paste On

This command puts an escape sequence around what you copy and paste, so Bash can tell the difference between what you, the user types and what you copied from somewhere.

The default is Off.

Expand Tilde to Home Directory

When you refer to the home directory of the user you’re currently logged in as, you most likely use tilde ~ as in cp ~/thesis.txt ~/Documents/. Setting

set expand-tilde On

will always expand the tilde to the full path of your home directory when word completion is attempted (meaning you need to press Tab at some point).

The default is Off.

Further Reading

See man bash under “Readline Variables” for more information and even more ways to tweak your readline.