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 every occurrence of the pattern, not just the first one
  • c —confirm each substitution: prompt the user before replacing the text
  • e —do not show errors if no matches are found
  • i —ignore case: make the search case-insensitive
  • I —make the search case-sensitive

You can mix and match these (except for i and I) as you see fit. For example, running :s/cat/dog/gi will turn the string cat.Cat() into dog.dog().

:substitute can be prefixed by a range, which tells it what to operate on. The most common range used with :substitute is %, which makes :s operate on the current file.

For instance, if we wanted to replace each instance of animal in a file with creature, we would run the folxlowing:

:%s/animal/creature/g

If you try it on animal_farm.py, you'll see, as in the screenshot, that every instance of animal was replaced with creature:

The :substitute command conveniently tells us how many matches were replaced in the status line at the bottom of the screen.

It seems as if we just completed a very simple case of refactoring!

:substitute supports more ranges. Here are some common ones:

  • numbersa line number
  • $—the last line in the file
  • %—a whole file (this is one of the most used ones)
  • /search-pattern/—lets you find a line to operate on
  • ?backwards-search-pattern?—does the same thing as the previous flag, but searches backwards

Furthermore, you can combine the ranges with a ; operator. For example, 20;$ will let you search from line 20 until the end of the file.

To demonstrate, the following command will search for and replace every instance of animal with creature from line 12, up to and including the line where it encounters dog:

:12;/dog/s/animal/creature/g

As you can see in the following screenshot, two instances of animal were replaced on lines 13 and 14, but not on lines 10 or 21 (I've enabled line number display by running :set nu):

You can also select a range in a visual mode, and run :s without any explicit ranges to operate on a selected text. See :help cmdline-ranges for more information on ranges.

If you find yourself working with Linux file paths (or anything with / in them), you can escape them by prefixing with a backslash (), or change the separator. For example, :s+path/to/dir+path/to/other/dir+gc is (with a separator changed to +) equivalent to :s/path/to/dir/path/to/other/dir/gc.

Most often, you will find yourself replacing all occurrences in the whole file by running the following:

:%s/find-this/replace-with-this/g

When replacing text, you may want to only search for the whole word. You can use < and > for this purpose. For example, given the following file, we can search for /animal (:set hlsearch is enabled to highlight all results), but we also get results we're not exactly interested in, such as animals:

However, if we search for /<animal>, we'll be able to match whole words only, without falsely detecting animals, as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.197