• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Vim find and replace

Posted Dec 16, 2023 — Updated Jan 10, 2024

Easy find and replace is one of those quality of life improvements that make Vim great. With one quick command, and a little regex, you can replace a pattern across an entire file. Here’s how:

Find and replace in the entire file with Vim:%s

This is my most-used version of Vim find and replace. :%s will find and replace a pattern across the entire file being edited. The command is formatted like this:

:%s/match/replacement/.

It’ll take anything in your file which matches the match pattern, and replace it with replacement.

For example, here’s a screenshot of some identifying strings I need to reformat:

a list of identifier strings
I need to remove these “AA.csv_” prefixes

With %s, we can remove all of those prefixes with one quick command:

a list of identifier strings with some highlighted
These have to go
final list of strings after replacement
The final result

This is the exact command I used: :%s/\w.+_//g. Let’s break it down:

  • :%s – this is the Vim command for find and replace on all lines.
  • /\w.+_/ – this is the matcher expression. It says “find a word character, followed by any number of anything else followed by an underscore “_”. Not very specific, but good enough for this job.
  • // – between these two slashes is what to replace with. In this case it’s empty because I want to delete.
  • g – Global flag. This makes sure the replacement affects multiple occurrences on the same line. By default, this command only changes the first instance on each line.

Important note: Pure Vim tends to interpret regex characters literally, so you may have to escape regex quantifiers. Meaning: instead of using + in your pattern you might need \+. So the full pattern above would become /\w.\+_/.

I ran this pattern (/\w.+_/) in pure Vim and again in VS Code with Vim emulation—pure Vim did require additional escaping, VS Code did not.

Find and replace with Vim :s

This works just like the :%s command, but :s will only replace on your current line.

regex highlighting one line

That’s about it for the basics. For a lot more detail, like how to search specific line ranges, check out this detailed post.


Helpful Links

  • This Stackoverflow answer which taught me about Vim’s magic setting, responsible for how it interprets Regex patterns

Filed Under: Productivity, Tips, Vim

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy