Advanced Text-Fu · Lesson 4 of 13

Vim Search Patterns

0 / 5 checkpoints answered

Full screen ↗

Vim searches from the current cursor position using patterns. Begin in Normal mode, enter a forward or backward search, and then repeat matches without retyping the pattern.

Searching Forward

In Normal mode, type /, enter a pattern, and press Enter. Vim moves to the next match after the cursor:

/pretty

Searches use Vim's regular-expression syntax, so characters such as ., *, [, and \ can have special meaning. Use \V at the start when the rest of a pattern should be treated as very nomagic, or escape special characters deliberately.

CheckFrom Normal mode, which command starts a forward search for pretty?

Searching Backward

Type ?, enter a pattern, and press Enter to move to the preceding match before the cursor:

?pretty

This does not inherently mean “the final match in the file.” The result depends on the current cursor position. With Vim's default wrapscan setting, a search can wrap at the beginning or end; :set nowrapscan disables that wrapping.

CheckWhich Normal-mode search prefix looks toward earlier text from the cursor?

Repeating a Search

After either kind of search:

  • Press n to repeat in the original search direction.
  • Press N to repeat in the opposite direction.

Therefore, after /pretty, n moves forward and N backward. After ?pretty, n moves backward and N forward.

CheckAfter running ?error, which key repeats the search in the same backward direction?

Searching for the Word under the Cursor

In Normal mode, place the cursor on a word and use:

  • * to search forward for that whole word.
  • # to search backward for that whole word.

These commands set the latest search pattern, so n and N can continue from it.

CheckWhich Normal-mode key searches forward for the whole word under the cursor?

Controlling Case and Highlighting

Vim options can change case behavior:

  • :set ignorecase makes searches ignore case.
  • :set smartcase makes an uppercase character restore case sensitivity when ignorecase is also set.
  • \c inside a pattern forces that search to ignore case.
  • \C forces that search to respect case.

For example, /\cerror matches error, Error, and ERROR regardless of the current case options.

When search highlighting is enabled, :nohlsearch clears the current visual highlights without deleting the search pattern. The next search or repeat can highlight matches again.

CheckWhich pattern forces one Vim search for error to ignore case regardless of the current case options?

To practice Vim navigation and search in a controlled file, try this hands-on lab:

  1. Edit Text Files in Linux with Vim and Nano - Practice creating, editing, saving, and navigating text files with Vim and Nano.

Summary

You can now search a Vim buffer and move between matches predictably.

  1. Start forward searches with / and backward searches with ?.
  2. Repeat in the same direction with n or the opposite direction with N.
  3. Search for the whole word under the cursor with * or #.
  4. Control case behavior for one pattern or through options.
  5. Clear highlights without losing the current search pattern.