One Tip a Week: ripgrep

This week’s tip of the week is ripgrep. Think of it as super grep.

From their docs:

ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern.

In this screen shote, I’m in my copilot-extension-template project and I want to see where the word error is.

copilot-extension-template on & main is V1.0.0 via 0 v22.13.1 •› rg -i error package-lock. json 30: "@octokit/request-error": "^6.1.4" 63: "@octokit/request-error": "^6.1.7" 112: "Qoctokit/request-error": "^6.1.7" 121: "node_modules/@octokit/request-error": { 123: "resolved": "https://registry.npmjs.org/@octokit/request-error/ -/request-error-6.1.7.tgz" sic/index.ts 8: createErrorsEvent, 39: console.error("Request verification failed"); 47: createErrorsEvent([ 78: } catch (error) { 80: createErrorsEvent(I 83: message: error instanceof Error ? error. message : "Unknown error" 84: 85: code: "PROCESSING_ERROR" identifier: "processing_error"

Ripgrep respects your .gitignore out of the box, meaning less noise from things like node_modules, .git, etc. Grep needs extra flags and manual exclusions, e.g.

grep -iR error . --exclude-dir={.git,node_modules}

Another example is searching for a function call in code where you want to show line numbers and file names.

copilot-extension-template on › rg -nH "createErrorsEvent\("' sic/index.ts 47: createErrorsEvent ([ 80: createErrorsEvent([ main is v1.0.0 via O v22.13.1
rg -nH "createErrorsEvent\("

Breakdown:

  • -n: show line numbers

  • -H: always show filenames (even with single file results)

Ripgrep is optimized for code. It's fast, has smart defaults, and easily shows relevant context. The above just scratches the surface. Check out the ripgrep user guide for even more ripgrep goodness. Even if you’re a die hard grepper, I encourage you to give this a go.

That’s it! Short and sweet. Until the next one!