Ability to cycle through pickers in results window, maintaining search criteria
Is your feature request related to a problem? Please describe.
Currently telescope allows me to specify what I'd like to search for and what I'm searching for, with no way to change what I'd like to search without starting over. Before migrating to telescope, I had fzf set to open buffers but on pressing ctrl+f it would switch with the same search criteria to git status, and pressing again would switch to old files. ctrl-b would cycle back through the same types.
Describe the solution you'd like I'd like to be able to specify a list of pickers and their order, when telescope is activated it would use the first, and when displaying, use a key combination to move to the next picker in the list with the same search, with another combination to move to the previous.
Additional context This is the implementation using fzf in viml running in Neovim 0.4.x
function! s:fzf_next(idx)
let commands = ['Buffers', 'GFiles?', 'Files', 'History']
execute commands[a:idx]
let next = (a:idx + 1) % len(commands)
let previous = (a:idx - 1) % len(commands)
execute 'tnoremap <buffer> <silent> <c-f> <c-\><c-n>:close<cr>:sleep 100m<cr>:call <sid>fzf_next('.next.')<cr> execute 'tnoremap <buffer> <silent> <c-b> <c-\><c-n>:close<cr>:sleep 100m<cr>:call <sid>fzf_next('.previous.')endfunction
command! Cycle call <sid>fzf_next(0)
nnoremap <silent> <leader><space> :Cycle<cr>
Came here to create the same feature request. I often run the keybind to open recent buffers to realize that I haven't opened that buffer yet. Would be awesome to run a keybind to change to searching all files
I can send you a snippet, this evening. Everything should be somewhat possible, besides the maintaining search criteria. That should be possible soon. And we might have to expose some internal action to make it better. I need to test some things first :)
Is there any update on maintaining search criteria? Just jumping to the right picker is easy, typing is hard :smile_cat:
I'm having trouble with a related use case-- I'm trying to select a directory in the find_files picker and then restart find_files with a recursive search from that directory. I can't get it to work right. I have a custom action where I try to close the first picker with require('telescope.actions').close(prompt_bufnr), but if I do this then my follow-up find_files call doesn't work correctly.
What's the right way to close a picker inside an action and restart another one?
I have hacked together a key binding that I am currently using. I use the Key <c-space> to open telescope and also to cycle through some pickers. The relevant pickers are defined in the local table/list variable pickers.
Here is the config function I use to set it up with Packer.nvim. I think it should be possible to just call this config function if you are installing telescope not via packer.nvim.
config = function()
local builtin = require "telescope.builtin"
local pickers = {
builtin.oldfiles,
builtin.find_files,
index = 1,
}
pickers.cycle = function()
if pickers.index >= #pickers then
pickers.index = 1
else
pickers.index = pickers.index + 1
end
pickers[pickers.index] { default_text = require "telescope.actions.state".get_current_line() }
end
local actions = require 'telescope.actions'
require'telescope'.setup {
defaults = { mappings = { i = { ["<C-Space>"] = pickers.cycle } } }
}
local opts = {silent = true, noremap = true} --, callback = function() pickers.index = 1 end}
vim.api.nvim_set_keymap("n", "<C-Space>", "<CMD>Telescope oldfiles<CR>", opts)
vim.api.nvim_set_keymap("i", "<C-Space>", "<CMD>Telescope oldfiles<CR>", opts)
end
There still is a bug: the callback on nvim_set_keymap does not work for me and index is not reset when I close and open telescope again.
@lucc I don't know lua and am new to telescope, so I'm sure there's a more elegant way to do this, but here's what I did to get this working:
builtin = require "telescope.builtin"
actions = require "telescope.actions"
pickers = {
builtin.find_files,
builtin.oldfiles,
index = 1,
}
pickers.cycle = function()
if pickers.index >= #pickers then
pickers.index = 1
else
pickers.index = pickers.index + 1
end
pickers[pickers.index] { default_text = require "telescope.actions.state".get_current_line() }
end
pickers.close = function(prompt_bufnr)
pickers.index = 1
actions.close(prompt_bufnr)
end
I basically reset the index by wrapping the telescope close function
Then I add these mappings:
require('telescope').setup {
defaults = {
mappings = {
i = {
["<esc>"] = {
pickers.close, type = "action",
opts = { nowait = true, silent = true }
},
["<C-f>"] = {
pickers.cycle,
type = "action",
opts = { nowait = true, silent = true }
},
},
},
},
}
And you may need to also map selections, ex:
pickers.select_default = function(prompt_bufnr)
pickers.index = 1
actions.select_default(prompt_bufnr, "default")
end
...
["<CR>"] = {
pickers.select_default, type = "action",
opts = { nowait = true, silent = true }
},
...
Works, but perhaps there's a cleaner way.
Here's a gist of my lazy.nvim config that implements this. To add more pickers edit the 'picker_map' and 'ordered_pickers' tables. New to Lua so it might not be that efficient but it's working well for me so far.