telescope keybindings don't work in nvim-tree

1.8k views Asked by At

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 })
2

There are 2 answers

0
casonadams On

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 and telescope.lua into the lua directory. But I would put and _ infront of each filename so there won't be any conflicts.

File structure

├── lua
│   ├── _tree.lua
│   ├── _telescope.lua

Then in your plugins.lua (or you call it packer.lua) file. Which I would recommend is in the new lua directory you created.

-- lua/_plugins.lua

  use({
    "nvim-telescope/telescope.nvim",
    requires = {
      "nvim-lua/plenary.nvim",
      "kyazdani42/nvim-web-devicons"
    },
    config = function()
      require("_telescope")
    end,
  })
  use ({
    'nvim-tree/nvim-tree.lua',
    requires = {
      'nvim-tree/nvim-web-devicons', -- optional, for file icons
    },
    config = function()
      require("_tree")
    end,
  })
-- init.lua

require("_options") -- other vim options etc...
require("_plugins") -- load in plugins from `lua/_plugins.lua` file
require("_mapping") -- load key mappings

Keymaps

-- lua/_mapping.lua

vim.keymap.set("n", "<leader>e", ":NvimTreeToggle<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<leader>f", ":Telescope find_files<CR>", { noremap = true, silent = true })
0
ssp On

Yes it's probably because your telescope keymaps are being overriden with nvim-tree's keymaps.

You could create your own custom mappings to avoid these conflicts.