1*84d9c625SLionel Sambuc# @(#)quoting 5.5 (Berkeley) 11/12/94 2*84d9c625SLionel Sambuc 3*84d9c625SLionel SambucQUOTING IN EX/VI: 4*84d9c625SLionel Sambuc 5*84d9c625SLionel SambucThere are four escape characters in historic ex/vi: 6*84d9c625SLionel Sambuc 7*84d9c625SLionel Sambuc \ (backslashes) 8*84d9c625SLionel Sambuc ^V 9*84d9c625SLionel Sambuc ^Q (assuming it wasn't used for IXON/IXOFF) 10*84d9c625SLionel Sambuc The terminal literal next character. 11*84d9c625SLionel Sambuc 12*84d9c625SLionel SambucVi did not use the lnext character, it always used ^V (or ^Q). 13*84d9c625SLionel Sambuc^V and ^Q were equivalent in all cases for vi. 14*84d9c625SLionel Sambuc 15*84d9c625SLionel SambucThere are four different areas in ex/vi where escaping characters 16*84d9c625SLionel Sambucis interesting: 17*84d9c625SLionel Sambuc 18*84d9c625SLionel Sambuc 1: In vi text input mode. 19*84d9c625SLionel Sambuc 2: In vi command mode. 20*84d9c625SLionel Sambuc 3: In ex command and text input modes. 21*84d9c625SLionel Sambuc 4: In the ex commands themselves. 22*84d9c625SLionel Sambuc 23*84d9c625SLionel Sambuc1: Vi text input mode (a, i, o, :colon commands, etc.): 24*84d9c625SLionel Sambuc 25*84d9c625SLionel Sambuc The set of characters that users might want to escape are as follows. 26*84d9c625SLionel Sambuc As ^L and ^Z were not special in input mode, they are not listed. 27*84d9c625SLionel Sambuc 28*84d9c625SLionel Sambuc carriage return (^M) 29*84d9c625SLionel Sambuc escape (^[) 30*84d9c625SLionel Sambuc autoindents (^D, 0, ^, ^T) 31*84d9c625SLionel Sambuc erase (^H) 32*84d9c625SLionel Sambuc word erase (^W) 33*84d9c625SLionel Sambuc line erase (^U) 34*84d9c625SLionel Sambuc newline (^J) (not historic practice) 35*84d9c625SLionel Sambuc 36*84d9c625SLionel Sambuc Historic practice was that ^V was the only way to escape any 37*84d9c625SLionel Sambuc of these characters, and that whatever character followed 38*84d9c625SLionel Sambuc the ^V was taken literally, e.g. ^V^V is a single ^V. I 39*84d9c625SLionel Sambuc don't see any strong reason to make it possible to escape 40*84d9c625SLionel Sambuc ^J, so I'm going to leave that alone. 41*84d9c625SLionel Sambuc 42*84d9c625SLionel Sambuc One comment regarding the autoindent characters. In historic 43*84d9c625SLionel Sambuc vi, if you entered "^V0^D" autoindent erasure was still 44*84d9c625SLionel Sambuc triggered, although it wasn't if you entered "0^V^D". In 45*84d9c625SLionel Sambuc nvi, if you escape either character, autoindent erasure is 46*84d9c625SLionel Sambuc not triggered. 47*84d9c625SLionel Sambuc 48*84d9c625SLionel Sambuc Abbreviations were not performed if the non-word character 49*84d9c625SLionel Sambuc that triggered the abbreviation was escaped by a ^V. Input 50*84d9c625SLionel Sambuc maps were not triggered if any part of the map was escaped 51*84d9c625SLionel Sambuc by a ^V. 52*84d9c625SLionel Sambuc 53*84d9c625SLionel Sambuc The historic vi implementation for the 'r' command requires 54*84d9c625SLionel Sambuc two leading ^V's to replace a character with a literal 55*84d9c625SLionel Sambuc character. This is obviously a bug, and should be fixed. 56*84d9c625SLionel Sambuc 57*84d9c625SLionel Sambuc2: Vi command mode 58*84d9c625SLionel Sambuc 59*84d9c625SLionel Sambuc Command maps were not triggered if the second or later 60*84d9c625SLionel Sambuc character of a map was escaped by a ^V. 61*84d9c625SLionel Sambuc 62*84d9c625SLionel Sambuc The obvious extension is that ^V should keep the next command 63*84d9c625SLionel Sambuc character from being mapped, so you can do ":map x xxx" and 64*84d9c625SLionel Sambuc then enter ^Vx to delete a single character. 65*84d9c625SLionel Sambuc 66*84d9c625SLionel Sambuc3: Ex command and text input modes. 67*84d9c625SLionel Sambuc 68*84d9c625SLionel Sambuc As ex ran in canonical mode, there was little work that it 69*84d9c625SLionel Sambuc needed to do for quoting. The notable differences between 70*84d9c625SLionel Sambuc ex and vi are that it was possible to escape a <newline> in 71*84d9c625SLionel Sambuc the ex command and text input modes, and ex used the "literal 72*84d9c625SLionel Sambuc next" character, not control-V/control-Q. 73*84d9c625SLionel Sambuc 74*84d9c625SLionel Sambuc4: The ex commands: 75*84d9c625SLionel Sambuc 76*84d9c625SLionel Sambuc Ex commands are delimited by '|' or newline characters. 77*84d9c625SLionel Sambuc Within the commands, whitespace characters delimit the 78*84d9c625SLionel Sambuc arguments. Backslash will generally escape any following 79*84d9c625SLionel Sambuc character. In the abbreviate, unabbreviate, map and unmap 80*84d9c625SLionel Sambuc commands, control-V escapes the next character, instead. 81*84d9c625SLionel Sambuc 82*84d9c625SLionel Sambuc This is historic behavior in vi, although there are special 83*84d9c625SLionel Sambuc cases where it's impossible to escape a character, generally 84*84d9c625SLionel Sambuc a whitespace character. 85*84d9c625SLionel Sambuc 86*84d9c625SLionel Sambuc Escaping characters in file names in ex commands: 87*84d9c625SLionel Sambuc 88*84d9c625SLionel Sambuc :cd [directory] (directory) 89*84d9c625SLionel Sambuc :chdir [directory] (directory) 90*84d9c625SLionel Sambuc :edit [+cmd] [file] (file) 91*84d9c625SLionel Sambuc :ex [+cmd] [file] (file) 92*84d9c625SLionel Sambuc :file [file] (file) 93*84d9c625SLionel Sambuc :next [file ...] (file ...) 94*84d9c625SLionel Sambuc :read [!cmd | file] (file) 95*84d9c625SLionel Sambuc :source [file] (file) 96*84d9c625SLionel Sambuc :write [!cmd | file] (file) 97*84d9c625SLionel Sambuc :wq [file] (file) 98*84d9c625SLionel Sambuc :xit [file] (file) 99*84d9c625SLionel Sambuc 100*84d9c625SLionel Sambuc Since file names are also subject to word expansion, the 101*84d9c625SLionel Sambuc underlying shell had better be doing the correct backslash 102*84d9c625SLionel Sambuc escaping. This is NOT historic behavior in vi, making it 103*84d9c625SLionel Sambuc impossible to insert a whitespace, newline or carriage return 104*84d9c625SLionel Sambuc character into a file name. 105*84d9c625SLionel Sambuc 106*84d9c625SLionel Sambuc4: Escaping characters in non-file arguments in ex commands: 107*84d9c625SLionel Sambuc 108*84d9c625SLionel Sambuc :abbreviate word string (word, string) 109*84d9c625SLionel Sambuc* :edit [+cmd] [file] (+cmd) 110*84d9c625SLionel Sambuc* :ex [+cmd] [file] (+cmd) 111*84d9c625SLionel Sambuc :map word string (word, string) 112*84d9c625SLionel Sambuc* :set [option ...] (option) 113*84d9c625SLionel Sambuc* :tag string (string) 114*84d9c625SLionel Sambuc :unabbreviate word (word) 115*84d9c625SLionel Sambuc :unmap word (word) 116*84d9c625SLionel Sambuc 117*84d9c625SLionel Sambuc These commands use whitespace to delimit their arguments, and use 118*84d9c625SLionel Sambuc ^V to escape those characters. The exceptions are starred in the 119*84d9c625SLionel Sambuc above list, and are discussed below. 120*84d9c625SLionel Sambuc 121*84d9c625SLionel Sambuc In general, I intend to treat a ^V in any argument, followed by 122*84d9c625SLionel Sambuc any character, as that literal character. This will permit 123*84d9c625SLionel Sambuc editing of files name "foo|", for example, by using the string 124*84d9c625SLionel Sambuc "foo\^V|", where the literal next character protects the pipe 125*84d9c625SLionel Sambuc from the ex command parser and the backslash protects it from the 126*84d9c625SLionel Sambuc shell expansion. 127*84d9c625SLionel Sambuc 128*84d9c625SLionel Sambuc This is backward compatible with historical vi, although there 129*84d9c625SLionel Sambuc were a number of special cases where vi wasn't consistent. 130*84d9c625SLionel Sambuc 131*84d9c625SLionel Sambuc4.1: The edit/ex commands: 132*84d9c625SLionel Sambuc 133*84d9c625SLionel Sambuc The edit/ex commands are a special case because | symbols may 134*84d9c625SLionel Sambuc occur in the "+cmd" field, for example: 135*84d9c625SLionel Sambuc 136*84d9c625SLionel Sambuc :edit +10|s/abc/ABC/ file.c 137*84d9c625SLionel Sambuc 138*84d9c625SLionel Sambuc In addition, the edit and ex commands have historically 139*84d9c625SLionel Sambuc ignored literal next characters in the +cmd string, so that 140*84d9c625SLionel Sambuc the following command won't work. 141*84d9c625SLionel Sambuc 142*84d9c625SLionel Sambuc :edit +10|s/X/^V / file.c 143*84d9c625SLionel Sambuc 144*84d9c625SLionel Sambuc I intend to handle the literal next character in edit/ex consistently 145*84d9c625SLionel Sambuc with how it is handled in other commands. 146*84d9c625SLionel Sambuc 147*84d9c625SLionel Sambuc More fun facts to know and tell: 148*84d9c625SLionel Sambuc The acid test for the ex/edit commands: 149*84d9c625SLionel Sambuc 150*84d9c625SLionel Sambuc date > file1; date > file2 151*84d9c625SLionel Sambuc vi 152*84d9c625SLionel Sambuc :edit +1|s/./XXX/|w file1| e file2|1 | s/./XXX/|wq 153*84d9c625SLionel Sambuc 154*84d9c625SLionel Sambuc No version of vi, of which I'm aware, handles it. 155*84d9c625SLionel Sambuc 156*84d9c625SLionel Sambuc4.2: The set command: 157*84d9c625SLionel Sambuc 158*84d9c625SLionel Sambuc The set command treats ^V's as literal characters, so the 159*84d9c625SLionel Sambuc following command won't work. Backslashes do work in this 160*84d9c625SLionel Sambuc case, though, so the second version of the command does work. 161*84d9c625SLionel Sambuc 162*84d9c625SLionel Sambuc set tags=tags_file1^V tags_file2 163*84d9c625SLionel Sambuc set tags=tags_file1\ tags_file2 164*84d9c625SLionel Sambuc 165*84d9c625SLionel Sambuc I intend to continue permitting backslashes in set commands, 166*84d9c625SLionel Sambuc but to also permit literal next characters to work as well. 167*84d9c625SLionel Sambuc This is backward compatible, but will also make set 168*84d9c625SLionel Sambuc consistent with the other commands. I think it's unlikely 169*84d9c625SLionel Sambuc to break any historic .exrc's, given that there are probably 170*84d9c625SLionel Sambuc very few files with ^V's in their name. 171*84d9c625SLionel Sambuc 172*84d9c625SLionel Sambuc4.3: The tag command: 173*84d9c625SLionel Sambuc 174*84d9c625SLionel Sambuc The tag command ignores ^V's and backslashes; there's no way to 175*84d9c625SLionel Sambuc get a space into a tag name. 176*84d9c625SLionel Sambuc 177*84d9c625SLionel Sambuc I think this is a don't care, and I don't intend to fix it. 178*84d9c625SLionel Sambuc 179*84d9c625SLionel Sambuc5: Regular expressions: 180*84d9c625SLionel Sambuc 181*84d9c625SLionel Sambuc :global /pattern/ command 182*84d9c625SLionel Sambuc :substitute /pattern/replace/ 183*84d9c625SLionel Sambuc :vglobal /pattern/ command 184*84d9c625SLionel Sambuc 185*84d9c625SLionel Sambuc I intend to treat a backslash in the pattern, followed by the 186*84d9c625SLionel Sambuc delimiter character or a backslash, as that literal character. 187*84d9c625SLionel Sambuc 188*84d9c625SLionel Sambuc This is historic behavior in vi. It would get rid of a fairly 189*84d9c625SLionel Sambuc hard-to-explain special case if we could just use the character 190*84d9c625SLionel Sambuc immediately following the backslash in all cases, or, if we 191*84d9c625SLionel Sambuc changed nvi to permit using the literal next character as a 192*84d9c625SLionel Sambuc pattern escape character, but that would probably break historic 193*84d9c625SLionel Sambuc scripts. 194*84d9c625SLionel Sambuc 195*84d9c625SLionel Sambuc There is an additional escaping issue for regular expressions. 196*84d9c625SLionel Sambuc Within the pattern and replacement, the '|' character did not 197*84d9c625SLionel Sambuc delimit ex commands. For example, the following is legal. 198*84d9c625SLionel Sambuc 199*84d9c625SLionel Sambuc :substitute /|/PIPE/|s/P/XXX/ 200*84d9c625SLionel Sambuc 201*84d9c625SLionel Sambuc This is a special case that I will support. 202*84d9c625SLionel Sambuc 203*84d9c625SLionel Sambuc6: Ending anything with an escape character: 204*84d9c625SLionel Sambuc 205*84d9c625SLionel Sambuc In all of the above rules, an escape character (either ^V or a 206*84d9c625SLionel Sambuc backslash) at the end of an argument or file name is not handled 207*84d9c625SLionel Sambuc specially, but used as a literal character. 208*84d9c625SLionel Sambuc 209