-
-
Notifications
You must be signed in to change notification settings - Fork 889
Configuration Recipes
A place for the community to share configurations and custom pickers that dont fit into core or an extension
If you'd prefer Telescope not to enter a normal-like mode when hitting escape (and instead exiting), you can map <Esc>
to do so via:
local actions = require("telescope.actions")
require("telescope").setup{
defaults = {
mappings = {
i = {
["<esc>"] = actions.close
},
},
}
}
If you'd prefer Telescope to clear the prompt on <C-u>
rather than scroll the previewer add the following to your config:
local actions = require("telescope.actions")
require("telescope").setup{
defaults = {
mappings = {
i = {
["<C-u>"] = false
},
},
}
}
If you want to add a mapping to toggle the previewer (when applicable), you can map it using the toggle_preview
action.
For example, you can map <M-p>
to do so via:
local action_layout = require("telescope.actions.layout")
require("telescope").setup{
defaults = {
mappings = {
n = {
["<M-p>"] = action_layout.toggle_preview
},
i = {
["<M-p>"] = action_layout.toggle_preview
},
},
}
}
-- telescope-config.lua
local M = {}
M.project_files = function()
local opts = {} -- define here if you want to define something
local ok = pcall(require"telescope.builtin".git_files, opts)
if not ok then require"telescope.builtin".find_files(opts) end
end
return M
-- call via:
-- :lua require"telescope-config".project_files()
-- example keymap:
-- vim.api.nvim_set_keymap("n", "<Leader><Space>", "<CMD>lua require'telescope-config'.project_files()<CR>", {noremap = true, silent = true})
Credits to @Conni2461 for the project_files snippet.
Require at least fd v0.8.3. Prior to this version there shouldn't be a ./
require("telescope").setup {
defaults = {
-- ....
},
pickers = {
find_files = {
find_command = { "fd", "--type", "f", "--strip-cwd-prefix" }
},
}
}
Credits to @numToStr
To trim the indentation at the beginning of presented line in the result window, change the defaults like shown below:
require("telescope").setup {
defaults = {
vimgrep_arguments = {
"rg",
"--color=never",
"--no-heading",
"--with-filename",
"--line-number",
"--column",
"--smart-case",
"--trim" -- add this value
}
}
}
This is a snippet on how you can add change directory functionality to some pickers, like find files.
require("telescope").setup {
defaults = {
-- ....
},
pickers = {
find_files = {
mappings = {
n = {
["cd"] = function(prompt_bufnr)
local selection = require("telescope.actions.state").get_selected_entry()
local dir = vim.fn.fnamemodify(selection.path, ":p:h")
require("telescope.actions").close(prompt_bufnr)
-- Depending on what you want put `cd`, `lcd`, `tcd`
vim.cmd(string.format("silent lcd %s", dir))
end
}
}
},
}
}
Credits to @ranjithshegde
local previewers = require("telescope.previewers")
local _bad = { ".*%.csv", ".*%.lua" } -- Put all filetypes that slow you down in this array
local bad_files = function(filepath)
for _, v in ipairs(_bad) do
if filepath:match(v) then
return false
end
end
return true
end
local new_maker = function(filepath, bufnr, opts)
opts = opts or {}
if opts.use_ft_detect == nil then opts.use_ft_detect = true end
opts.use_ft_detect = opts.use_ft_detect == false and false or bad_files(filepath)
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
require("telescope").setup {
defaults = {
buffer_previewer_maker = new_maker,
}
}
Credit: @Conni2461
local previewers = require("telescope.previewers")
local new_maker = function(filepath, bufnr, opts)
opts = opts or {}
filepath = vim.fn.expand(filepath)
vim.loop.fs_stat(filepath, function(_, stat)
if not stat then return end
if stat.size > 100000 then
return
else
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
end)
end
require("telescope").setup {
defaults = {
buffer_previewer_maker = new_maker,
}
}
Credit: @Conni2461
local previewers = require("telescope.previewers")
local putils = require("telescope.previewers.utils")
local pfiletype = require("plenary.filetype")
local new_maker = function(filepath, bufnr, opts)
opts = opts or {}
if opts.use_ft_detect == nil then
local ft = pfiletype.detect(filepath)
-- Here for example you can say: if ft == "xyz" then this_regex_highlighing else nothing end
opts.use_ft_detect = false
putils.regex_highlighter(bufnr, ft)
end
previewers.buffer_previewer_maker(filepath, bufnr, opts)
end
require("telescope").setup {
defaults = {
buffer_previewer_maker = new_maker,
}
}
Credit: @Conni2461
local previewers = require("telescope.previewers")
local Job = require("plenary.job")
local new_maker = function(filepath, bufnr, opts)
filepath = vim.fn.expand(filepath)
Job:new({
command = "file",
args = { "--mime-type", "-b", filepath },
on_exit = function(j)
local mime_type = vim.split(j:result()[1], "/")[1]
if mime_type == "text" then
previewers.buffer_previewer_maker(filepath, bufnr, opts)
else
-- maybe we want to write something to the buffer here
vim.schedule(function()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "BINARY" })
end)
end
end
}):sync()
end
require("telescope").setup {
defaults = {
buffer_previewer_maker = new_maker,
}
}
Credit: @Conni2461
It is possible to use a terminal image viewer such as catimg to preview images in telescope.
require("telescope").setup {
defaults = {
preview = {
mime_hook = function(filepath, bufnr, opts)
local is_image = function(filepath)
local image_extensions = {'png','jpg'} -- Supported image formats
local split_path = vim.split(filepath:lower(), '.', {plain=true})
local extension = split_path[#split_path]
return vim.tbl_contains(image_extensions, extension)
end
if is_image(filepath) then
local term = vim.api.nvim_open_term(bufnr, {})
local function send_output(_, data, _ )
for _, d in ipairs(data) do
vim.api.nvim_chan_send(term, d..'\r\n')
end
end
vim.fn.jobstart(
{
'catimg', filepath -- Terminal image viewer command
},
{on_stdout=send_output, stdout_buffered=true})
else
require("telescope.previewers.utils").set_preview_message(bufnr, opts.winid, "Binary cannot be previewed")
end
end
},
}
}
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local function run_selection(prompt_bufnr, map)
actions.select_default:replace(function()
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
vim.cmd([[!git log ]]..selection[1])
end)
return true
end
M.git_log = function()
-- example for running a command on a file
local opts = {
attach_mappings = run_selection
}
require('telescope.builtin').find_files(opts)
end
return M
local action_state = require "telescope.actions.state"
-- pressing set keybind in normal mode over a buffer will close it.
-- selecting multiple buffers and then pressing the keybind will close all selected.
-- you can decide to force files with changes to close.
local close_buffers = function(prompt_bufnr, map)
map("n", "<c-b>", function()
action_state.get_current_picker(prompt_bufnr):delete_selection(function(sel)
vim.api.nvim_buf_delete(sel.bufnr, { force=false, unload=false })
end)
end)
return true
end
require("telescope.builtin").buffers({ attach_mappings = close_buffers })
-- in lua/finders.lua
local finders = {}
-- Dropdown list theme using a builtin theme definitions :
local center_list = require"telescope.themes".get_dropdown({
winblend = 10,
width = 0.5,
prompt = " ",
results_height = 15,
previewer = false,
})
-- Settings for with preview option
local with_preview = {
winblend = 10,
show_line = false,
results_title = false,
preview_title = false,
layout_config = {
preview_width = 0.5,
},
}
-- Find in neovim config with center theme
finders.fd_in_nvim = function()
local opts = vim.deepcopy(center_list)
opts.prompt_prefix = "Nvim>"
opts.cwd = vim.fn.stdpath("config")
require"telescope.builtin".fd(opts)
end
-- Find files with_preview settings
function fd()
local opts = vim.deepcopy(with_preview)
opts.prompt_prefix = "FD>"
require"telescope.builtin".fd(opts)
end
return finders
-- make sure to map it:
-- nnoremap <leader>ff :lua require"finders".fd_in_nvim()<cr>
-- nnoremap <leader>ff :lua require"finders".fd()<cr>
local center_list -- check the above snippet
local with_preview -- check the above snippet
local main = {}
local telescopes = {
fd_nvim = {
prompt_prefix = "Nvim>",
fun = "fd",
theme = center_list,
cwd = vim.fn.stdpath("config")
-- .. other options
}
fd = {
prompt_prefix = "Files>",
fun = "fd",
theme = with_preview,
-- .. other options
}
}
main.run = function(str, theme)
local base, fun, opts
if not telescopes[str] then
fun = str
opts = theme or {}
--return print("Sorry not found")
else
base = telescopes[str]
fun = base.fun; theme = base.theme
base.theme = nil; base.fun = nil
opts = vim.tbl_extend("force", theme, base)
end
if str then
return require"telescope.builtin"[fun](opts)
else
return print("You need to a set a default function")
-- return require"telescope.builtin".find_files(opts)
end
end
return main
-- make sure to map it:
-- nnoremap <leader>ff :lua require"main".run("fd")<cr>
-- nnoremap <leader>ff :lua require"main".run("fd_in_nvim")<cr>

-- In: lua/rc/telescope/my_make_entry.lua
local my_make_entry = {}
local devicons = require"nvim-web-devicons"
local entry_display = require("telescope.pickers.entry_display")
local filter = vim.tbl_filter
local map = vim.tbl_map
function my_make_entry.gen_from_buffer_like_leaderf(opts)
opts = opts or {}
local default_icons, _ = devicons.get_icon("file", "", {default = true})
local bufnrs = filter(function(b)
return 1 == vim.fn.buflisted(b)
end, vim.api.nvim_list_bufs())
local max_bufnr = math.max(unpack(bufnrs))
local bufnr_width = #tostring(max_bufnr)
local max_bufname = math.max(
unpack(
map(function(bufnr)
return vim.fn.strdisplaywidth(vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":p:t"))
end, bufnrs)
)
)
local displayer = entry_display.create {
separator = " ",
items = {
{ width = bufnr_width },
{ width = 4 },
{ width = vim.fn.strwidth(default_icons) },
{ width = max_bufname },
{ remaining = true },
},
}
local make_display = function(entry)
return displayer {
{entry.bufnr, "TelescopeResultsNumber"},
{entry.indicator, "TelescopeResultsComment"},
{entry.devicons, entry.devicons_highlight},
entry.file_name,
{entry.dir_name, "Comment"}
}
end
return function(entry)
local bufname = entry.info.name ~= "" and entry.info.name or "[No Name]"
local hidden = entry.info.hidden == 1 and "h" or "a"
local readonly = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and "=" or " "
local changed = entry.info.changed == 1 and "+" or " "
local indicator = entry.flag .. hidden .. readonly .. changed
local dir_name = vim.fn.fnamemodify(bufname, ":p:h")
local file_name = vim.fn.fnamemodify(bufname, ":p:t")
local icons, highlight = devicons.get_icon(bufname, string.match(bufname, "%a+$"), { default = true })
return {
valid = true,
value = bufname,
ordinal = entry.bufnr .. " : " .. file_name,
display = make_display,
bufnr = entry.bufnr,
lnum = entry.info.lnum ~= 0 and entry.info.lnum or 1,
indicator = indicator,
devicons = icons,
devicons_highlight = highlight,
file_name = file_name,
dir_name = dir_name,
}
end
end
return my_make_entry
-- Use in telescope buffers
require("telescope.builtin").buffers({
-- ...
entry_maker = require"vimrc.telescope.my_make_entry".gen_from_buffer_like_leaderf(),
})
Credit: @elianiva
local previewers = require("telescope.previewers")
local pickers = require("telescope.pickers")
local sorters = require("telescope.sorters")
local finders = require("telescope.finders")
pickers.new {
results_title = "Resources",
-- Run an external command and show the results in the finder window
finder = finders.new_oneshot_job({"terraform", "show"}),
sorter = sorters.get_fuzzy_file(),
previewer = previewers.new_buffer_previewer {
define_preview = function(self, entry, status)
-- Execute another command using the highlighted entry
return require('telescope.previewers.utils').job_maker(
{"terraform", "state", "list", entry.value},
self.state.bufnr,
{
callback = function(bufnr, content)
if content ~= nil then
require('telescope.previewers.utils').regex_highlighter(bufnr, 'terraform')
end
end,
})
end
},
}:find()