I have to replace every occurrence of % in a file with
% |. I have been effectively replacing text using the
following construct:

:%s/\<text\>/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 "\<text\>" 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



Reply via email to