- Read Tutorial
- Watch Guide Video
A common behavior that you need to have as a developer, is the ability to search through your full set of files. And my favorite way it's doing that, and kind of the standard way that you will from the command line, is by using the grep command. And let's first talk about how you can do this in another text editor.
So I'm gonna open up Sublime here, and actually let me open up Sublime with our current project. And I have created a little nav bar, it doesn't actually do anything, but it's in the application layout file. And, as you can see, right here we have a div of class nav.
Now, let's pretend that I was new on the project, I had no idea that this was there, how would I find it? In Sublime, I'd run Command + Shift + F and simply type in nav and it would show me that hey, this is in the views layouts application file and then I can double-click on it and come to it right here. Now, when we're using this in the terminal, we don't quite have the same tools available to us as we do in Sublime.
Sublime's an actual application; here we have to use some more low level kinds of constructs and so what you can do is type in grep. So grep is a very common thing in UNIX; it's also even a method in Ruby, and it's a way that you can pass in a expression and then find it. So I'm gonna say grep, and then in quotation marks, I'm gonna say nav, and then we have to tell it where to look.
If we just said grep and nav, it would look everywhere and about 20 minutes later, it would finally find all the spots that said nav. So that would not be a good way of looking.
So the way that you want to do this is pass in a double asterisk, which means that we want you to look in any file that obviously if you already know the file name, where it's at, then you probably should just open up vim and then go into Control + P in order to go find it via fuzzy search, but what I use grep for is when I don't actually know the file it's in.
So I'm gonna say, double asterisk slash single asterisk dot erb. So what this is saying, is look inside of all of the files that end with the file extension .erb. Now if I hit return, you can see that it brings it right up.
It brings up app, views, layouts, application. And then I can go vim, views, layouts, application, and I can find it right here, and it's right here on line 12. So, that is the standard way of being able to use grep from the terminal in order to find anything you're looking for in your application.