git/Q/Why does git grep -E report no matches for a word-boundary pattern?
Properties
- logseq-entity
- Logseq/Entity/Question
Why does
git grep -Ereport no matches for a word-boundary pattern?AI Answer
- Short answer:
\bcarries no meaning in the pattern languagegit grep -Euses, so the whole pattern stops matching anything. The search prints nothing and exits1, which is the same result it gives for a word that is genuinely absent. Usegit grep -Pwith the pattern in single quotes. \bis the regular-expression escape for a word boundary, meaning the edge between a letter and a space or a punctuation mark. It is how you ask forcatand skipconcatenate.Measured here
- Run on 2026-08-25 Tue against
pages/LazyVim___Tutorial___Work with the Terminal.mdin this graph, under git 2.55.0 with nogrep.patternTypeset: Command Output Exit git grep -c -F 'LazyVim'50git grep -c -E 'LazyVim'50git grep -c -E '\bLazyVim\b'none 1grep -c -E '\bLazyVim\b'50git grep -c -P '\bLazyVim\b'50- The
git grep -c -E 'LazyVim'row shows that-Ereads this file correctly. The answer changes only on the rows whose pattern carries\b.
- Run on 2026-08-25 Tue against
The pattern matches nothing at all
- One explanation for this behavior is that git reads
\bas the backspace character, which would make the pattern ask for a literal backspace in front of the word. The evidence goes against that. - Two files were searched with
git grep --no-index. One held the textplain LazyVim here. The other heldlit, then the single byte0x08, thenLazyVim here. The pattern\bLazyVimmatched neither file, and the pattern\bon its own also matched neither. - So a pattern holding
\bmatches no line of any file, whatever that line contains.
- One explanation for this behavior is that git reads
What to use instead
git grep -P '\bword\b'keeps the whole-word search and keeps git's own choice of which files to read. It needs a git built with PCRE2, which this machine has.git grep -F 'word'searches for a fixed string. It also matches inside longer words, socatfindsconcatenatetoo.grep -rE '\bword\b'handles\bthe usual way. It reads files from the working directory, so it also opens untracked and ignored files, and it ignores git's index.
A shell that rewrites the pattern defeats
-Pas well-Pneeds\bto reach git as two characters, a backslash and ab. Several shell constructs replace it with the single byte0x08first, and then-Pfinds nothing either. The symptom matches the-Efailure while the cause differs, so ruling out one leaves the other.- Measured on 2026-08-25 Tue under zsh on this machine:
Construct What git receives '\bword\b'in single quotesa backslash and ba heredoc, delimiter quoted or not a backslash and bprintf '\bword\b'the byte 0x08echo -e '\bword\b'the byte 0x08$'\bword\b'the byte 0x08- On
pages/LazyVim___Tutorial___Work with the Terminal.md,git grep -c -Preturns5for the single-quoted pattern and nothing for the0x08form. Single-quote the pattern and it arrives whole.
Run a positive control first
A false answer costs more when several agents share a graph
- Each seat in My/AI/Agent/Fleet searches before it writes, so a search tool that answers false shapes what gets written. On 2026-08-25 Tue a whole-word search reported that no tracked file in My/Dotfiles held a term, and a fixed-string search for the same term then found 27 occurrences of it in one file.
- Short answer: