2

Is there a way to highlight a directory of a found file?

For example I look for a specific file with telescope and when I open it, my nvim-tree is updated and shows me parent directory?

1

2 Answers 2

0

You can jump to the file in current buffer by using :NvimTreeFindFile. To do this automatically upon opening the file in telescope, you need to "overload" the open action of Telescope (called select_default, on - carriage return) to run NvimTreeFindFile command just after you open the file. You can do so by using transform_mod:

In ~/.config/nvim/init.vim put this configuration:

lua << EOF

local transform_mod = require('telescope.actions.mt').transform_mod
local actions = require('telescope.actions')
local mod = {}
mod.open_in_nvim_tree = function(prompt_bufnr)
    local cur_win = vim.api.nvim_get_current_win()
    vim.cmd("NvimTreeFindFile")
    vim.api.nvim_set_current_win(cur_win)
end

mod = transform_mod(mod)

require("telescope").setup{
    defaults = {
        mappings = {
            i = {
                ["<CR>"] = actions.select_default + mod.open_in_nvim_tree,
            },
            n = {
                ["<CR>"] = actions.select_default + mod.open_in_nvim_tree,
            },
        },
    },
}
EOF
0

Short answer

Nvim-tree provide this behavior out of the box.

You can achieve this by adding next lines for example:

require("nvim-tree").setup {
  sync_root_with_cwd = true,
  respect_buf_cwd = true,
  update_focused_file = {
    enable = true,
    update_root = {
      enable = true,
    }
  },
}

Explanation

sync_root_with_cwd

Changes the tree root directory on DirChanged and refreshes the three. (from h: sync_root_with_cwd)

respect_buf_cwd

Will change cwd of nvim-tree to that of new buffer's when opening nvim-tree. (from h: respect_buf_cwd)

update_focused_file

Update the focused file on BufEnter, up-collapses the folders recursively until if finds the file. (from h: update_focused_files)

update_focused_file.update_root

Update the root directory of the tree if the file is not under current root directory. It prefers vim's cwd and root_dirs. Otherwise it falls back to the folder containing the file. (from h: update_root)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.