---
title: "Search and Replace with Regex"
url: "https://courses.osherove.com/courses/vim-hates-you/search-and-replace-with-regex.md"
canonical: "https://courses.osherove.com/courses/vim-hates-you/search-and-replace-with-regex/"
tags: []
author: "Roy Osherove"
description: "Lesson 10 of 26 in Vim Hates You (Get Somewhat productive in an hour or so), 3:20. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Search and Replace with Regex

1. [Courses](https://courses.osherove.com/#courses)
2. [Vim Hates You](https://courses.osherove.com/courses/vim-hates-you/)
3. Get Somewhat productive in an hour or so

Lesson 10 of 26

Duration: 3:20 [Watch on YouTube](https://www.youtube.com/watch?v=f-oFpi6WpYo)

## In this lesson

Roy explains that Vim's search and replace is based on regular expressions, and replaces every run of word characters in a file with three underscores, using backslash w for any alphanumeric character, a plus for one or more and the global flag. He points to the vimregex.com site as a reference for character classes and quantifiers. He then replaces all uppercase characters, and after that all runs of non-uppercase characters, with underscores.

## Transcript

457 words

[\[00:15\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=15s) So, if you thought, you know, kind of basic search and replace was kind of cool, which if you did, you're very, very geeky and nerdy, which is awesome. I appreciate the fact that you're even watching a Vim class. That should definitely give you some props. So if you like that realize that all of the vim search and replace is based on regular expressions. So if you know regular expressions you can be very very powerful. So for example for a regular expression we don't have to have hard-coded values. We can say

[\[00:45\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=45s) In this file substitute anything in the current reax. So in this case, let's choose everything that is kind of a word character or alpha numeric. So you just put the special back slashw for any alpha numeric character. And of course you can put a slash plus to say it has to be one or more. And now we've finished the search regular expression. And let's replace it with anything that is underscore.

[\[01:21\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=81s) Let's say three underscores and we'll do this globally of course and if I press enter now we've replaced all of the word characters so it's a pretty powerful thing we have a bunch of regular expressions that you can use in in in Vim and there's a site called Vimregex vimregex.com that you can use to look up it's pretty nice and simple so for example all the whites characters all the digits hex digit etc etc so /w W is what I I really like to use. And of course all the quantifiers.

[\[01:52\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=112s) So zero or more, at least one or more, etc., etc. And these are like the most common ones that I'll usually use. Also note that you can also say uppercase or or lowerase or non-upercase by having this uppercase and non- lowerase by having this uppercase. So for example, if I want to replace all the uppercase characters in this file, I'll say in this file substitute everything that is

[\[02:24\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=144s) This would be uppercase but I want to say not uppercase. Well actually let's say uppercase in this case uppercase in this case. Oh I killed me. And replace that with these three underscores and do that globally. If I press enter, replace all of my uppercases. And now I'm going to undo and if I just replace that with an uppercase U, I have done the exact opposite of that. In fact, let me do this.

[\[02:56\]](https://www.youtube.com/watch?v=f-oFpi6WpYo&t=176s) Replace it for non-upercase at least one or more replace that with three. So it's not per one or but basically for any sequence of non-uppercase characters. So that's an example of using regular expressions and counters in your Vim substitutions. It's pretty pretty powerful.

[Previous lesson Basic Search and Replace in a file](https://courses.osherove.com/courses/vim-hates-you/basic-search-and-replace-in-a-file/) [Next lesson Useful Vim Commands Reference](https://courses.osherove.com/courses/vim-hates-you/useful-vim-commands-reference/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
