I am trying to map q in vi (not Vim) to find a pattern and replace it with newline:Name1 Name2 Name3 Name4toName1Name2Name3Name4When I try ([Ctrl-V] and [Enter] are actual keystrokes):map q :%s/Name. /[Ctrl-V][Enter]&/g[Ctrl-V][Enter]It fails, since it considers the first [Ctrl-V][Enter] itself as enter. I don't know how to make it a literal newline.I have read Carriage return vs newline in vim and it suggests using \r for newline while replacing. This seems only to work in Vim and does not work under vi.Please help.
How do I map a key to search for a pattern and replace it with newline in ex/vi?
ex mode;original vi
This will be a bit convoluted, but, try::map q :%s/Name. /\^V^V^M&/g^MEach ^x is produced by pressing CtrlVCtrlx, so the actual sequence here is:CtrlV CtrlV for the first ^V,CtrlV CtrlV for the second ^V,CtrlV Enter for the first ^M.And again for the last ^M.From man ex:Lines may be split by substituting new-line characters into them. The  newline in repl must be escaped by preceding it with a '\'. Other  metacharacters available in pat and repl are described below.And from An Introduction to Display Editing with Vi:You can use the map command from vi (typically in your EXINIT)  with a command of the form::map lhs rhsCRmapping lhs into rhs. There are restrictions: lhs should be one  keystroke (either 1 character or one function key) since it must be  entered within one second (unless notimeout is set, in which case  you can type it as slowly as you wish, and vi will wait for you to  finish it before it echoes anything). The lhs can be no longer than  10 characters, the rhs no longer than 100. To get a space, tab or  newline into lhs or rhs you should escape them with a ^V. (It  may be necessary to double the ^V if the map command is given  inside vi, rather than in ex.) Spaces and tabs inside the rhs  need not be escaped.So:We need to prefix the newline given to :s with a \.If we were typing the command in vi, then we'd press CtrlV CtrlM to get the newline in the command. That means we have to make the mapping so that it would be as if we had pressed CtrlV CtrlM.We need CtrlV CtrlV just to get one ^V to the command. But we need two, so that it would be as if we'd typed CtrlV.The reduction would be like this:^V^V^V^V^V^M typedWhich the map command will receive as ^V^V^MWhich will be interpreted while executing the map as ^V^MTo get a literal ^M.After all that, the output still won't be what you wanted:Name1Name2Name3 Name4