0

According to this documentation

http://vim.wikia.com/wiki/Find_in_files_within_Vim

if grep is used like

:vim[grep][!] /{pattern}/[g][j] {file} ...

it should be able to find files by a matching pattern.

I have a text file named text.txt in the directory where I am starting vim from.

Content of text.txt is

look for me

In vim I enter the command

:grep /look/gj *.txt

but I don't get any results. Although the file text.txt contains the string "look".

file content current directory

pattern result

LongHike
  • 4,016
  • 4
  • 37
  • 76

3 Answers3

3

You are simply confusing two commands: :vimgrep and :grep.

:help :vimgrep uses internal methods for searching files and has its own syntax and flags.

:help :grep uses an external program for searching so it doesn't have a defined syntax; the syntax you use is determined by what external program is used under the hood.

You can't really expect the :vimgrep syntax to work in :grep.

romainl
  • 186,200
  • 21
  • 280
  • 313
2

You're using vim's syntax for grep instead of using grep(1) syntax.

As written here: How do I search in all files of my project using VIM?

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

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.

Community
  • 1
  • 1
Flo Sufmoe
  • 29
  • 4
0

The searches generally work in the current directory as well so I would check the output of :pwd and :ls to see if you are seeing what you expect to be seeing.

If you :cd ~ however, you can search subdirectories with vimgrep by using ** as the path:

:vim /look for me/ **/*.txt

I would also suggest that unless you have specific requirements, you do not use the g or j flags after the search pattern.

dash-tom-bang
  • 17,383
  • 5
  • 46
  • 62