---
title: "Basic Search and Replace in a file"
url: "https://courses.osherove.com/courses/vim-hates-you/basic-search-and-replace-in-a-file.md"
canonical: "https://courses.osherove.com/courses/vim-hates-you/basic-search-and-replace-in-a-file/"
tags: []
author: "Roy Osherove"
description: "Lesson 9 of 26 in Vim Hates You (Get Somewhat productive in an hour or so), 4:41. 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/"
---

# Basic Search and Replace in a file

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 9 of 26

Duration: 4:41 [Watch on YouTube](https://www.youtube.com/watch?v=ZmN-0NtXb4s)

## In this lesson

Roy uses the substitute command, typed as a colon, s, the search pattern and the replacement between forward slashes, to replace text on the current line, and applies it to several lines by selecting them with Shift-V first. Only the first match on each line is replaced unless g for global is added at the end. He also shows a range that covers the whole file and the c flag, which asks for confirmation (yes, no, all or quit) at each match.

## Transcript

617 words

[\[00:07\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=7s) Let's talk about search and replace inside a file. First the basic commands for searching are acting on the current line. So if I want to replace the letter a in the current line which is the first line. What I want to do is in the command line type colon and then of course I want to substitute. So I want to well I just in my head I say s for search and then forward slash I want you to search for the letter a.

[\[00:39\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=39s) So n forward slash that's the search regular expression basically and I want you to replace it with ttt for example and if I just do that what happens is that the letter a changes. However this is only for the current line. So how do I make it so that it applies to two lines? Well there are two ways. Technically before the s you can type in the starting and ending line or the easier way is to move to [visual mode](https://www.youtube.com/watch?v=aaCGJCJPSjA "Vimming - Natural Selection (YouTube)") shift v and let me undo that first shift v

[\[01:16\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=76s) And select let's say these three lines and then type colon. And now you'll see at the bottom here is that this is actually indicating the current highlighted search current highlight highlighted text. So I'm going to type again s. So search for a replace it with ttt. Now what happens now is something a bit weird is that it search for all the a's but only the first ones.

[\[01:50\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=110s) Let's try that again. This time I'm going to change the letter e. Okay. Just select this and then type search for E and change it to Y Y. And what we we're supposed to see here is that only the first E have been changed. For example, in the first line here. Let's try that again just to make sure.

[\[02:21\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=141s) I'm going to press undo. And let's try the same thing with the letter M. Shift V. Search for M. Replace it with UU. And what you'll see is that only the first search in each line is actually being replaced. So if I want more than the first or basically all of the found characters that match the regular expression to actually be replaced, I have to append g for global at the end.

[\[02:55\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=175s) So if I select this I type search for in this case m replace it with uu and I'm going to add just this extra slash to say do that globally and that will replace all of the found matches. Now I can also say if I wanted to do this throughout the file, I could technically go GG up, shift V, and then shift G to select all the file and do my search and replace.

[\[03:33\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=213s) But there's also a shortcut which is the file name. So I can say the file name that's the range and again substitute all of the E characters with I I I and do that globally. And now this is going to work throughout the file. One last thing is that sometimes you want to get a confirmation

[\[04:04\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=244s) For each change. So then you can technically I'm just going to say colon and then go up to repeat and you just add the letter C for confirmation. And now for each match you see at the bottom here you have the ability to say yes or no or all or quit to stop etc or just for this line or until the end of the line. So for us, I can just say yes, yes, no, yes, yes, and all from now on.

[\[04:36\]](https://www.youtube.com/watch?v=ZmN-0NtXb4s&t=276s) So that's the simplest way to get that stuff working.

## Related

### Also linked in this lesson

#### YouTube

- [Vimming - Natural Selection](https://www.youtube.com/watch?v=aaCGJCJPSjA)

[Previous lesson Color Schemes](https://courses.osherove.com/courses/vim-hates-you/color-schemes/) [Next lesson Search and Replace with Regex](https://courses.osherove.com/courses/vim-hates-you/search-and-replace-with-regex/)


---

## 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/
