Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search across a directory of files in vim?

Tags:

vim

editing

A common programming task for me in vim is:

:s/some pattern/ 

do some work

n # finds the next entry

do some work

n # finds the next entry ...

Now, s/.... only searches in the current file.

Is there a way I can do this, but search across a directory of files? Say do "s/..../" over all files in subdirectoires of pwd that ends in .hpp of .cpp ?

Thanks!

like image 517
anon Avatar asked Feb 03 '10 09:02

anon


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 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.

How do we use grep to search for a pattern in multiple files 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.


1 Answers

You can simply use the :grep command: or for a more complete integration of search tools, use the grep.vim extension.

Simply type :help grep to get a nice documentation of what is available out of the box in Vim. Using :grep foo *.?pp should do what you want. This will open the QuickFix list, just like the one you get using :make, enabling to jump to the found occurrences.

like image 100
tonio Avatar answered Sep 20 '22 16:09

tonio