Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Why is ^M used to represent a carriage return in VIM and other contexts?

My guess is that M is the 13th letter of the Latin alphabet and a carriage return is \x0D or decimal 13. Is this the reason? Is this representation documented anywhere?

I notice that Tab is represented by ^I, which is the ninth letter of the Latin alphabet. Conversely, Tab is \x09 or decimal 9, which supports my theory stated above. However, where might this be documented as fact?

share|improve this question
1  
Also keep in mind that dos/windows use "0x0d 0x0a", also noted as "CR LF". But unix/linux use only "0x0a" or "LF". So when you open a windows document in linux it detects extra "CR", and when you open a linux document in windows it doesn't detect new lines. –  LatinSuD Jun 5 at 8:47
3  
@LatinSuD caret notation (and corresponding use of the Ctrl-key) relates to the C0 control set (historically part of ASCII) directly and not whether and how a given operating system or program uses part of that set in representing new lines, or anything else. Similarly, whether ^H deletes a character or allows overprinting (such as n^H~ as an obsolete way to produce ñ) or any other actual use of the control character is separate from the caret notation. –  Jon Hanna Jun 5 at 12:05
10  
old one ... I can't remember the original code, but ctrl-G rings a bell! –  Brian Drummond Jun 5 at 13:28
    
the ^M you see when in linux (which uses "0x0a"(LF)) is probably from a file made on windows (which uses "0x0d 0x0a" (CR LF)). Thus, at the end of each line, you see the extra "0x0d" (CR). (the 0x0a being interpreted as a newline, and not shown in vi (well, it is : the next line will have a "~" if the previous line didn't end with a Newline). So the the ^M is not exactly a "carriage return", it's part of what a carriage return is in windows. The Answer tells why it's represented that way (using Caret Notation, ^@ = 0x00, ^A=0x01, etc..., ^M=0x0d, ...) –  Olivier Dulac Jun 5 at 14:47
3  
@OlivierDulac no, the ^M is exactly a carriage return, just like ^J is exactly a line-feed. While different OSs have had different views as to whether line-feed and/or carriage return or something else (like the Newline character used by some IBM characters but not part of ASCII and so not part of the historical heritage of some other OSs) should represent a new line in a text file, and while some programs have then overridden that in different ways, U+000D itself is still a carriage return, whatever later operating systems like Unix or DOS decided to do with it. (Of course, calling it... –  Jon Hanna Jun 5 at 21:43

7 Answers 7

up vote 104 down vote accepted

I believe that what OP was actually asking about is called Caret Notation.

Caret notation is a notation for unprintable control characters in ASCII encoding. The notation consists of a caret (^) followed by a capital letter; this digraph stands for the ASCII code that has the numerical value equivalent to the letter's numerical value. For example the EOT character with a value of 4 is represented as ^D because D is the 4th letter in the alphabet. The NUL character with a value of 0 is represented as ^@ (@ is the ASCII character before A). The DEL character with the value 127 is usually represented as ^?, because the ASCII '?' is before '@' and -1 is the same as 127 if masked to 7 bits. An alternative formulation of the translation is that the printed character is found by inverting the 7th bit of the ASCII code

The full list of ASCII control characters along with caret notation can be found here

Regarding vim and other text editors: You'll typically only see ^M if you open a Windows-formatted (CRLF) text file in an editor that expects Linux line endings (LF). The 0x0A is rendered as a line break, the 0x0D right before it gets printed as ^M. Most of the time, editor default settings include 'automatically recognize line endings'.

share|improve this answer
1  
I always wondered what that thing was called... –  smci Jun 5 at 12:08
3  
This convention goes back at least to the 1970's; I first saw it on the TOPS-10 operating system but it may well have existed earlier. For what it's worth, on older ASCII terminals the character now shown as a caret was actually an upward-pointing arrow, so this originated as "uparrow notation". –  keshlam Jun 5 at 12:56
13  
This is explictly built into the ASCII design so that the Ctrl key just toggles bit 7. –  OrangeDog Jun 5 at 20:19
1  
It's not used only with letters. I would not define it as the control character with "the letter's numeric value" but rather as "xor 64". In other words, ^A is 0x41 xor 0x40, or 0x01 and ^? is 0x3F xor 0x40, or 0x7F. –  R.. Jun 6 at 6:24
1  
Ascii DEL (^?) has nothing to do with the delete key. It's actually the standard code generated by the <--- key (also, confusingly, called backspace) on VT100-like terminals. –  R.. Jun 9 at 22:32

That is exactly the reason.

ASCII defines characters 0-31 as non-printing control codes. Here's an extract from the ascii(7) manual page from a random Linux system (man ascii), up to and including CR (13):

   Oct   Dec   Hex   Char                       
   ─────────────────────────────────────────────
   000   0     00    NUL '\0'                    
   001   1     01    SOH (start of heading)     
   002   2     02    STX (start of text)         
   003   3     03    ETX (end of text)           
   004   4     04    EOT (end of transmission)   
   005   5     05    ENQ (enquiry)               
   006   6     06    ACK (acknowledge)           
   007   7     07    BEL '\a' (bell)             
   010   8     08    BS  '\b' (backspace)       
   011   9     09    HT  '\t' (horizontal tab)  
   012   10    0A    LF  '\n' (new line)        
   013   11    0B    VT  '\v' (vertical tab)    
   014   12    0C    FF  '\f' (form feed)       
   015   13    0D    CR  '\r' (carriage ret)    

Conventionally these characters are generated with Control and the letter relating to the character required. Teletypes and early terminal keyboards had 'BELL' written above the G key for this reason.

The standards document that defined ASCII is ASA X3.4-1963, which was published by the American Standards Association in 1963. I can't find the original document on their website, but this extract from the original document shows the character table, including the control codes above.

share|improve this answer
4  
Thank you. Though informative, this answer does not contain the answer to the question. –  dotancohen Jun 5 at 9:01
1  
The answer is hidden in the second paragraph: ^M is shorthand for Control-M. On the terminal you would press the Control key together with the M key to send the ASCII kode 0x0D also known as a carriage return. –  Martin Liversage Jun 6 at 4:02

The notation goes back to the earliest ASCII Teletypes (ca 1963). There was a CTRL key that toggled the 0x40 bit so that CTRL-M (carriage return) would be 0D instead of 4D, CTRL-G (bell) would be 07 instead of 47, CTRL-L (form feed) would be 0C instead of 4C.

There was no "design" in assigning particular letters to particular functions, it was just chance that, when the dust settled from assigning ASCII codes, the M key was one bit different from carriage return and hence carriage return became CTRL-M.

Here is the best shot I can find of an ASR33 keyboard. As you can see the control character names are printed in small letters on the corresponding alpha keys.

enter image description here

The M key does not have a notation on it because there is a dedicated "RETURN" key, so CTRL-M is redundant.

share|improve this answer
1  
On some level the extent to which we are still bound by design choices made for what now seem like ancient systems is quite surprising - I guess on reflection that (a) it's not that long ago, it's just that the pace of change in the interim has been astonishing, and (b) if enough design decisions are made, some of them (especially the ones that don't cause people enough problems) are bound to stick around long after the reasons for them disappear into memory. Still an odd feeling to look back at the history of some of these things though. –  Stuart Golodetz Jun 7 at 16:15
1  
@StuartGolodetz - Actually, I find it strangely reassuring. But then I remember when Teletypes were "advanced technology". (The Teletype ASR-33, by the way, was remarkable for it's elegant simplicity. I only wish that "modern" computer systems were as well-designed.) –  Daniel R Hicks Jun 7 at 17:29
    
This is fascinating but what I don't understand is.. why of all things did they decide this typewriter needed a bell? –  CaptainCodeman Jun 8 at 0:08
3  
@CaptainCodeman - When you transmitted an important message you'd ring the bell to get the attention of the operator on the other end. –  Daniel R Hicks Jun 8 at 1:05
1  
It is interesting to note that the Ctrl key survives to this day on PC keyboards. –  Daniel R Hicks Jun 8 at 11:38

The caret (^) is just shorthand for writing hold the Control key - CTRL down.

In the good old days you could type these codes (see above) in directly, Ctrl key + G (^G) would make the terminal go "ding"

When you want to add a CR in Vim you use Ctrl key + M etc tab = Ctrl + I

share|improve this answer
    
The term you are looking for is digraph, which means two characters that represent one character. Specifically, digraphs and trigraphs are used to represent nonprintable characters. Historically they have also been used for characters that do not appear on a keyboard, although with modern GUIs and keyboards this is less of an issue so this use is more archaic. –  Snowman Jun 6 at 15:08
    
"In the good old days" is still today, with ^C and ^D being perfectly functional. The only reason that ^G doesn't make the terminal ding anymore is that most terminal emulators have that response turned off. –  SevenSidedDie Jun 9 at 17:45

The need for some visual manner of displaying what are by definition non-printable characters.

So, someone in the early 1970s (or maybe earlier) (I remember seeing it on CP/M, and someone else has already mentioned TOPS) decided that "caret plus letter" would be the symbol for the 26 unprintable ASCII control characters with values 1 thru 26. Value 0 is/was printed as ^@, and value 127 as ^?.

share|improve this answer

Where is it documented, well this page lists every control character, with how to enter/represent it with the control key(though the first one, ascii character 0, has no control key representation), and it has nothing for character 127. And it provides sources at the bottom

https://www.cs.tut.fi/~jkorpela/chars/c0.html

One might wonder, given that there are 33 control characters (ASCII characters 0-31 so 32 charactres, + character 127. so, =33 characters) How they would be all represented as there are only 26 letters in the alphabet. Well, it uses Ctrl-A for Ascii character 1, Ctrl-Z for ascii character 26, and there once it reaches Ctrl-Z, it uses [ \ ] ^ _

It lists Ctrl-Z as SUB, though in DOS and the cmd prompt it's EOF, and as a techie user you use it when doing copy con a.a where a.a is your file. You enter the text and terminate it with Ctrl-Z which funnily enough doesn't enter an EOF marker. But does tell CMD that's the end of the file so CMD writes it.

That cs.tut.fi webpage gives this as a source
http://www.wps.com/texts/codes/X3.4-1963/index.html

but it's a broken link, but available on archive.org it's in the form of JPGs

American Standard Code for Information Interchange
ASA standard X3.4-1963

https://web.archive.org/web/20010430085116/http://www.wps.com/texts/codes/X3.4-1963/index.html

share|improve this answer
    
Most of the control characters are meaningless, but even some of those with meaning like Ctrl-I i'm not sure where you can just do Ctrl-I and get a tab. –  barlop Jun 5 at 18:27
1  
none of the control characters are meaningless. Many of them are unused in many contexts, but every single one has at least one meaning. –  Jon Hanna Jun 5 at 21:53
    
@JonHanna Of course I don't mean they were meaningless(past tense).But R.Have been meaningless for decades i.e. they had their original meanings from eons ago,tech that no longer runs, are (most of the chars) meaningless today w/ current and even slightly old tech.n if any are being put to modern uses it's not many. There's a list here en.wikipedia.org/wiki/Control_character of ones in common use 0,7,8,9,10,11,12,13,127. 9/33 so the others (24 of them) u would either c very rarely or not at all as they r as dead as the antic unused out of use for decades machinery they were used on –  barlop Jun 5 at 23:25
    
Associated Press still use ANPA-1312 which uses 1–4, 6 & 16 are used to start every TCP/IP connection. Modern printers (among other thigns) still use 17 & 19. Together with those you mention, we've quite a percentage of them covered without really trying. I'll grant you they aren't in heavy use, but they ain't dead either. –  Jon Hanna Jun 5 at 23:47
1  
@barlop You can do ^I for a tab in standard bash: type ls ~/^I^I and you should see all the folders in your home directory. –  WChargin Jun 6 at 2:11

You can see all of the non pritable ASCII characters Control mapping in this table.

share|improve this answer
5  
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. That way, should the linked page ever change or become invalid for any reason, the answer will still be useful to visitors to Super User. –  Michael Kjörling Jun 5 at 8:56
3  
Thank you. Though informative, this answer does not contain the answer to the question. –  dotancohen Jun 5 at 9:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.