Skip to content

Neovim: Using Telescope to find text inside specifics paths

Posted on:October 3, 2022 at 06: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:

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 theextension 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

Only search on files on specific directories

Only search on files with certain extensions like .js

Only search on files with certain extensions like .js

ripgrep documentation with more flags and examples