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

Search and replace

Vim supports search and replace through the :substitute command, most often abbreviated to :s. By default, :s will replace one substring with another in a current line. It has the following format:

:s/<find-this>/<replace-with-this>/<flags>

The flags are optional, and you shouldn't worry about them for now. To try it, open animal_farm.py, navigate to the line containing cat (for example, with /cat), and execute the following:

:s/cat/dog

As you can see in the screenshot, this replaces the first occurrence of cat in the current line:

Now, let's look at the flags you can pass to the substitute command:

  • g—global replace: replace ...

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