Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Auto expand to current file #2911

Unanswered
BhasherBEL asked this question in Q&A
Sep 12, 2024 · 1 comments · 2 replies
Discussion options

Is your feature request related to a problem? Please describe.
When opening a file, i.e. with nvim a/b/c.sh, nvim tree shows the current folder, which is really nice, but don't expand to the opened file.

Describe the solution you'd like
It would be nice to be able to automatically expend the tree up to the current file, at least at startup, but probably also when using commands such as :split, :edit, ...

Describe alternatives you've considered
For now, I manually expand the tree, but it's not really a long-term solution.

You must be logged in to vote

Replies: 1 comment · 2 replies

Comment options

You can automatically expand the tree to focus on the current file, with several options to customise this: :help nvim-tree-opts-update-focused-file

It's turned off by default.

There are also several recipes here to define startup behaviour: wiki: Open At Startup

I reckon "Open For Files And [No Name] Buffers" might suit you.

You must be logged in to vote
2 replies
@CoinCheung
Comment options

@alex-courtis Would you please show me how to use that function, and how to write it to my init.lua?

@alex-courtis
Comment options

Before anything disable netrw Hijacking

-- start of your init.lua
vim.g.loaded_netrw       = 1
vim.g.loaded_netrwPlugin = 1

Create an open function to match your tastes. This might suit you:

local function open_nvim_tree(data)

  -- buffer is a real file on the disk
  local real_file = vim.fn.filereadable(data.file) == 1

  if not real_file then
    return
  end

  -- open the tree, find the file but don't focus it
  require("nvim-tree.api").tree.toggle({ focus = false, find_file = true, })
end

Create a hook to call that function on vim enter. After everything else:

local function open_nvim_tree()

  -- open the tree
  require("nvim-tree.api").tree.open()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
3 participants
Converted from issue

This discussion was converted from issue #2905 on September 14, 2024 02:40.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.