Skip to main content

Fixed Strings (-F)

When you know exactly what text you are looking for — with no wildcards, no pattern matching — use --fixed-strings (-F).

Why It Matters

The default regex engine interprets ., [, *, +, ?, (, |, and \ as special operators. This causes three problems:

  1. Wrong results: grep "192.168.1.1" also matches 192x168y1z1
  2. Escaping complexity: You must remember to write 192\.168\.1\.1
  3. Slower: Regex evaluation costs more than raw byte comparison

-F eliminates all three problems by treating every character as a literal byte.

Examples

# Search for a literal IP address (no escaping needed)
rg -F "192.168.1.100" /var/log/auth.log

# Search for a function call with parentheses (no escaping)
rg -F "process.exit(1)" src/

# Search for a regex-like string in a file
rg -F "(\d+)" template.txt

# Search for a URL with query params
rg -F "api.example.com/v2/users?page=1" access.log

Combining -F With Other Flags

# Case-insensitive literal search
rg -F -i "connection refused" syslog

# Only in Python files
rg -F -t py "import os" src/

# Print only filenames containing the literal
rg -F -l "TODO: REMOVE" .

Performance Comparison

-F is measurably faster on large files because:

  • No regex compilation overhead
  • Uses memmem (vectorized two-way string search) instead of the regex automaton

For a 10 GB log file, searching for a 36-character UUID with -F is typically 2–5x faster than the regex equivalent.