Skip to content

how do I search for a pattern starting with "-"? #1560

Answered by BurntSushi
tgray asked this question in General
Discussion options

You must be logged in to vote

This is correct and expected behavior. The -F flag is a red herring here. If your pattern starts with a -, then you must either precede it with the -e flag or use -- to indicate that all subsequent arguments are positional. This is documented in the man page:

A regular expression used for searching. To match a pattern beginning with a dash, use the -e/--regexp option.

So you'll want rg -F -e '- [ ]' or rg -F -- '- [ ]'.

Running the search by escaping the leading dash works fine

This is a different search. There is no escaping when using -F, so if you use \- [ ], then you'll actually be searching for a literal \\ followed by a -:

$ echo '- [ ]' | rg -F '\- [ ]'
$ echo '\- [ ]' | rg -F …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1560 on April 23, 2020 14:05.