Overview
I'm using both telescope and nvim-tree in Neovim. I've noticed that my telescope keybindings to search for files don't work when I'm focused in the nvim-tree buffer.
This is even more problematic since when I use nvim .
to open the current directory in Neovim, only the nvim-tree buffer is shown (and it's focused). Therefore, to use telescope to find any file, I first need to open a random file. Then I can use my telescope bindings.
If I read the docs correctly, I may be able to read the keymaps inside the nvim-tree configuration. However, ideally I would just have the keymaps in the telescope.lua
file to avoid duplication.
Configuration
-- packer.lua
return require('packer').startup(function(use)
-- Packer can manage itself
use 'wbthomason/packer.nvim'
use {
'nvim-telescope/telescope.nvim', tag = '0.1.0',
-- or , branch = '0.1.x',
requires = { { 'nvim-lua/plenary.nvim' } }
}
use {
'nvim-tree/nvim-tree.lua',
requires = {
'nvim-tree/nvim-web-devicons', -- optional, for file icon
},
tag = 'nightly' -- optional, updated every week. (see issue #1193)
}
-- after/plugin/telescope.lua
local builtin = require('telescope.builtin')
-- the following three keymaps are the ones that don't work in nvim-tree
vim.keymap.set('n', '<leader>pf', builtin.find_files, {})
vim.keymap.set('n', 'p', builtin.git_files, {})
vim.keymap.set('n', '<leader>ps', function()
builtin.grep_string({ search = vim.fn.input("Grep > ") })
end)
local telescope = require('telescope')
telescope.setup({
defaults = require('telescope.themes').get_dropdown({
layout_config = {
prompt_position = "top",
},
}),
})
-- after/plugin/tree.lua
-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
-- set termguicolors to enable highlight groups
vim.opt.termguicolors = true
require("nvim-tree").setup({
open_on_setup = true,
view = {
hide_root_folder = true,
},
update_focused_file = {
enable = true,
update_cwd = true,
},
renderer = {
icons = {
show = {
git = false,
},
}
},
})
vim.keymap.set("n", "<C-d>", ":NvimTreeFocus<CR>", { noremap = true, silent = true })
First I would recommend you restructure.
Create a directory
lua
in the root of where all your other files are. For me it is~/.config/nvim
.Then move
tree.lua
andtelescope.lua
into thelua
directory. But I would put and_
infront of each filename so there won't be any conflicts.File structure
Then in your plugins.lua (or you call it packer.lua) file. Which I would recommend is in the new
lua
directory you created.Keymaps