telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

Ability to cycle through pickers in results window, maintaining search criteria

Open ssoriche opened this issue 4 years ago • 5 comments

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>

ssoriche avatar Mar 01 '21 01:03 ssoriche

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

skbolton avatar Mar 04 '21 23:03 skbolton

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 :)

Conni2461 avatar Mar 05 '21 08:03 Conni2461

Is there any update on maintaining search criteria? Just jumping to the right picker is easy, typing is hard :smile_cat:

thraizz avatar Jun 20 '21 08:06 thraizz

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?

smackesey avatar Jul 21 '21 21:07 smackesey

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 avatar Jul 14 '22 14:07 lucc

@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.

npearson72 avatar Dec 23 '22 01:12 npearson72

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.

sublipri avatar Jul 30 '23 09:07 sublipri