0
0
Linux-cliHow-ToBeginner · 3 min read

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:

  1. Open a file: vim filename.txt
  2. Type /linux and press Enter to search forward.
  3. Press n to find the next occurrence.
  4. Press N to 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 Enter after typing the search pattern.
  • Confusing n and N directions.
  • 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

CommandDescription
/patternSearch forward for 'pattern'
?patternSearch backward for 'pattern'
nRepeat search forward
NRepeat search backward
:set ignorecaseMake search case-insensitive
:set smartcaseCase-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.