Personal Blog

Neovim: Using Telescope to find text inside specifics paths

10/3/2022, 6:00 AM

Introduction

In this post, we will learn how to replicate the search functionality that some GUI editors provide to filter text only inside certain folders.

VS Code example

VSCode searching inside path

What we will need

Telescope already provides us with a built-in function call live_grep this function does its job fine until you need special filtering like:

  • Only search on files on specific directories

  • Only search on files with certain extensions like .js

In order to elevate the searching experience we will need a Telescope extension named Telescope live grep args, this extension let us use pass flags to the ripgrep dependency that telescope use under the hood.

ripgrep is already a telescope dependency so you probably already have it installed, otherwise if you're on Mac you can just run install it using brew:

brew install ripgrep

And then follow the extension installation steps

Using the extension

You can create a new mapping for the new extension, in my case I replaced the normal mapping of the live_grep function for this one

vim.keymap.set('n', '<leader>fs', require("telescope").extensions.live_grep_args.live_grep_args, { noremap = true })

Now you can just invoke the function

Only search on files on specific directories

  • invoke the live_grep_args function

  • Type the code you want to find

  • Press <C-k> or surround the query with parenthesis yourself

  • Add the flag -g "folder/*"

Only search on files on specific directories

Only search on files with certain extensions like .js

  • Add the flag -t js

Only search on files with certain extensions like .js

ripgrep documentation with more flags and examples

Profile Picture

Miguel

I'm a Senior Software Engineer working in Berlin, primary focused on Frontend Technologies but also interested in Backend with Go, Terminal Apps and Neovim.

More posts

How to automate software installation and configuration on macOS using Homebrew and Stow

Debugging Javascript applications with Neovim

My notes of how to compile a typescript library

How to create a basic implementation of React Hook Form

How to debug like a PRO using Neovim 🔥

© 2023 • Built with Gatsby