DEV Community

Pavol Z. Kutaj
Pavol Z. Kutaj

Posted on

How to combine rg with less in terminal

USECASE

Uploading image

The aim of this page 📝 is to explain how to paginate the results of ripgrep (rg) using less. This is especially useful for querying my knowledge base, which is a simple file tree full of markdown files, edited in Visual Studio Code.

less is a powerful terminal pager program used to view the contents of a file one screen at a time. It allows backward and forward navigation through the file, and it is more feature-rich than other pagers like more. With less, you can search within the file, navigate by lines or pages, and even view files with color formatting.

  • Ripgrep (rg) is a powerful tool for searching within files.
  • To handle large search results, pipe the output to a pager like less.
  • The -R flag is crucial for displaying colors correctly in less.
  • Using ripgrep with the --pretty flag and less provides a great combination.
rg --pretty '<search_term>' | less -R
Enter fullscreen mode Exit fullscreen mode
  • Benefits of this approach:
    • Scroll up/down through results: Navigate large outputs easily.
    • Search within results: Press / in less to search (like vim)
    • Preserved colors and formatting: Keep syntax highlighting.
    • Line numbers and file names remain visible.
    • Easy exit: Press q to quit less.
    • Hyperlinks + preview: with Warp, I'll get the contents of the linked file OOTB

LINKS

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free