-
-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
find_files and grep in selected directory #2201
Comments
You can achieve this with telescope-file-browser and a custom function. This action will launch telescope-file-browser in folder mode, and then launch live_grep within the selected directory: local ts_select_dir_for_grep = function(prompt_bufnr)
local action_state = require("telescope.actions.state")
local fb = require("telescope").extensions.file_browser
local live_grep = require("telescope.builtin").live_grep
local current_line = action_state.get_current_line()
fb.file_browser({
files = false,
depth = false,
attach_mappings = function(prompt_bufnr)
require("telescope.actions").select_default:replace(function()
local entry_path = action_state.get_selected_entry().Path
local dir = entry_path:is_dir() and entry_path or entry_path:parent()
local relative = dir:make_relative(vim.fn.getcwd())
local absolute = dir:absolute()
live_grep({
results_title = relative .. "/",
cwd = absolute,
default_text = current_line,
})
end)
return true
end,
})
end The action also captures the current line from telescope, because it's meant to be launched from within live_grep: telescope.setup({
pickers = {
live_grep = {
mappings = {
i = {
["<C-f>"] = ts_select_dir_for_grep,
},
n = {
["<C-f>"] = ts_select_dir_for_grep,
},
},
},
},
}) So the flow is:
You can repeat 3-6 as many times as needed. (If you use live_grep_args, replace |
The above solution is of course much nicer, but one can simply specify the
|
What would be even nicer than spending time figuring out how to make this work and then writing all the above code that uses an extension would be if this functionality was builtin. |
I find myself constantly running into this, especially in monorepos. With all 3 of my most used pickers: file picker, text search and live grep. I would also much prefer to have this feature natively instead of adding more plugin and script bloat. Telescope already does almost everything I personally need from it and hope it could extend to this as well at some point. It's an extremely useful utility for me. Example scenario:
I would prefer this workflow:
I imagine directory picker would be a normal file browser with traversal capabilities (
One can dream but perhaps someone well versed with the plugin might get a spark from this idea. Thanks for all you've done already, love the plugin! |
For |
The dir-telescope plugin linked in the OP is what I'd recommend. It should really be builtin to telescope. |
If you use telescope-file-browser you can also use an custom action like below from within a directory to local Path = require("plenary.path")
local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local open_using = function(finder)
return function(prompt_bufnr)
local current_finder = action_state.get_current_picker(prompt_bufnr).finder
local entry = action_state.get_selected_entry()
local entry_path
if entry.ordinal == ".." then
entry_path = Path:new(current_finder.path)
else
entry_path = action_state.get_selected_entry().Path
end
local path = entry_path:is_dir() and entry_path:absolute() or entry_path:parent():absolute()
actions.close(prompt_bufnr)
finder({ cwd = path })
end
end
-- map keys to for file_browser to...
open_using(builtin.find_files),
open_using(builtin.live_grep), |
I tried some of the solutions mentioned above today. I like the idea of scoping the search to a directory using
Thanks for the help, much appreciated :) |
dir-telescope plugin linked in the OP allows you to make multiple selections and you can then search through all those directories at the same time, is that what you mean? |
Hi Brian. Actually, both the plugins were working similarly for me. Suppose I take the above folder structure as an example. If my current buffer is I'll try to add a reproducible demo link here and probably I should also ask this query in the |
Are you using LazyVim? It sounds like you are running search in |
I am using Neovim without LazyVim. And yes. I open Neovim at the root of the monorepo. From the root level I can grep/find_file everything but when I open a file inside any package, grep/find_file only works in the scope of that package. |
I think something similar is happening to you, the |
Finally found the issue in my config. I was using |
I have the same issue as @pr7prashant : I would sometimes like to search outside of the current root. One specific use case that I'm running into right now is that as I'm setting up my configuration, I frequently want to search for where certain mappings/plugins are set up by grepping inside the plugin folder, e.g. I tried passing in an option to the @jamestrew how does your solution differ from the one above by schoblaska, which both use |
You could take a look at LazyVim's keybindings and add your own specifying that one directory as the |
I want to be able to live_grep some string and then, while I have results, further filter the results to only those in a specific subdirectory. Is that not possible? |
@thallada not exactly what you're asking for but you can |
@schoblaska I know it's been a while, but I am trying to use your suggested solution. I used it with normal extensions = {
live_grep_args = {
mappings = {
i = {
["<C-p>"] = ts_select_dir_for_grep,
},
},
},
} But when I try to invoke the filebrowser I get this error:
Where line 16 is line 5 in the function: local ts_select_dir_for_grep = function(_)
local action_state = require("telescope.actions.state")
local current_line = action_state.get_current_line()
local fb = require("telescope").extensions.file_browser
local live_grep = require("telescope").extension.live_grep_args.live_grep -- this is line 16
fb.file_browser({
files = false,
depth = false,
attach_mappings = function(_)
require("telescope.actions").select_default:replace(function()
local entry_path = action_state.get_selected_entry().Path
local dir = entry_path:is_dir() and entry_path or entry_path:parent()
local relative = dir:make_relative(vim.fn.getcwd())
live_grep({
cwd = dir:absolute(),
default_text = current_line,
results_title = relative .. "/",
})
end)
return true
end,
})
end For all other usecases the filebrowser works as expected, so any pointer on what I missed? Thanks!! EDIT: Also tried it with |
The error says that I also use select_dir_for_grep = function(prompt_bufnr)
local action_state = require("telescope.actions.state")
local fb = require("telescope").extensions.file_browser
local lga = require("telescope").extensions.live_grep_args
local current_line = action_state.get_current_line()
fb.file_browser({
files = false,
depth = false,
attach_mappings = function(prompt_bufnr)
require("telescope.actions").select_default:replace(function()
local entry_path = action_state.get_selected_entry().Path
local dir = entry_path:is_dir() and entry_path or entry_path:parent()
local relative = dir:make_relative(vim.fn.getcwd())
local absolute = dir:absolute()
lga.live_grep_args({
results_title = relative .. "/",
cwd = absolute,
default_text = current_line,
})
end)
return true
end,
})
end |
Ahh... 🙈 Thanks for your quick response @schoblaska! Works like a charm now 👍🏻 |
@svanharmelen Does folder browser ( |
Thanks again for your quick response @schoblaska! I just reverted the last commit from 3 days ago, and that fixed the problem. So its indeed a bug with the file-browser plugin 😏 Will open an issue there to see if they can get it sorted. |
@jamestrew are you sure this is correct? I don't see this binding in the telescope default bindings. |
@dudicoco It's here:
|
thanks @briandipalma. |
@gitkumi Have you tried the refine search feature triggered with So I'd start searching for Frankly I think this issue can be closed as I think this is what people are looking for it just needed better documentation. |
I wanted to test how this works, so added the Thanks! |
@svanharmelen perhaps Try rebinding it: config = function()
require("telescope").setup({
defaults = {
mappings = {
i = {
["<C-f>"] = require("telescope.actions").to_fuzzy_refine,
},
},
},
})
require("telescope").load_extension("fzf")
end |
Thank you for the detailed explanation, I'll try this and see how it goes. |
I use this in my neovim keybindings: |
What I think would be nice is to have a functionality to toggle between live_grep/file_find and telescope-file-browser. I have exactly the same workflow as what @albertfgu described in #2201 (comment) where I need to work outside both CWD and root directory. You can achieve that by doing If anyone knows that either Telescope or telescope-file-browser already offers such functionality, please do share, I'm sure it's going to save many people in the current issue. Since I don't know such functionality exists, I created an issue in telescope-file-browser: nvim-telescope/telescope-file-browser.nvim#382. |
Hello, thank you for providing this code, i searched a long time ago about this solution. And it worked. But one thing would be great if you could help me. At the time if i search in live_grep for a string and then want filter the result to a folder. I got all the folders. Is it possible only to see the folder where the match was found und then filtering the folders... |
@juridiener Have you taken a look at my comment above (#2201 (comment))? Just use |
Is your feature request related to a problem? Please describe.
the builtin
grep
andfind_files
searches the current directory. in cases when you're working in a monorepo or a project with multiple apps, it may be harder to narrow down the searches with the builtin grep and find_files. are there plans to support these functions natively?Describe the solution you'd like
i have a working library which can do this (dir-telescope.nvim). i just wanted to know if there are future plans to directly implement this in telescope.nvim, thanks
The text was updated successfully, but these errors were encountered: