Obnoxious postings from Google Groups

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Nov 7 01:13:47 CET 2012


On Tue, 06 Nov 2012 23:08:11 +0000, Prasad, Ramit wrote:

> 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.
>> 
>> 
> Would you mind elaborating on how this works? I know it's not a bash
> list, but I do not understand how ctrl-J is considered a literal.
> Obviously, I must have a different definition of "literal". Where can I
> find a list of other literals? My Google-fu is being weak today. :(

I'm not an expert, so the following may not be exactly correct. As I 
understand it, when you hit a key on the keyboard, it sends the character 
you typed to the operating system. (The OS can then remap keys, generate 
keyboard events including a timestamp, etc.)

Hit the J key, and the event includes character "j". Hit Shift-J, and 
character "J" is sent. Hit Ctrl-J, and the character sent is the ASCII 
control character ^J, or newline. (Technically, the name for ASCII 10 is 
"linefeed" rather than "newline".)

Similarly, other control character combinations send other control codes:

^A = ASCII 0x01 Start Of Heading
^L = ASCII 0xFF Formfeed \f
^M = ASCII 0x0D Carriage Return \r

etc.

http://en.wikipedia.org/wiki/C0_and_C1_control_codes


When readline is enabled in bash, one of the standard editing commands is 
that C-q (usually ctrl-Q on the keyboard) instructs readline to treat the 
next key as a literal. So Ctrl-Q followed by Backspace won't delete the 
previous character, but insert a literal DEL 0x7F character. 

(One of those historical quirks is that on most(?) keyboards, the 
Backspace key generates a DEL character rather than the ^H backspace 
control code, and the Delete key generates an escape sequence. Go figure.)


-- 
Steven


More information about the Python-list mailing list