Obnoxious postings from Google Groups

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Nov 9 12:30:42 CET 2012


On Fri, 09 Nov 2012 10:49:41 +0100, Hans Mulder wrote:

> On 6/11/12 23:50:59, Steven D'Aprano wrote:
>> On Tue, 06 Nov 2012 17:16:44 +0000, Prasad, Ramit wrote:
>> 
>>>> To enter the newline, I typed Ctrl-Q to tell bash to treat the next
>>>> character as a literal, and then typed Ctrl-J to get a newline.
>>>
>>> That sounds complicated, my version of bash lets me type
>>> 'foo<enter>bar'<enter> for the same effect.
>> 
>> Well, I learned something new about bash.
>> 
>> On the other hand, the Ctrl-Q next-char-is-literal trick works for
>> entering control characters that otherwise don't have a key on the
>> keyboard.
> 
> How does that trick work?  If I need a control character that is not
> available in my current keyboard mapping, how would I enter such a
> character using this Ctrl-Q trick?

This only works if you are running a Linux or Unix shell with the 
libreadline library installed. This should work on nearly any modern 
Linux system with the bash shell. I don't know about other shells.

On Mac OS, the relevant library is called libedit instead, and the 
details may be different.

On Windows, you're out of luck.

Anyway, using Linux and bash: at the shell, if I type Ctrl-U, that is 
interpreted by the shell to mean "clear the line currently being edited". 
So if I type Ctrl-U, the line is cleared.

But if I type Ctrl-Q first, then Ctrl-U, instead readline enters a 
literal ^U character (ASCII value 0x15 = NAK Negative AcKnowledgment) 
into the line editing buffer.

The same trick should work in the Python interactive editor:

>>> ord('^U')  # type Ctrl-Q Ctrl-U to get the ^U char
21


Note that this may or may not work in IDEs such as IDLE. Many IDEs do 
their own thing for editing, and there's no guarantee they will support 
this functionality.

One last comment: readline is very configurable, and the command to 
insert the next character could be just about anything. But Ctrl-Q is the 
standard.


-- 
Steven


More information about the Python-list mailing list