Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ripgrep no way to exclude directory e.g. Tests and search for all *.c files #839

Closed
sumonto opened this issue Feb 28, 2018 · 12 comments
Closed

Comments

@sumonto
Copy link

sumonto commented Feb 28, 2018

What version of ripgrep are you using?

ripgrep 0.7.1
-AVX -SIMD

What operating system are you using ripgrep on?

Mac High Sierra

Describe your question, feature request, or bug.

Imagine a folder structure like:
Feature
codefiles1 (contains *.c, *.h)
codefiles2 (contains *.c, *.h)
tests (contains *.c, *.h)

I would like to search a string str1 in *.c, *.h files however exclude/ignore all occurrences in "tests" folder

I would assume the below syntax would do it?
rg -w str1 -tc -g '!tests/*'

If a feature request, please describe the behavior you want and the motivation.
Please also provide an example of how ripgrep would be used if your feature
request were added.

If a bug, please see below.

If this is a bug, what are the steps to reproduce the behavior?

NA

If this is a bug, what is the actual behavior?

NA

If this is a bug, what is the expected behavior?

ripgrep should have excluded files from "tests" folder

@BurntSushi
Copy link
Owner

Please fill out the entire issue template. That's why it is there to guide you, because the request you've made is insufficient to act on. In particular, I have no idea what you are asking for.

Please also make sure you've read the documentation. ripgrep can of course exclude directories.

@kbknapp
Copy link
Contributor

kbknapp commented Mar 1, 2018

@sumonto you're correct $ rg -w str1 -tc -g '!tests/*' would do what you want. So is that not working for you? It works on my machine with v0.8.1

kevin@beefcake:/tmp/rg$ rg --version
ripgrep 0.8.1
-SIMD -AVX
kevin@beefcake:/tmp/rg$ tree
.
├── code
│   ├── other.h
│   └── some.c
└── test
    └── bar.c

2 directories, 3 files
kevin@beefcake:/tmp/rg$ cat code/other.h 
foo
kevin@beefcake:/tmp/rg$ cat code/some.c 
foo
kevin@beefcake:/tmp/rg$ cat test/bar.c 
foo
kevin@beefcake:/tmp/rg$ rg -w foo -tc -g '!test/*'
code/some.c
1:foo

code/other.h
1:foo

@BurntSushi
Copy link
Owner

It's not possible to act on this issue. Closing.

@mmahmoudian
Copy link

I might know what the issue might be: ZSH

I was trying to make this work in ZSH:

rg --stats --ignore-case --type r --max-filesize 200K -g "!packrat" --regexp "stringi"

The command was suppose to only search in specific file types (in this case r type) and search for the text stringi but I want it to exclude everything in "packrat" folders in the following scenario:

.
├── Project1/
│   ├── packrat/
│   │   ├── lib1/R/
│   │   │   └── some_file.R
│   │   └── lib2/R/
│   │       └── some_file.R
│   ├── other_folder/
│   │   └── some_file.Rmd
│   └── some_file.R
│
└── Project2/
     ├── packrat/
     │   └── lib1/R/
     │       └── some_file.R
     └── some_file.R

the ! has a meaning in ZSH, so you should escape the character (except if you use it as an alias). The correct command in my case is:

rg --stats --ignore-case --type r --max-filesize 200K -g "\!packrat" --regexp "stringi"

@okdana
Copy link
Contributor

okdana commented Oct 9, 2019

You can also just use single-quotes instead of double- (which is what the OP was doing, so i don't think it was the issue there). Or you can turn off history expansion, but you lose some functionality obv

@BobrImperator
Copy link

BobrImperator commented Mar 6, 2021

I found out that -g didn't work for me, but --glob did as per man rg, and final command looks like this:
.zshrc
export FZF_DEFAULT_COMMAND="rg --files --follow --hidden --glob=\!.git"

which previously looked like this
`"rg --files --follow --hidden -g !.git"'

@okybr
Copy link

okybr commented Apr 29, 2021

@BobrImperator, use -g '!.git', i.e. single-quotes ;)

@joeky888
Copy link

Hello everyone. How do you exclude multiple directories? Currently, I am using --glob multiple times like this:

rg --hidden -g '!.git' -g '!.svn' -g '!.hg' -g '!CVS' -g '!.bzr' -g '!vendor' -g '!node_modules' -g '!dist' -g '!bin'

@BurntSushi
Copy link
Owner

@joeky888 Why are you bumping old issues? Use Discussions to ask questions next time please.

In any case, you can use -g '!{.git,.svn,.hg}' (and so on) to exclude multiple directories. But your approach works fine too.

@nyngwang
Copy link

nyngwang commented Nov 8, 2021

@BurntSushi Thanks for the solution, it works!

@dharmaturtle
Copy link

Apologies for necrobumping, but to emphasize the directories part of joeky888's question, this works for me for (sub)directories: -g '!{**/node_modules/*,**/.git/*}'

@Riveascore
Copy link

@dharmaturtle Thank you!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests