Nvim-tree.lua: Prevent open a buffer in NvimTree window?

Created on 27 Feb 2021  路  11Comments  路  Source: kyazdani42/nvim-tree.lua

I often accidentally open a buffer in NvimTree window, via Fzf, tabline buffers, etc. That's quite annoying.

I tried to mimic the NerdTree solution: https://github.com/preservim/nerdtree#how-can-i-prevent-other-buffers-replacing-nerdtree-in-its-window

autocmd BufEnter * if expand('%') =~ 'NvimTree' && bufname('%') !~ 'NvimTree' && winnr('$') > 1 |
    \ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif

But it doesn't work.

Most helpful comment

just fixed this in latest master, it might be clunky but if you feel it good, you don't have to make mappings anymore :)

All 11 comments

you could bind you fzf keys in the tree buffer to avoid that. My solution for this:

vim.g.nvim_tree_bindings = {
    ["<C-p>"] = ":wincmd l | Files<CR>",
    ["<C-t>"] = ":wincmd l | RG<CR>",
    ["<C-b>"] = ":wincmd l | Buffers<CR>",
}

Do you have a solution for the autocmd BufEnter. It would work without having to bind other plugins's keymaps. I don't know how to by "clicking to tab" like this.

I cannot figure out the different between NvimTree and NerdTree windows and buffers that make the similar solution does not work.

hum i'm not sure, I've always struggled with this issue. I never did master vimscript unforntunatly.

just fixed this in latest master, it might be clunky but if you feel it good, you don't have to make mappings anymore :)

It still opens other buffers from fzf pickers for me. Not always though, didn't find a pattern when it does and when it doesn't happen.

hum strange, i'll reopen this if i can reproduce

Thanks, I will come back to you if I find a reproducible pattern. At the moment I just keep trying to open files with fzf from nvim-tree and most of the times they open in window on the right (so it works) but every now and then it will eventually open in nvim-tree buffer.

thanks for the debugging though, i really appreciate it ! I'll do more debugging myself then

just fixed that in latest master, seems like setting the cursor after the BufEnter/find_file command was erroring because of the new buffers being put in the tree window. Also deferred the autocmd on the event loop to avoid conflicting with the find file command

Seems to work well now, thanks!

@kyazdani42 while the autocmd works, the buffer that opens is not "of the same type" (background color is like the one in the NvimTree window) and the line numbers for example are missing. See an example:

NvimTree + a regular buffer:
image

Now, if I open another file within the NvimTree window, the autocmd kicks in, opening the buffer in another window but the buffer looks different:

image

Note that the line numbers are missing, the background color is different, etc. Do you know what can be causing that and how to address that?

EDIT: I just realized that the autocmd is not necessary and that the plugin is taking care of that. The question of why the new buffer has no line numbers and different background still applies though

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gmr458 picture gmr458  路  5Comments

seblj picture seblj  路  6Comments

chinnkarahoi picture chinnkarahoi  路  7Comments

DimitrisMilonopoulos picture DimitrisMilonopoulos  路  5Comments

kristijanhusak picture kristijanhusak  路  5Comments