Reputation: 1871
The man page of cat says:
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
What is the M-
notation and where is it documented?
Example:
$cat log -A
wrote 262144 bytes from file test.x in 9.853947s (25.979 KiB/s)^M$
^M> ^H^H ^H^H>
What do ^M
and ^H
mean?
Upvotes: 26
Views: 11070
Reputation: 41962
You can see the definition in the key_name(3)
manpage
Likewise, the
meta
(3X) function allows the caller to change the output ofkeyname
, i.e., it determines whether to use the “M-” prefix for “meta” keys (codes in the range 128 to 255). Bothuse_legacy_coding
(3X) andmeta
(3X) succeed only after curses is initialized. X/Open Curses does not document the treatment of codes 128 to 159. When treating them as “meta” keys (or ifkeyname
is called before initializing curses), this implementation returns strings“M-^@”
,“M-^A”
, etc.
So basically the Meta analog of the Ctrl version is the keycode of Ctrl + 128. You can see that easily in Brian's table. Here's a slightly modified version for ease of comparison
$ LC_ALL=C perl -e 'for( my $i=0 ; $i < 128; $i++ ) {
print ( sprintf( "%c is %d %x\t\t%c is %d %x\n",
$i, $i, $i, $i + 128, $i + 128, $i + 128 ) );
}' >bytes.txt
$ cat -v bytes.txt
^@ is 0 0 M-^@ is 128 80
^A is 1 1 M-^A is 129 81
^B is 2 2 M-^B is 130 82
^C is 3 3 M-^C is 131 83
...
^Y is 25 19 M-^Y is 153 99
^Z is 26 1a M-^Z is 154 9a
^[ is 27 1b M-^[ is 155 9b
^\ is 28 1c M-^\ is 156 9c
^] is 29 1d M-^] is 157 9d
^^ is 30 1e M-^^ is 158 9e
^_ is 31 1f M-^_ is 159 9f
is 32 20 M- is 160 a0
! is 33 21 M-! is 161 a1
" is 34 22 M-" is 162 a2
# is 35 23 M-# is 163 a3
$ is 36 24 M-$ is 164 a4
% is 37 25 M-% is 165 a5
& is 38 26 M-& is 166 a6
' is 39 27 M-' is 167 a7
( is 40 28 M-( is 168 a8
) is 41 29 M-) is 169 a9
* is 42 2a M-* is 170 aa
+ is 43 2b M-+ is 171 ab
, is 44 2c M-, is 172 ac
- is 45 2d M-- is 173 ad
. is 46 2e M-. is 174 ae
/ is 47 2f M-/ is 175 af
0 is 48 30 M-0 is 176 b0
1 is 49 31 M-1 is 177 b1
...
: is 58 3a M-: is 186 ba
; is 59 3b M-; is 187 bb
< is 60 3c M-< is 188 bc
= is 61 3d M-= is 189 bd
> is 62 3e M-> is 190 be
? is 63 3f M-? is 191 bf
@ is 64 40 M-@ is 192 c0
A is 65 41 M-A is 193 c1
B is 66 42 M-B is 194 c2
...
Z is 90 5a M-Z is 218 da
[ is 91 5b M-[ is 219 db
\ is 92 5c M-\ is 220 dc
] is 93 5d M-] is 221 dd
^ is 94 5e M-^ is 222 de
_ is 95 5f M-_ is 223 df
` is 96 60 M-` is 224 e0
a is 97 61 M-a is 225 e1
b is 98 62 M-b is 226 e2
...
z is 122 7a M-z is 250 fa
{ is 123 7b M-{ is 251 fb
| is 124 7c M-| is 252 fc
} is 125 7d M-} is 253 fd
~ is 126 7e M-~ is 254 fe
^? is 127 7f M-^? is 255 ff
The part after M-
on the right is exactly the same as on the left, with the keycodes differ by 128
You can also check cat's source code, the basic expression is *bpout++ = ch - 128;
for the Meta key version in the show_nonprinting
case
Upvotes: 1
Reputation: 11
Answer from the book.
Unix power tools.
25.7 Show Non-Printing Characters with cat -v or od -c.
cat -v
has its own symbol for characters outside the ASCII range with their high bits set, also called metacharacters.cat -v
prints those asM-
followed by another character. There are two of them in thecat -v
output:M-^?
andM-a
. To get a metacharacter, you add 200 octal. "Say what?" Let's look atM-a
first. The octal value of the letter a is 141. Whencat -v
printsM-a
, it means the character you get by adding 141+200, or 341 octal. You can decode the character cat prints asM-^
? in the same way. The^?
stands for the DEL character, which is octal 177. Add 200+177 to get 377 octal.
Upvotes: 1
Reputation: 2493
coreutils cat src
https://github.com/coreutils/coreutils/blob/v9.3/src/cat.c#L415
busybox cat src
https://github.com/mirror/busybox/blob/1_36_0/libbb/printable.c#L43
if (ch >= 128) {
ch -= 128;
*buf++ = 'M';
*buf++ = '-';
}
if (ch < 32 || ch == 127) {
*buf++ = '^';
ch ^= 0x40;
}
?
// equal to \x7F
# setup raw input: so that ctrl+c, ctrl+d not terminate cat
$ stty raw
# try input: ctrl+a, ctrl+b.. //but alt+a, alt+b not got M-A, M-B
$ cat | hexdump -ve '1/1 " %02x\r\n"'
^@ 00
^A 01
^B 02
^C 03
^D 04
^E 05
^F 06
# end test, run in another terminal
$ killall hexdump
Upvotes: 1
Reputation: 4357
I am not sure about the M-
notation, but the ones involving ^
uses the caret notation:
Caret notation is a notation for control characters in ASCII.
In particular,
The digraph stands for the control character whose ASCII code is the same as the character's ASCII code with the uppermost bit, in a 7-bit encoding, reversed.
which you can verify by looking at the ASCII binary (octal) representation:
Image source: http://www.asciitable.com
Because ASCII is such a limited character set (as you can see above), it's straightforward to list all control chars representable by the caret notation, e.g., http://xahlee.info/comp/unicode_character_representation.html.
Upvotes: 1
Reputation: 7986
I was wondering this too. I checked the source but it seemed easier to create a input file to get the mapping.
I created a test input file with a Perl scrip for( my $i=0 ; $i < 256; $i++ ) { print ( sprintf( "%c is %d %x\n", $i, $i ,$i ) ); }
and then ran it through cat -v
Also if you see M-oM-;M-? at the start of a file it is the UTF-8 byte order mark.
Scroll down through these to get to the M- values:
^@ is 0 0
^A is 1 1
^B is 2 2
^C is 3 3
^D is 4 4
^E is 5 5
^F is 6 6
^G is 7 7
^H is 8 8
(9 is tab)
(10 is NL)
^K is 11 b
^L is 12 c
^M is 13 d
^N is 14 e
^O is 15 f
^P is 16 10
^Q is 17 11
^R is 18 12
^S is 19 13
^T is 20 14
^U is 21 15
^V is 22 16
^W is 23 17
^X is 24 18
^Y is 25 19
^Z is 26 1a
^[ is 27 1b
^\ is 28 1c
^] is 29 1d
^^ is 30 1e
^_ is 31 1f
...printing chars removed...
^? is 127 7f
M-^@ is 128 80
M-^A is 129 81
M-^B is 130 82
M-^C is 131 83
M-^D is 132 84
M-^E is 133 85
M-^F is 134 86
M-^G is 135 87
M-^H is 136 88
M-^I is 137 89
M-^J is 138 8a
M-^K is 139 8b
M-^L is 140 8c
M-^M is 141 8d
M-^N is 142 8e
M-^O is 143 8f
M-^P is 144 90
M-^Q is 145 91
M-^R is 146 92
M-^S is 147 93
M-^T is 148 94
M-^U is 149 95
M-^V is 150 96
M-^W is 151 97
M-^X is 152 98
M-^Y is 153 99
M-^Z is 154 9a
M-^[ is 155 9b
M-^\ is 156 9c
M-^] is 157 9d
M-^^ is 158 9e
M-^_ is 159 9f
M- is 160 a0
M-! is 161 a1
M-" is 162 a2
M-# is 163 a3
M-$ is 164 a4
M-% is 165 a5
M-& is 166 a6
M-' is 167 a7
M-( is 168 a8
M-) is 169 a9
M-* is 170 aa
M-+ is 171 ab
M-, is 172 ac
M-- is 173 ad
M-. is 174 ae
M-/ is 175 af
M-0 is 176 b0
M-1 is 177 b1
M-2 is 178 b2
M-3 is 179 b3
M-4 is 180 b4
M-5 is 181 b5
M-6 is 182 b6
M-7 is 183 b7
M-8 is 184 b8
M-9 is 185 b9
M-: is 186 ba
M-; is 187 bb
M-< is 188 bc
M-= is 189 bd
M-> is 190 be
M-? is 191 bf
M-@ is 192 c0
M-A is 193 c1
M-B is 194 c2
M-C is 195 c3
M-D is 196 c4
M-E is 197 c5
M-F is 198 c6
M-G is 199 c7
M-H is 200 c8
M-I is 201 c9
M-J is 202 ca
M-K is 203 cb
M-L is 204 cc
M-M is 205 cd
M-N is 206 ce
M-O is 207 cf
M-P is 208 d0
M-Q is 209 d1
M-R is 210 d2
M-S is 211 d3
M-T is 212 d4
M-U is 213 d5
M-V is 214 d6
M-W is 215 d7
M-X is 216 d8
M-Y is 217 d9
M-Z is 218 da
M-[ is 219 db
M-\ is 220 dc
M-] is 221 dd
M-^ is 222 de
M-_ is 223 df
M-` is 224 e0
M-a is 225 e1
M-b is 226 e2
M-c is 227 e3
M-d is 228 e4
M-e is 229 e5
M-f is 230 e6
M-g is 231 e7
M-h is 232 e8
M-i is 233 e9
M-j is 234 ea
M-k is 235 eb
M-l is 236 ec
M-m is 237 ed
M-n is 238 ee
M-o is 239 ef
M-p is 240 f0
M-q is 241 f1
M-r is 242 f2
M-s is 243 f3
M-t is 244 f4
M-u is 245 f5
M-v is 246 f6
M-w is 247 f7
M-x is 248 f8
M-y is 249 f9
M-z is 250 fa
M-{ is 251 fb
M-| is 252 fc
M-} is 253 fd
M-~ is 254 fe
M-^? is 255 ff
Upvotes: 20
Reputation: 17159
^M
is for Control-M (a carriage return), ^H
for Control-H (a backspace). M-Something
is Meta-Something (Meta- is what the Alt key does in some terminals).
Upvotes: 4