The power of GNU Readline - part 1 (HPR Show 2073)

Dave Morriss


Table of Contents

GNU Readline

We all use GNU Readline if we we use the CLI in Linux because it manages input, line editing and command history in Bash and in many tools.

I have been using Unix and later Linux since the 1980's, and gradually learnt how to do things like jump to the start or the end of the line, delete a character backwards up to a space, or delete the entire line.

I think that learning GNU Readline is worthwhile since it contains a lot more features than what I just described. I thought I would do a few episodes on HPR to introduce some of what I consider to be the most useful features.

I want to keep the episodes short since this is a dry subject, and, if you are anything like me, you can't take in more than a few key sequences at a time.

The source of my information is the GNU Readline Manual. This is very well written, if a little overwhelming.

Keys and Notation

Most of the features in GNU Readline are invoked by multi-key sequences. These involve the Control key and the so-called Meta key. The Control key is usually marked Ctrl on the keyboard. The Meta key is the key marked Alt.

The notation used in the GNU Readline manual is C-k for 'Control-k', meaning the character produced when the k key is pressed while the Control key is being held down.

For the Meta key the notation M-k (Meta-k) means the character produced when the k key is pressed while the Meta key is being held down.

If your keyboard does not have a Meta key then the same result can be obtained by pressing the Esc key, releasing it, then pressing the k key.

In some instances both the Control and the Meta key might be used, so M-C-k would mean the character produced when the k key is pressed while the Meta and Control keys are being held down.

Commands you probably already know

C-b
Move back one character. This is the same as the left arrow key if you have one.
C-f
Move forward one character. This is the same as the right arrow key if you have one.
Backspace
Delete the character to the left of the cursor.
C-d
Delete the character underneath the cursor.
DEL
Depends on your setup may be the same as Backspace or C-d. In my case (Debian Testing with Xfce) it's the same as C-d.

Commands you might not know

C-_ (or C-x C-u)
Undo the last editing command. Can undo all the way back to the blank line you started with. Remember the '_' underscore is usually on the same key as the '-' hyphen, so you'll need to use Control, Shift and underscore.
C-a
Move to the start of the line. This is the same as the Home key if you have one.
C-e
Move to the end of the line. This is the same as the End key if you have one.
M-f
Move forward a word. A word is what you would expect, a sequence of letters and numbers.
M-b
Move backward a word.
C-l
Clear the screen, reprinting the current line at the top.

Example

This is a little difficult to demonstrate on an audio podcast, but hopefully the description will be understandable.

  • In a terminal type: The quick brown fox
    • After the 'x' of 'fox' press M-b. The cursor moves to the 'f' of 'fox'.
    • Press M-b again. The cursor moves to the 'b' of 'brown'.
    • Press C-d. The 'b' is deleted and the cursor is on the 'r' of 'brown'.
    • Press C-_. The 'b' is restored, but the cursor is on the 'r' still.
    • Press C-_ again. The whole line disappears.