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/\<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? 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.

Reply via email to