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

Top comments (0)