0

Is there a listing of the various escape codes and how they are represented in vim (and outside I suppose to)? For example:

^M    -- newline
^@    -- NIL
etc.

It seems I always have trouble googling these to figure out what a particular code means (in this case, ^J).

  • bestasciitable.com – D. Ben Knoble Jun 21 at 12:07
  • @D.BenKnoble sure but that doesn't really show the 'display' I am looking for -- i.e., \r being shown as ^J, or maybe I'm not reading it correctly? – David542 Jun 21 at 18:04
  • 1
    Well, ^J is actually LF (line-feed), which C-style escapes represent as \n. Similarly, ^M is actually CR (carriage-return), which is often seen as \r. So, 3 different notations. And it's easy to see by reading across two columns from J or M or @ – D. Ben Knoble Jun 21 at 18:24
  • @D.BenKnoble I see, got it, thanks for the clarification! Why is the representation done +64 over what the code is -- i.e., NUL is 0, but represented as 64 --> @ ? – David542 Jun 21 at 18:35
2

The man page for ascii(7) helps.

While it doesn't list the combinations such as ^@ and ^J, it lists @ and the uppercase letters, then [, \, ], ^ and _, on the right column, aligned with the control symbols on the left column.

The former (@, uppercase letters and the other 5 symbols) have ASCII codes 0x40 through 0x5f, while the control characters have ASCII codes 0x00 through 0x1f. The Control key masks bit 0x40, so the end result of using it with those letters on the right is the corresponding control character on the left.

(The Control key also masks bit 0x20, which explains why lowercase characters, which are on the 0x60 through 0x7f range also produce the same control characters. 0x40 | 0x20 = 0x60 so that's what gets masked.)

In specific, ^J is NL, newline, also represented as "\n". It is the character used to indicate the end of a line in Unix file format.

| improve this answer | |
  • 1
    oh that's great, thank you. So it seems ^M is \r and ^J is \n. Finally, what's the convention of using ^ as a prefix for those characters, do you know why that is done in vim? – David542 Jun 21 at 1:41
  • @David542 Exactly! Updated the answer to include it in specific. – filbranden Jun 21 at 1:42
  • actually, one question about this. Why is J being used (0x4A) instead of \n (0x0A) for this? ie., it seems like they are adding 0x40 (64) to the original hex code for its display? Here's an image: imgur.com/a/Wy9WQc2. – David542 Jun 21 at 18:08
  • @David542 I mixed up on my answer... Just updated it. Yes, ^J is "\n" and not "\r". – filbranden Jun 21 at 18:21
  • out of curiosity, why does it do: The Control key masks bit 0x40, so the end result of using it with those letters on the right is the corresponding control character on the left. so that it doesn't display the character basically "as-is", ie NUL for null. – David542 Jun 21 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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