telescope.nvim
telescope.nvim copied to clipboard
feat(pickers): initial_mode = 'select'
Description
Minimal solution of what's needed to implement what's needed for #1939 (initial_mode = "select") to get a feel. Looks like it works as expected to move in between pickers etc. without much hassle.
Not 100% sure whether default to insert mode mappings is a sane default but I think it works reasonably well.
Would close #1766 as everything needed would be provided then I suppose.
Type of change
Please delete options that are not relevant.
- New feature (non-breaking change which adds functionality)
- This change requires a documentation update
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list relevant details about your configuration
- Moving from one picker to another
- select mode mappings
Snippet:
require("telescope.builtin").find_files({
initial_mode = "select",
default_text = "test",
attach_mappings = function(prompt_bufnr, map)
map("s", "<C-e>", function()
print("test")
end)
return true
end,
})
Checklist:
- [x] My code follows the style guidelines of this project (stylua)
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation (lua annotations)
TODO
- [ ] Discuss if we'd want that @Conni2461 -- I'm open to the idea because it seems relatively easy to implement (ofc might have forgotten something) and has utility as the linked issues show (plus maybe some ideas around always starting of with prior prompt or something)
- [ ] Docs
cc @rkoval if you wouldn't mind stress testing this branch :)
ahh awesome!
i just tried this out though, and i'm still seeing the same exact CPU pegging issue i described here when using initial_mode = 'select' on two different macbooks. i tried to remove all configuration from my telescope to isolate it to just this, but it still occurred. i may have to try and throw together a barebones nvim setup to troubleshoot where specifically this could be coming from because i have a decent amount of plugins in my setup
EDIT: to clarify, this happens in git_files and live_grep, though i haven't tested other pickers (i'm guessing it's the same though?)
I cannot reproduce this issue. Seems to work just as other initial modes for these two pickers for me.
@fdschmidt93 Also gave it a try. Work nice, great job :)

So once this gets merged, how would one get the content from the visual selection to actually show up (asking for https://github.com/nvim-telescope/telescope.nvim/issues/1766)?
In the example here the content is static and I assume defined with default_text = "test".
So once this gets merged, how would one get the content from the visual selection to actually show up (asking for https://github.com/nvim-telescope/telescope.nvim/issues/1766)?
You have a function that extracts the visual selection for you (lots on stackoverflow and there is an upstream neovim PR waiting for an owner) and pass that to default_text.
Not part (and 100% won't be) of this PR. It's agreed neovim will have a function for this in lua space, just somebody would have to pick it up.
@fdschmidt93 Thanks for the tips. Found a snippet here.
I've managed to get it working with the code below and your PR:
function vim.getVisualSelection()
vim.cmd('noau normal! "vy"')
local text = vim.fn.getreg('v')
vim.fn.setreg('v', {})
text = string.gsub(text, "\n", "")
if #text > 0 then
return text
else
return ''
end
end
function live_grep()
text = vim.getVisualSelection()
require("telescope.builtin").live_grep({
initial_mode = "select",
default_text = text})
end
vim.keymap.set({'n','v'} , '<Leader>r', live_grep)
In the screencast below I'm visually selecting "Gaze" and pressing <Leader> + r.
This results in a live grep with "Gaze" preselected.

@fdschmidt93 You know if there something I can help you with to get this PR merged?
Patience? I'm waiting for Conni's feedback as to whether he'd agree that we should have this in this capacity and then I guess I'd write docs.
I already pinged him in the OP, so he'll consider it when he can/wants to make time for it.
Sorry for the late response. I am really swamped lately with everything :laughing:
We can do this, the gif looks like fun and there is some demand for it. I also think it doesn't break normal and insert mode. So if you finish the docs, feel free to merge.
Totally unrelated, we should talk about backporting, future development, (retrospective for 0.1.0) ...
We can do this, the gif looks like fun and there is some demand for it. I also think it doesn't break normal and insert mode. So if you finish the docs, feel free to merge.
Ok cool :) will do.
Totally unrelated, we should talk about backporting, future development, (retrospective for 0.1.0) ...
Sure, I'm relatively flexible timewise after my deadline now, just ping me on Discord whenever you're free.
@fdschmidt93 Hey, experiencing an issue. Can't directly trace it to this code but it is triggered by it. Posting here in case you have some ideas if it might be related to the PR.
Working code
-- Telescope find_files with a custom rg command and ivy theme
function find_files()
function_name = {"rg", "--files", "--no-ignore-vcs" }
text = vim.getVisualSelection()
builtin = require('telescope.builtin')
builtin.find_files({find_command = function_name,
-- initial_mode = "select",
default_text = text})
end
-- Key mappings
vim.keymap.set({'n', 'v'}, '<Leader>f', find_files)
Not working code
-- Telescope find_files with a custom rg command and ivy theme
function find_files()
function_name = {"rg", "--files", "--no-ignore-vcs" }
text = vim.getVisualSelection()
builtin = require('telescope.builtin')
builtin.find_files({find_command = function_name,
initial_mode = "select",
default_text = text})
end
-- Key mappings
vim.keymap.set({'n', 'v'}, '<Leader>f', find_files)
Issue manifestation
Pressing <leader> + f opens the telescope window but then everything blocks. Couldn't find a responsive key to which nvim would react to.
After killing nvim from a different terminal I saw something which looks like an error message:
Error executing vim.schedule lua callback: ...ck/packer/start/telescope.nvim/lua/telescope/pickers.lua:1503: attempt to index a nil value
stack traceback:
...ck/packer/start/telescope.nvim/lua/telescope/pickers.lua:1503: in function '_get_prompt'
...ck/packer/start/telescope.nvim/lua/telescope/pickers.lua:1243: in function ''
vim/_editor.lua: in function <vim/_editor.lua:0>
[telescope] [WARN 20:19:17] ...ck/packer/start/telescope.nvim/lua/telescope/pickers.lua:491: Finder failed with msg: ...ck/packer/start/telescope.nvim/lua/telescope/pickers.lua:1160: Failed to delete line
Try again, I fixed something that might have caused this.
Please next time point out your error rather than pasting unformatted code and provide more context (your initial mode etc).
E: I amended the original commit, make sure you properly update.
Tried, still occurring. I've managed to trace is to a specific step.
- Start new instance of
nvimnot opening any file - Press
Leader + f- moving up and down the telescope list works - Start typing letters to search for a file - now everything blocks
The case when there is something visually selected before pressing Leader + f doesn't block nvim and works ok.
Please next time point out your error rather than pasting unformatted code and provide more context (your initial mode etc).
Sure, hope the above gives more context.
As for the error part, I got that by accident (it was printed after nvim was killed with kill from a different terminal).
Not sure what you mean by "pointing our your error".
Anyways, do suggest, will gladly adapt if it makes it simpler on your end.
Not sure what you mean by "pointing our your error".
Sorry, I wasn't clear, I just meant having to visually parse unhighlighted and unformatted code myself. Either embed it with syntax highlighting (```lua at beginning of code block) or describe it properly.
I think I've understood the problem: can you confirm that the crashes only happen if there is no visual selection, that is the text you return is ''?
@rkoval that might also be your problem -- crashes/getting stuck I can reproduce with something like
-- no default_text
require "telescope.builtin".find_files { initial_mode = "select" }
Though there is no use case for initial_mode = "select" if there is no default_text I suppose and we should then fall back to initial_mode = "insert" to avoid the crash, which I presume might stem from a faulty interaction of select mode and prompt buffers.
I think I've understood the problem: can you confirm that the crashes only happen if there is no visual selection, that is the text you return is ''?
I gave it another test and made a screencast.

- For the first case I'm running a
live_grepwithselecton an empty visual selection. This doesn't cause the blocking - I'm running
find_fileson a visually selected "Gaze". All works as expected. - I'm running
find_fileson empty visual selection. This blocks it.
heyo! i'm wondering if this branch can be rebased/merged up from master to potentially retest the hanging/crashing issue we were seeing? i'm wondering if related code that touched these codepaths were modified and this hanging issue is no longer a problem. i know it's a long shot, but just wanting to make some traction on this feature given that i miss it!
Apologies, I won't see this through at the very least anytime soon and quite likely at all. My foreseeable efforts into contributing new features will be focused on telescope-file-browser.nvim.