Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I search in all files of my project using VIM?

Tags:

vim

search

neovim

There are a couple of things I do not yet understand the VIM way.

One of these is searching in a project like so (using VIM in Atom):

enter image description here

I use CtrlP currently for file names, but what about the contents?

How can I search with a string, and then look through a list of all occurrences using VIM and/or VIM plugins?

like image 827
Sem Avatar asked Jul 15 '16 14:07

Sem


People also ask

How do I navigate multiple files in Vim?

Using windows. Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l ) to navigate through open windows. Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one. Starting vim with a -o or -O flag opens each file in its own split.

How do we use grep to search for a pattern in multiple files in Vim?

From the root of your project, you can search through all your files recursively from the current directory like so: grep -R '. ad' . The -R flag is telling grep to search recursively.

How do I navigate folders in Vim?

Select a file or directory name and press Enter to open that file or directory. (For example :e /home/user displays the contents of that directory.) To return to the explorer window, press Ctrl-^ (usually Ctrl-6). You can also "edit" a directory to explore that directory.


Video Answer


2 Answers

I've found an even better solution for this: FZF

It simply searches through everything in your project asynchronously using the :Ag command.

enter image description here

like image 122
Sem Avatar answered Oct 19 '22 19:10

Sem


Use :grep or :vimgrep to search file contents. The results are put onto the "location list" which you can open by typing :cw Enter.

Syntax for :grep is, by default, the same as the grep(1) command:

:grep 'my pattern.*' /path/to/dir

By default it will search the current directory (:pwd). I added set autochdir to my .vimrc so my PWD always follows the file I'm editing.

The major difference between :grep and :vimgrep is that :vimgrep (:vim for short) uses Vim-compatible regular expressions, whereas :grep uses whatever regular expressions your &grepprg uses.

You can use a custom program by setting &grepprg to something different. I personally like ack which uses Perl-compatible regex.

like image 36
amphetamachine Avatar answered Oct 19 '22 18:10

amphetamachine