Re: Replacing '%' in a text file
--- Tim Chase <[EMAIL PROTECTED]> wrote: > > I have to replace every occurrence of % in a file > with > > % |. I have been effectively replacing text using > the > > following construct: > > > > :%s/\/replacement/g > > > > However when I try to do the following: > > > > :%s/\<%\>/% |/g > > > > I am greeted by an error message. Obviously, the % > > character needs to be treated differently for > being > > replaced. Escap sequence? > > The error message returned should give a clue > regarding the > problem ("E486: Pattern not found: \<%\>"). Your > pattern > "\" works well for words, ensuring that you > don't find > them as a sub-portion of some other word (such as > finding the > "foo" in "food", "snafoo", or "confoosion"). > However, the "\<" > and "\>" tokens require a transition from a > non-word-character to > a word-character (or vice-versa). The "%" > character, by default, > is not a key-word character (though this can be > altered by > changing the 'iskeyword' setting). > > Unless there is some context in which you *don't* > want to replace > a "%" with "% |", you can just use > > :%s/%/% |/g > > without the "\<" and "\>" markers. You can read > more about the > problematic operators at > > :help /\< > > or making them part of the set of characters that > constitute a > keyword, by reading at > > :help 'iskeyword' > > HTH, > > -tim > Thanks. The problem is now resolved. -- Muhammad Farooq-i-Azam __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Re: Replacing '%' in a text file
Hello, --- "A.J.Mechelynck" <[EMAIL PROTECTED]> wrote: > Muhammad Farooq-i-Azam wrote: > > I have to replace every occurrence of % in a file with > > % |. I have been effectively replacing text using the > > following construct: > > > > :%s/\/replacement/g > > > > However when I try to do the following: > > > > :%s/\<%\>/% |/g A.J. is right, the \< and \> won't work because there's no word boundary there. Are you trying to say 'replace % when surrounded by whitespace?' If so, you could use: :%s/\s\@<[EMAIL PROTECTED]/% |/g regards, Peter On Yahoo!7 Men's Health: What music do you want to hear on Men's Health Radio? http://www.menshealthmagazine.com.au/
Re: Replacing '%' in a text file
Muhammad Farooq-i-Azam wrote: I have to replace every occurrence of % in a file with % |. I have been effectively replacing text using the following construct: :%s/\/replacement/g However when I try to do the following: :%s/\<%\>/% |/g I am greeted by an error message. Obviously, the % character needs to be treated differently for being replaced. Escap sequence? I cannot figure out how to do it. May be trivial for the gurus here. I will be thankful for a hint. Many thanks in advance. -- Muhammad Farooq-i-Azam Which message do you encounter? If it's "E486: Pattern not found: \<%\>" it means that Vim doesn't find a match. I guess the boundary between space and %, or % and space, is not seen as a word boundary. What is 'iskeyword' set to? By default, nothing lower than 48 (0x30 i.e., the digit zero) is a keyword character. Now % is 0x25, i.e., it is not included. Thus it is not seen as part of a word, and therefore it cannot be preceded by a begin-of word or followed by an end-of-word. I suggest the following code snippet: let save_isk = &isk setlocal isk+=% %s/\<%\>/% |/g let &l:isk = save_isk If you can afford to replace every percent sign regardless of what precedes or follows it, you can do simply :%s/%/% |/g Best regards, Tony.
Re: Replacing '%' in a text file
I create a test file and was able to replace all % with % | by doing this: :%s/%/% |/g You shouldn't need to escape the % Muhammad Farooq-i-Azam wrote: I have to replace every occurrence of % in a file with % |. I have been effectively replacing text using the following construct: :%s/\/replacement/g However when I try to do the following: :%s/\<%\>/% |/g I am greeted by an error message. Obviously, the % character needs to be treated differently for being replaced. Escap sequence? I cannot figure out how to do it. May be trivial for the gurus here. I will be thankful for a hint. Many thanks in advance. -- Muhammad Farooq-i-Azam __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- Scot P. Floess 27 Lake Royale Louisburg, NC 27549 252-478-8087 (Home) 919-754-4592 (Work) Chief Architect JPlate http://sourceforge.net/projects/jplate Chief Architect JavaPIM http://sourceforge.net/projects/javapim
Re: Replacing '%' in a text file
I have to replace every occurrence of % in a file with % |. I have been effectively replacing text using the following construct: :%s/\/replacement/g However when I try to do the following: :%s/\<%\>/% |/g I am greeted by an error message. Obviously, the % character needs to be treated differently for being replaced. Escap sequence? The error message returned should give a clue regarding the problem ("E486: Pattern not found: \<%\>"). Your pattern "\" works well for words, ensuring that you don't find them as a sub-portion of some other word (such as finding the "foo" in "food", "snafoo", or "confoosion"). However, the "\<" and "\>" tokens require a transition from a non-word-character to a word-character (or vice-versa). The "%" character, by default, is not a key-word character (though this can be altered by changing the 'iskeyword' setting). Unless there is some context in which you *don't* want to replace a "%" with "% |", you can just use :%s/%/% |/g without the "\<" and "\>" markers. You can read more about the problematic operators at :help /\< or making them part of the set of characters that constitute a keyword, by reading at :help 'iskeyword' HTH, -tim
Re: Replacing '%' in a text file
Hi, Muhammad Farooq-i-Azam wrote: > > I have to replace every occurrence of % in a file with > % |. I have been effectively replacing text using the > following construct: > > :%s/\/replacement/g > > However when I try to do the following: > > :%s/\<%\>/% |/g > > I am greeted by an error message. Obviously, the % > character needs to be treated differently for being > replaced. Escap sequence? I cannot figure out how to > do it. May be trivial for the gurus here. I will be > thankful for a hint. normally % is not included in the 'iskeyword' option so it is not considered part of a word. Therefore there can not be the beginning of a word right in front of %. The same is for true for the end of a word immediately after a %. You either have to include % in the 'iskeyword' option by issuing a :set iskeyword+=% before executing your substitute command or use :%s/%/% |/g without '\<' and '\>', respectively. Regards, Jürgen -- Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us. (Calvin)
Replacing '%' in a text file
I have to replace every occurrence of % in a file with % |. I have been effectively replacing text using the following construct: :%s/\/replacement/g However when I try to do the following: :%s/\<%\>/% |/g I am greeted by an error message. Obviously, the % character needs to be treated differently for being replaced. Escap sequence? I cannot figure out how to do it. May be trivial for the gurus here. I will be thankful for a hint. Many thanks in advance. -- Muhammad Farooq-i-Azam __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com