Search code examples
luaneovimlanguage-server-protocol

How to exclude files or directories in nvim lua based config?


I have nvim-tree/nvim-tree.lua plugin installed in my nvim lua based config. How can I exclude files or diractories so it wont show up in the nvim-tree window ?

Here is the code:

nvim-tree.lua

-- following options are the default
-- each of these are documented in `:help nvim-tree.OPTION_NAME`
-- vim.g.nvim_tree_ignore = { "" }

local status_ok, nvim_tree = pcall(require, "nvim-tree")
if not status_ok then
    return
end

--[[ local config_status_ok, nvim_tree_config = pcall(require, "nvim-tree.config") ]]
--[[ if not config_status_ok then ]]
--[[    return ]]
--[[ end ]]

--[[ local tree_cb = nvim_tree_config.nvim_tree_callback ]]

nvim_tree.setup({
    filters = {
        dotfiles = false,
        git_clean = false,
        no_buffer = false,
        custom = {},
        exclude = { 'node_modules' },
    },
    disable_netrw = true,
    hijack_netrw = true,
    --open_on_setup = false,
    open_on_tab = false,
    hijack_cursor = false,
    update_cwd = true,
    hijack_directories = {
        enable = true,
        auto_open = true,
    },
    diagnostics = {
        enable = true,
        icons = {
            hint = "",
            info = "",
            warning = "",
            error = "",
        },
    },
    update_focused_file = {
        enable = true,
        update_cwd = true,
        ignore_list = {},
    },
    git = {
        enable = true,
        ignore = true,
        timeout = 500,
    },
    view = {
        width = 30,
        --height = 30,
        hide_root_folder = false,
        side = "left",
        --auto_resize = true,
        mappings = {
            custom_only = false,
        },
        number = false,
        relativenumber = false,
    },
    actions = {
        --quit_on_open = true,
        --window_picker = { enable = true },
    },
    renderer = {
        highlight_git = true,
        root_folder_modifier = ":t",
        icons = {
            show = {
                file = true,
                folder = true,
                folder_arrow = true,
                git = true,
            },
            glyphs = {
                default = "",
                symlink = "",
                git = {
                    unstaged = "",
                    staged = "S",
                    unmerged = "",
                    renamed = "➜",
                    deleted = "",
                    untracked = "U",
                    ignored = "◌",
                },
                folder = {
                    default = "",
                    open = "",
                    empty = "",
                    empty_open = "",
                    symlink = "",
                },
            },
        },
    },
})

This is the snapshot of my current working directory:

enter image description here

I've also set the filter to exclude = { 'node_modules' }, but nothing seems to give any changes, I can still see the node_modules folder in the nvim-tree window ? Any idea ?


Solution

  • You understand incorrectly filters.exclude keyword for nvim-tree plugin. From plugin docs: "nvim-tree.filters.exclude - List of directories or files to exclude from filtering: always show them."

    If you want exclude some directories/files (that will not be shown), use filters.custom keyword :

    nvim_tree.setup({
        filters = {
            dotfiles = false,
            git_clean = false,
            no_buffer = false,
            custom = { 'node_modules' },
        },
    (...)
    })