Skip to main content
2 of 4
added 849 characters in body
Rich
  • 32.6k
  • 4
  • 74
  • 141

I think the simplest :substitute command might be to use lookarounds:

:%s/<\@<!<<\@!/"/g

This replaces every single < character that is neither preceded nor followed by a < character.

It uses a :substitute command (See :help :substitute) to replace strings that match the regular expression <\@<!<<\@! with " characters.

The regular expression is reasonably simple when broken down. It consists of three parts.

In the middle is a single < character. This is what we want to match.

Before that is: <\@<!. This is a second < character followed by Vim's atom for negative lookbehind: \@<!, which means that the following will only be matched if we didn't match a < here.

At the end we have: <\@!. This is a third <, this time followed by Vim's atom for negative lookahead: \@!, which means that the preceding will only be matched if what follows is not a <.

See :help \@! and :help \@<! for more details of Vim's negative lookaround atoms.

Rich
  • 32.6k
  • 4
  • 74
  • 141