---
title: "Find text in multiple files with AG"
url: "https://courses.osherove.com/courses/vim-hates-you/find-text-in-multiple-files-with-ag.md"
canonical: "https://courses.osherove.com/courses/vim-hates-you/find-text-in-multiple-files-with-ag/"
tags: []
author: "Roy Osherove"
description: "Lesson 19 of 26 in Vim Hates You (Bring in reinforcements - Plugins), 6:05. 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/"
---

# Find text in multiple files with AG

1. [Courses](https://courses.osherove.com/#courses)
2. [Vim Hates You](https://courses.osherove.com/courses/vim-hates-you/)
3. Bring in reinforcements - Plugins

Lesson 19 of 26

Duration: 6:05 [Watch on YouTube](https://www.youtube.com/watch?v=fn0Et0b-rjY)

## In this lesson

Roy compares tools for finding text in many files, grep, vimgrep and Ack, and picks AG (the Silver Searcher), which is designed for programmers who search recursively. After installing AG with brew on a Mac, he adds the ag.vim plugin to his vimrc using his leader mappings and installs it. Searching with ag for the word class opens the results in a quickfix window, from which he opens matches in vertical or horizontal splits, moves between windows with Ctrl-W and closes the quickfix window with ccl.

## Transcript

913 words

[\[00:09\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=9s) One of the things that always for frustrated me the most about working inside Vim is trying to find stuff inside multiple multiple files. So if we have this following directory structure and I want to find all the instances of class then I don't have a lot of good choices that are easy really easy to remember. One one tool that everyone uses is grep and there's a thing called vimgrep inside vim but the syntax for it is really hard to remember and it's not very comfortable.

[\[00:40\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=40s) Then there's AC Ack, which is another tool that's also very common and we could technically use that. But then there's this third tool that I learned about called AG or the silver searcher. And this is the website for this tool. The silver searcher is basically faster than Ack and it also has a lot of defaults. It's basically designed for programmers that want to search stuff recursively. So by default, there's already a lot of functionality. Now, what I'm going to show you is that there is a plugin that connects AG to Vim.

[\[01:16\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=76s) But first, you actually have to have AG installed on your system. So, you actually have to write you have to, for example, on a Mac, you'd have to brew install AG. And in this case, I I should already have it installed. So, that's not a problem. And by default, if you call ag inside a folder, you just say AG and then the word you're looking for. and it recursively searches and gives you nicely colored output to show exactly where things are.

[\[01:47\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=107s) And then there's this ag.vim plugin for Vim. And it's very very easy to install. I'll just literally just copy this. And at this point, I'm assuming that you've seen my video on how to install and use plugins. And I'm also going to very quickly open Vim here and use a shortcut for editing my vimrc file.

[\[02:19\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=139s) I I've mapped it to leader which in my case is comma leader EV. So I'm editing my vimrc file here. And if you're not sure what I just did, don't worry about it. There's a special video under the going fast section that shows exactly what I just did and how I map this stuff. It's this set of commands right here. But what I'm going to do is I'm going to install a new plugin here. I'm going to call it plug. And here I'm going to just command V and paste

[\[02:53\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=173s) The location for for this plugin. And now I'm going to write quit write and then quit and then source the file. And I have a command for that doing this whole thing right here. So I'm just going to type leader WQ and that's going to source my vimrc file and allow me to use the plugin. So, comma wq

[\[03:26\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=206s) And I'll just move this here so you can see my shortcuts from now on. And now I have actually have vim open. I'll have to do a command plugin install which will actually install the ag plugin. Now I already have it installed from for because I prepared a demo but that's what you would have gone through and from this moment on I can quit this file and if I wanted to just so you can see if I open NERDTree

[\[03:56\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=236s) This is where I am in my file system and if I just type command ag and then the word class what you'll see is that I now have two windows open one is the first result found is actually open in a window on top and then the bottom window is the window that's currently active. This is called a quick fix window and there's a bunch of shortcuts that I can use and it shows all the all the locations of where that word actually exists in files here and you can see here at the bottom I can press E to enter E T to tab a horizontal or vertical split.

[\[04:38\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=278s) Basically, if I wanted to, for example, open index as a split here, all I have to do is to say V or H. So, I'll just do V and it opened this file right there. And if I wanted now to move back into the bottom window, just like I'm using multiple windows, Ctrl W and and J will go down. Ctrl W and K goes up. Ctrl W and L goes right. So let me go down for a second here.

[\[05:10\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=310s) And so I can open this. I can open other.rb in another split window just like another V. And now I have these three windows open. So let me close the quick list window. I can literally just move to it and then type Q or there is a command called colon. This is the quickfix window starts with the C and I'll just say CL for close. CCL will close the quick fix window.

[\[05:40\]](https://www.youtube.com/watch?v=fn0Et0b-rjY&t=340s) Now I have my three files and it's easy to move around. And if you want to understand all the options that AG has, just look up for the silver searcher command line reference. You should be good to go. Very simple, very nice, very easy. This totally takes away all the horrible stuff that is the grep syntax that I really really do not like and makes things much much easier to live with.

[Previous lesson Ctrl-P](https://courses.osherove.com/courses/vim-hates-you/ctrl-p/) [Next lesson Mapping keys to actions](https://courses.osherove.com/courses/vim-hates-you/mapping-keys-to-actions/)


---

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