Vim Practical Reference

Searching and Replacing

Searching and replacing text in Vim is a powerful feature, essential for editing large files or making quick modifications.

Basic Search

  • Search for a Word: Type / followed by the word and press Enter.
  • Navigate Search Results: Use n (next occurrence) and N (previous occurrence).

Case Sensitivity

  • Ignore Case: Type :set ignorecase.
  • Respect Case: Type :set noignorecase.

Search in Visual Mode

  • Select Text: Enter Visual Mode and select text.
  • Search Selected Text: Press * (forwards) or # (backwards).

Advanced Search with Regular Expressions

  • Basic Regex: Use /\v followed by the pattern.
  • Example: Search for 'vim' or 'Vim': /\v(vim|Vim).

Search and Replace

  • Basic Replace: :%s/old/new/g - Replace 'old' with 'new' globally in file.
  • With Confirmation: Add c flag - :%s/old/new/gc.

Replace in Range

  • Specific Lines: :10,20s/old/new/g - Replace in lines 10 to 20.
  • Current Line: :s/old/new/g - Replace only in the current line.