Skip to content

Open nvim-tree on VimEnter but without focus #968

@kdauria

Description

@kdauria

I'm really enjoying this plugin, but I'm stuck trying to figure out how to open the nvim-tree window automatically when opening nvim without focusing on it.

I've been able to toggle nvim-tree without focus using something like this:

function M.show()
    require('nvim-tree.view').open({ focus_tree = false })
end

function M.toggle(find_file)
    local nvimtree = require 'nvim-tree'
    local view = require 'nvim-tree.view'
    if view.win_open() then
        view.close()
    else
        if M.setup_config.update_focused_file.enable or find_file then
            nvimtree.find_file(true)
        end
        M.show()
    end
end

function M.show_on_startup()
    require('nvim-tree').open()
    vim.cmd('wincmd p')
    -- this wincmd does NOT work and I can't figure out a workaround
    -- focus is always on tree after entering vim
end

vim.cmd [[
    command! NvimTreeShowToggle lua require'plugins.configs.nvim-tree'.toggle()
    command! NvimTreeShowStartup lua require'plugins.configs.nvim-tree'.show_on_startup()
]]

vim.cmd [[
    augroup nvimtree
        autocmd BufEnter * if (winnr('$') == 1 && &filetype == 'NvimTree') | q | endif
        autocmd VimEnter * NvimTreeShowStartup
    augroup END
]]

But, no matter what I try, I can't figure out how to get what I want. When I was using CHADTree, I was able to use CHADTree --nofocus. I thought it might be due to other autocommands that the plugin is loading, but disabling them didn't help (assuming I did it correctly).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions