Skip to main content
deleted 20 characters in body
Source Link
Rich
  • 32.6k
  • 4
  • 74
  • 141

Use lookarounds

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 aanother < character.

How it works

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:

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

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

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

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

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

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

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

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:

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

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

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

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.

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

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.

Use lookarounds

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

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

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

How it works

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:

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

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

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

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

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

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

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

added 849 characters in body
Source Link
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.:

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

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

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

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.

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

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.

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.

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:

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

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

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

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.

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

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.

added 849 characters in body
Source Link
Rich
  • 32.6k
  • 4
  • 74
  • 141

I think the simplest regular expression: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.

I think the simplest regular expression might be to use lookarounds:

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

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.

Source Link
Rich
  • 32.6k
  • 4
  • 74
  • 141
Loading