4. Vim Search Patterns

Searching for text is a fundamental task in any editor. Vim provides powerful and fast ways to perform a vim search directly from normal mode. Let's explore how to use these search patterns to improve your workflow.

To perform a standard forward vim search, simply press the / key in normal mode, followed by your search term. When you press Enter, Vim will jump to the first occurrence of the term after your cursor.

My pretty file is very pretty.

/pretty

This will find the first "pretty" word after the cursor.

Similarly, you can search backward from your cursor's position. Use the ? key followed by your search term. Vim will find the first occurrence before your cursor.

My pretty file is very pretty.

?pretty

This will find the last "pretty" word in the file first.

Once a search is initiated, you can easily navigate through all the matches in the file.

  • Press n to jump to the next match in the direction of the original search.
  • Press N to jump to the previous match, moving in the opposite direction of the original search.

Effective Vim Lookup

The / and ? commands are the core of any vim lookup operation. Whether you need to find a specific function name, a variable, or just a word, this search mechanism is your primary tool. Mastering this simple vim lookup process is essential for efficient navigation and editing.

Sign in to save your learning progress

Sign in

Exercises

To apply these concepts, try the following hands-on lab. It will help you become proficient with essential text editing tools, including search functionalities.

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

Quiz

What key is used to initiate a forward search in Vim?