How to combine rg with less in terminal
Pavol Z. Kutaj

Pavol Z. Kutaj @pkutaj

About: A public interface of my current learnings. The focus is on everything from vim, python to cloud. Imperfect. Impersonal. Never too far from my terminal.

Location:
Brno, Czech Republic
Joined:
Jan 26, 2021

How to combine rg with less in terminal

Publish Date: Jan 30
0 0

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

Comments 0 total

    Add comment