Skip to content

Configuration Recipes

James Trew edited this page Jun 24, 2023 · 9 revisions

A place for the community to share configurations.

Table Of Contents

Actions

live_grep only within current path or multi-selected files

local fb_utils = require("telescope._extensions.file_browser.utils")
local action_state = require("telescope.actions.state")

require("telescope").setup({
  extensions = {
    file_browser = {
      mappings = {
        ["i"] = {
          ["<A-g>"] = function(prompt_bufnr)
            local selections = fb_utils.get_selected_files(prompt_bufnr, false)
            local search_dirs = vim.tbl_map(function(path) return path:absolute() end, selections)
            if vim.tbl_isempty(search_dirs) then
              local current_finder = action_state.get_current_picker(prompt_bufnr).finder
              search_dirs = { current_finder.path }
            end
            actions.close(prompt_bufnr)
            require("telescope.builtin").live_grep({ search_dirs = search_dirs })
          end,
        },
      },
    },
  },
})

Toggle between cwd and current (non-telescope) buffer path

local fb_utils = require("telescope._extensions.file_browser.utils")
local action_state = require("telescope.actions.state")
local Path = require("plenary.path")

require("telescope").setup({
  extensions = {
    file_browser = {
      mappings = {
        ["i"] = {
          ["<C-e>"] = function(prompt_bufnr)
            local current_picker = action_state.get_current_picker(prompt_bufnr)
            local finder = current_picker.finder
            local bufr_path = Path:new(vim.fn.expand("#:p"))
            local bufr_parent_path = bufr_path:parent():absolute()

            if finder.path ~= bufr_parent_path then
              finder.path = bufr_parent_path
              fb_utils.selection_callback(current_picker, bufr_path:absolute())
            else
              finder.path = vim.loop.cwd()
            end
            fb_utils.redraw_border_title(current_picker)
            current_picker:refresh(finder, {
              new_prefix = fb_utils.relative_path_prefix(finder),
              reset_prompt = true,
              multi = current_picker._multi,
            })
          end,
        },
      },
    },
  },
})

Input Callbacks

Trigger The File Browser on /

local os_sep = require "plenary.Path".sep
local action_state = require "telescope.actions.state"
local fb_actions = require "telescope".extensions.file_browser.actions

require("telescope").setup({
extensions = {
  file_browser = {
    on_input_filter_cb = function(prompt)
      if prompt:sub(-1, -1) == os_sep then
        local prompt_bufnr = vim.api.nvim_get_current_buf()
	if vim.bo[prompt_bufnr].filetype == "TelescopePrompt" then
	  local current_picker = action_state.get_current_picker(prompt_bufnr)
	  if current_picker.finder.files then
	  fb_actions.toggle_browser(prompt_bufnr, { reset_prompt = true })
	  current_picker:set_prompt(prompt:sub(1, -2))
         end
      end
     end
			end,
		},
	},
})

Make selection start at the second index

This is useful when you want to use file browser in normal mode and navigate as if you are in lf, ranger or nnn.

Telescope file_browser default_selection_index=2