How to Search in Vim on Linux: Simple Guide
To search in
vim on Linux, press / followed by the text you want to find and hit Enter. Use n to jump to the next match and N to go to the previous match.Syntax
In vim, searching uses the following syntax:
/pattern: Search forward for pattern.?pattern: Search backward for pattern.n: Repeat the last search in the same direction.N: Repeat the last search in the opposite direction.
vim
/pattern ?pattern n N
Example
This example shows how to search for the word linux in a file opened with vim:
- Open a file:
vim filename.txt - Type
/linuxand pressEnterto search forward. - Press
nto find the next occurrence. - Press
Nto find the previous occurrence.
vim
/linux n N
Output
The cursor moves to each occurrence of 'linux' in the file as you press n or N.
Common Pitfalls
Common mistakes when searching in vim include:
- Not pressing
Enterafter typing the search pattern. - Confusing
nandNdirections. - Case sensitivity causing missed matches.
To avoid case sensitivity issues, use :set ignorecase or :set smartcase.
vim
:set ignorecase :set smartcase
Output
Enables case-insensitive search or smart case search in vim.
Quick Reference
| Command | Description |
|---|---|
| /pattern | Search forward for 'pattern' |
| ?pattern | Search backward for 'pattern' |
| n | Repeat search forward |
| N | Repeat search backward |
| :set ignorecase | Make search case-insensitive |
| :set smartcase | Case-insensitive unless uppercase used |
Key Takeaways
Press / followed by text and Enter to search forward in vim.
Use n to go to the next match and N to go to the previous match.
Use :set ignorecase or :set smartcase to control case sensitivity.
Remember to press Enter after typing your search pattern.
Use ? to search backward through the file.