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
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 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
-
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 with certain extensions like .js
- Add the flag
-t js