X-mailer: m, by Cameron Simpson Date: Mon, 29 Mar 1999 00:51:34 +0000 Subject: Re: use substitute in VI editor References: <36FEBD38.2CB7E704@geocities.com> To: redhat-list@redhat.com CC: Zaigui Wang From: Cameron Simpson Reply-to: cs@zip.com.au Errors-to: cs@zip.com.au Return-receipt-to: cs@zip.com.au Organization: Canon Information Systems Research Australia, Sydney, Oz | Zaigui Wang wrote: | > I like the substitute feature in the Vi editor very much, [...] | > 1. What do you do if you want an empty line added before a certain word, | > such as "Chapter", Jono wrote: | 1. Maybe try something like s/Chapter/'\nChapter'/g | and mess around with the quotes. What is it with people giving vague and untested advice on this list? There are no quotes in vi. Jeez. The traditional method is to put a literal ctrl-M in the substitution; vi turns these into newlines. So you might type: s/thisnthat/this^V^Mthat/ where ^V and ^M are literal ctrl-V and ctrl-M; you need the ^V to escape the ^M, which otherwise looks just like you pressed . The above will insert a break between "this" and "that". So to insert a leading blank line you'd say: :g/Chapter/s/^/^V^M/ which would say for each line containing the word "Chapter", put a blank line that the front or if you want the blank line immediately before the word: :%s/Chapter/^V^M&/g which says on every line (the "%") replace "Chapter" with a line break and itself (the "&" means the text matched by the first half of the substitute) | > or how do you change an empty line to something like | > "*********" :g/^$/s/$/********* or :%s/^$/********* The first says: for every blank line (i.e the start "^" is right next to the end "$") replace the end with ********* The second says: for every line, replace the empty string with ********* provided the empty start is next to the start of the line and also next to the end (since "^$" is "start of line, empty string, end of line" ZW> 2. How to limit the substitute to exact match? If I want to replace a ZW> variable named "i" with "count", I surely do not want to make every "int" ZW> "countnt", or "if" "countf". Jono> 2. If it is a varriable instead of replacing 'i', try replacing '$i' or Jono> '$i=', that way you can be sure to get just that varriable. Yucko. This is very error prone. Many languages don't mark up variables with $ signs. Vi has a handy pattern syntax called \< and \>. \< means "start of a word" and \> means "end of a word". So the usual approach is: :%s/\/count/g which says on every line, replace complete words spelt "i" with "count" Of could, you're still at the mercy of "i" (or whatever) appearing in some quoted string. For this reason I tend to say: :g/\/s/\/count/gp which says, for each line containing "i" as a complete word, replace every "i" with "count" and print the changed line That way you can view the changed lines to watch for some unfortunate change. If there are few of these, it's best to chase them down after the substitute and fix them one by one. If there are many undesired changes, it is better to back out the entire substitute (by typing "u" - the undo command - it will undo any single change, however large). Then consider a better approach (like a more rerstrictive pattern match). If there really isn't a neat way to match only what you want, and provided the number of changes is tractable, you can take the following approach: - search for the target pattern or some good approximation, eg: /\ - once there, make the change interactively with a single vi command, eg: cwcountESC (where that "ESC" is a press of the "Escape" key). The above says change word "count" since the cursor is at the "i" from the search, the "word" is "i" - now you have a "rprevious search" established (the "/\"), and a "previous command" (the "cwcountESC") Typing "n" will go to the next occurence of the search (i.e the next "i") Typing "." will repeat the previous command (i.e. the "cwcountESC") - so you can keep typing "n" and whenever you hit an "i" which should be changed, type "." and it will be changed if the "i" should not be changed, just type "n" again to go the the next possibility Cheers, -- Cameron Simpson, DoD#743 cs@zip.com.au http://www.zip.com.au/~cs/ I'm a man of my word. In the end, that's all there is. - Avon