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.
Advanced Text-Fu · Lesson 4
Vim Search Patterns
Learn how to search forward or backward in Vim and repeat, refine, or clear pattern matches.
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.
From 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.
Which Normal-mode search prefix looks toward earlier text from the cursor?
Repeating a Search
After either kind of search:
- Press
nto repeat in the original search direction. - Press
Nto repeat in the opposite direction.
Therefore, after /pretty, n moves forward and N backward. After ?pretty, n moves backward and N forward.
After 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.
Which Normal-mode key searches forward for the whole word under the cursor?
Controlling Case and Highlighting
Vim options can change case behavior:
:set ignorecasemakes searches ignore case.:set smartcasemakes an uppercase character restore case sensitivity whenignorecaseis also set.\cinside a pattern forces that search to ignore case.\Cforces 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.
Which 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:
- Edit Text Files in Linux with Vim and Nano - Practice creating, editing, saving, and navigating text files with Vim and Nano.
Lesson complete
You finished Vim Search Patterns
You can now search a Vim buffer and move between matches predictably.
Start forward searches with
/and backward searches with?.Repeat in the same direction with
nor the opposite direction withN.Search for the whole word under the cursor with
*or#.Control case behavior for one pattern or through options.
Clear highlights without losing the current search pattern.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account