The power of GNU Readline - part 3 (HPR Show 2505)

Dave Morriss

Table of Contents


The GNU Readline manual uses the terms point, mark and region, which need definition. If you follow some of the links to this manual in this series you will encounter these terms, and I think they can be confusing.

The current cursor position (we have simply referred to it as the cursor position so far in this series). Also called the insertion point.
A cursor position saved by the set-mark command (we’ll look at this in a forthcoming episode)
The text between point and mark (also for a future episode)

In this series I will try not to use these terms without an explanation or reminder of what they mean. I will be looking at these and commands that affect them more in later episodes.

Readline Arguments

Readline commands (which are what are being invoked by the key sequences we have seen so far) can take numeric arguments. Sometimes the argument acts as a repeat count, other times it is the sign of the argument that is significant.

The argument itself begins with the Meta key, pressed in conjunction with a digit. If a multi-digit number is required this followed by further digits without the Meta key. The first “digit” can be a minus sign (‘-’) if the numeric argument is to be negative.

For example, to repeat the C-d (Control-d) command 10 times type: M-1 0 C-d (Meta-1 0 Control-d). This will delete forward 10 characters.

A negative argument reverses the effect of the command, so M-- C-k (Meta-- Control-k), instead of deleting characters forward to the end of the line deletes them backwards to the start of the line.


Some of the sequences we are looking at in this series can be intercepted and interpreted by:

  1. The terminal
  2. The desktop environment

For example, I have been testing the key sequences used in these episodes using the Terminator terminal emulator on XFCE and I have found M-l (see later) was interpreted by Terminator and could not be typed.

Remember that all meta key sequences can be entered as Esc key – press the Esc key then press the second key (l in this particular instance).

More character and word manipulations (and others)

Let’s get into some more Readline key sequences.

Commenting out a line

This is something I often do. I’m typing a complex command and I want to save what I’m doing and check something, or answer the phone perhaps. I used to hit the Home key or C-a and prefix the line with a # comment character then hit Return. The line is in the history and can be recalled, continued and executed after the comment has been removed.

There are Readline sequences that can help:

M-# (Meta-#)
Add a comment symbol to the start of the line and issue a Return.
M-1M-# (Meta-1 Meta-#)
If the current line begins with a comment remove it and execute the line with a Return. (If it doesn’t begin with # then add one in the same way as M-#)

The second sequence is actually M-# with an argument and any argument will have the same effect. So M-0M-# would also remove the comment and enter the command. This sequence is actually a “toggle” which adds a comment if there isn’t one and removes it if there is.

The comment character can be changed (which we’ll discuss in a later episode when we look at the Readline configuration file), which can be of relevance if Readline is being used in an application where a different comment character is required.


  • Type the Bash command: echo "Hello" but don’t press Return.
  • Type M-#. The command turns into #echo "Hello" and the line is accepted as if Return had been pressed.
  • Recall the line with the up arrow key.
  • Type M-1 M-#; the comment character is removed and the line accepted.

Transpose characters

I’m a bad typist. I so often type words like directroy with transposed letters. However, Readline offers a facility to correct such errors:

C-t (Control-t)
Transpose characters. Swap the character before the cursor (point) with that under the cursor, then move the cursor to the right.


After typing the word incorrectly position the cursor as shown:


Press C-t and the ‘o’ and ‘r’ are transposed and the cursor moves to the ‘y’.


If the insertion point is at the end of the line, then this transposes the last two characters of the line.

directoyr   --> C-t -->   directory   --> C-t -->   directoyr
         ^                         ^                         ^

Transpose words

You can also transpose words, where a word is defined as we have discussed earlier in the series, a series of letters and digits.

M-t (Meta-t)
Transpose words. The cursor (point) can be anywhere in a word (or just before it). It and the word before it are swapped and the cursor is left after the pair of words. If there is no word before the word the cursor is on then nothing happens. If the cursor is at the end of the line the last word and the word before it are swapped – repeatedly for every M-t.


You think that split infinitives are bad:

echo "to boldly go where..."

Press M-t and the result is:

echo "to go boldly where..."

Note that, even though the cursor is not on a word transposition still takes place. Press M-t again and the result is:

echo "to go where boldly..."

Change the case of words

Readline allows you to change the case of whole words, to upper case, to lower case or change the case of the first letter of a word to upper case (capitalise it).

M-u (Meta-u)
Uppercase the current (or following) word. With a negative argument, uppercase the previous word, but do not move the cursor.
M-l (Meta-l)
Lowercase the current (or following) word. With a negative argument, lowercase the previous word, but do not move the cursor.
M-c (Meta-c)
Capitalise the current (or following) word. With a negative argument, capitalise the previous word, but do not move the cursor.

To change the case of a whole word the cursor must be before the word or on its first letter. If it is part-way through the word then the rest of the word from the cursor to the end of the word is changed.

For capitalisation the situation is similar. The capital is at the start of the following word, or it occurs where the cursor is positioned. The rest of the word is lowercased.


1. Upper and lower case

Given the following command with the cursor positioned as shown (NB: type the line and press M-b three times to move three words backward):

echo "hacker public radio"

Press M-u and the result is:

echo "HACKER public radio"

The current word has been changed to upper case and the cursor moved after it. Press M-u again and the result is:

echo "HACKER PUBLIC radio"

The following word has been changed to upper case and the cursor moved after it. Press M--M-l (Meta-- Meta-l) (remember the simplest negative argument is achieved by pressing the Meta key in conjunction with a dash) and the result is:

echo "HACKER public radio"

The previous word has been changed to lower case but the cursor has not been moved.1

Regarding how much of a word is changed:

echo "hacker public radio"

Pressing M-u here gives the following result:

echo "hacKER public radio"
2. Capitalisation

Using another command:

echo "the capital of scotland is edinburgh"

Press M-b six times then press M-c, M-f twice, and M-c the result is:

echo "The capital of Scotland is edinburgh"

Press M-f and M-c the result is:

echo "The capital of Scotland is Edinburgh"

What was going on here should be self-evident from the previous episodes in this series! ☺

Revert the line

We saw the undo command in episode 1 of this series: C-_ (Control-_) or C-x C-u (Control-x Control-u) but there is a short-cut that undoes all changes. The following description is copied from the GNU Readline manual, section 1.4.8.

M-r (Meta-r)
Undo all changes made to this line. This is like executing the undo command enough times to get back to the beginning.


I don’t use this sequence often. I experimenting with it for this episode I did not find the description particularly useful so I spent a bit longer looking into it.

First the undo command: this will revert individual steps, as mentioned. Given the following line containing a command:

echo "Star Wars"

Move back to the ‘r’ in ‘Star’ and press C-t:

echo "Stra Wars"

Move forwards to the ‘W’ in ‘Wars’ and press C-t:

echo "StraW ars"

Now press C-_ to perform one undo:

echo "Stra Wars"

And again:

echo "Star Wars"

We are back to the original state except that the cursor is in a different place. Pressing C-_ again results in a blank line.

Given the same starting point, pressing M-r results in the blank line – all of the changes are undone including the typing of the command in the first place.

If you recall a command from the history, neither of these key sequences do anything because that is the original state of the line. The line can be deleted with C-u (which kills the entire line as we saw in episode 2).

Also, if the recalled line is edited, individual edits can be reverted with C-_ or all of them with M-r, but only back to the state of the line that was recalled.

  1. In the audio I wondered whether M-3 followed by M-u would uppercase the next three words to the right, and I later found that it does, and it works with M-l and M-c.

    So pressing M-3M-c with the cursor positioned on the ‘h’ of ‘hacker’ in echo "hacker public radio" results in: echo "Hacker Public Radio"