Lesson 19 of 26
Find text in multiple files with AG
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
[00:09] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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] (external link, opens in new tab) 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.