O'Reilly logo

Mastering Vim by Ruslan Osipov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Alternation and grouping

Vim has a few more special operators:

Symbol

Meaning

\|

alternation

\(\)

grouping

The alternation operator is used to signify or. For example, carrot\|parrot matches both carrot and parrot.

Grouping is used to put multiple characters in a group, which can serve two purposes. Firstly, you can combine operators with each other. For example, \(c\|p\)arrot is a nicer way to match both carrot and parrot.

Grouping can also be used to later refer to each section in parentheses. For example, if you wanted to turn the string cat hunting mice into mice hunting cat, you could use the following :substitute command:

:s/\(cat\) hunting \(mice\)/\2 hunting \1

This becomes relevant during refactoring; for example, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required