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)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay