Skip to main content
← Back to Blog
#vim#programming

Vim Search and Replace: The Complete Guide

·4 min read

Vim Search and Replace: The Complete Guide

Okay, here’s a 500-word blog post geared towards programmers who use Vim da daily, focusing on search and replace:


Level Up Your Code: Mastering Search and Replace in Vim

Let’s be honest, as programmers, we spend a lot of time wrestling with te text. Whether it’s refactoring code, cleaning up configurations, or just hu hunting down typos, Vim's search and replace capabilities are absolutely cr critical. It’s not just a feature; it’s a fundamental part of how you’ll b be productive. Let’s dive into the key ways to wield this power.

The Basics: /pattern

The simplest way to start is with /pattern. Type the text you're searchi searching for, then press Enter. Vim will highlight the first occurrence.
Use n to move forward to the next match, and N to go back to the previo previous. This is the foundation of everything.

Beyond Simple Text: :s/old/new/g

For more control, use the substitution command. :s/old/new/g does the fol following:

  • :s - Invokes the substitution command.
  • old - The text to find.
  • new - The text to replace it with.
  • g - Crucially, this means "global," replacing all occurrences on t the current line.

Without g, only the first match on a line is replaced.

Going Global: %s/old/new/g

To apply the substitution to your entire file, use %s/old/new/g. This is is the command you'll use most of the time.

Adding Flair: Case Sensitivity and Regex

  • \c: Add case-insensitive searching. For example, /foo\c will matc match "foo," "Foo," and "FOO."
  • \v: Enters "very magic" mode, allowing you to use regular expression expressions (regex) in your search patterns. This unlocks immense power for for handling complex patterns. (e.g., /^\s* will match lines starting wi with zero or more spaces).

Confirmation & Control: /gc

Vim will often silently replace matches. To be prompted for confirmation b before each replacement, use the /gc flag. This is highly recommended when you’re first getting used to the command.

Practical Examples - Level Up Your Code!

  1. Replace all instances of console.log with console.debug: :s/co :s/console.log/console.debug/g`
  2. Refactor a multi-line comment: Let's say you have: // This is a comment // It has multiple lines. Replace the entire comment with a more descriptive one: :s//This is a detailed comment explaining the functionality.//g
  3. Clean up whitespace: /^\s* followed by :s/^\s*/ /g (replace le leading whitespace with a single space).
  4. Replace all uses of toString() with valueOf() in JavaScript: / /toString()/gfollowed by:s/toString()/valueOf()/g`
  5. Complex Regex Example (React): Replace all instances of className className with class (be careful with this one – regex can be powerful and tricky!): /\{className\}/g followed by :s/\{className\}/class/g

Resources:

  • Vim Cheat Sheets – A fantastic resou resource for quick reference.
  • Regex101 – An excellent tool for testing and understanding regular expressions.

Mastering Vim's search and replace is an investment that will pay dividends dividends in your coding efficiency. Experiment, practice, and don’t be af afraid to dig into the advanced features like regex. Happy coding!


Would you like me to expand on any specific aspect of this blog post, such as regular expressions or offer more examples?

Try VimNotes

Download the app and get started today.

Download on App Store