From 46a31655da58c3b38b4611457d35fd41608b3958 Mon Sep 17 00:00:00 2001 From: Igor Date: Fri, 12 Jun 2026 00:22:19 -0300 Subject: [PATCH 1/2] feat: restore buffers after loading a session --- doc/nvim-tree-lua.txt | 6 +++ lua/nvim-tree/_meta/config/experimental.lua | 3 ++ lua/nvim-tree/autocmd.lua | 52 +++++++++++++++++++++ lua/nvim-tree/config.lua | 1 + 4 files changed, 62 insertions(+) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index c63b85beb51..cf32ebf45fe 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1911,6 +1911,11 @@ Config: experimental *nvim-tree-config-experimental* In the event of a problem please disable the experiment and raise an issue. + Fields: ~ + • {session_restore_nvim}? (`boolean`, default: `false`) Restore + nvim-tree buffers when restoring vim sessions + (requires 0.13+). + ============================================================================== Config: log *nvim-tree-config-log* @@ -2216,6 +2221,7 @@ Following is the default configuration, see |nvim_tree.config| for details. >lua persist = false, }, experimental = { + session_restore_nvim = false }, log = { enable = false, diff --git a/lua/nvim-tree/_meta/config/experimental.lua b/lua/nvim-tree/_meta/config/experimental.lua index da8467cf0a3..2d48842ee2c 100644 --- a/lua/nvim-tree/_meta/config/experimental.lua +++ b/lua/nvim-tree/_meta/config/experimental.lua @@ -9,6 +9,9 @@ error("Cannot require a meta file") --- ---@class nvim_tree.config.experimental --- +---Restore nvim-tree buffers when restoring vim sessions (requires 0.13+). +---(default: `false`) +---@field session_restore_nvim? boolean --Example below for future reference: -- --Buffers opened by nvim-tree will use with relative paths instead of absolute. diff --git a/lua/nvim-tree/autocmd.lua b/lua/nvim-tree/autocmd.lua index 2056927ffc0..5c9b7915e28 100644 --- a/lua/nvim-tree/autocmd.lua +++ b/lua/nvim-tree/autocmd.lua @@ -17,6 +17,58 @@ function M.global() end, }) + if vim.fn.has("nvim-0.13") == 1 and config.g.experimental.session_restore_nvim then + vim.api.nvim_create_autocmd("SessionWritePre", { ---@diagnostic disable-line: param-type-mismatch + group = augroup_id, + callback = function() + local cwd = require("nvim-tree.core").get_cwd() + vim.g.NvimTreeState = vim.json.encode({ cwd = cwd }) + end, + }) + + vim.api.nvim_create_autocmd("SessionLoadPost", { + group = augroup_id, + callback = function() + local api = require("nvim-tree.api").tree + + ---@type table + local tabs = {} + + local session_state = vim.json.decode(vim.g.NvimTreeState or "{}") or {} + local path = session_state and session_state.cwd or nil + + -- Save tabs with leftover nvim-tree buffers + for _, tab in ipairs(vim.api.nvim_list_tabpages()) do + for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do + local buf = vim.api.nvim_win_get_buf(win) + if api.is_tree_buf(buf) then + tabs[tab] = true + end + end + end + + -- The new window may inherit options from the current one if we don't schedule + vim.schedule(function() + api.close_in_all_tabs() + + for tab, _ in pairs(tabs) do + if vim.api.nvim_tabpage_is_valid(tab) then + local win = vim.api.nvim_tabpage_get_win(tab) + + -- Invoke `open` from each tab's current window to prevent having to switch tabs + if vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_call(win, function() + api.open({ path = path }) + end) + end + end + end + end) + end, + }) + end + + if config.g.tab.sync.open then vim.api.nvim_create_autocmd("TabEnter", { group = augroup_id, diff --git a/lua/nvim-tree/config.lua b/lua/nvim-tree/config.lua index 24d87c389a2..ae7d2fbfdd8 100644 --- a/lua/nvim-tree/config.lua +++ b/lua/nvim-tree/config.lua @@ -297,6 +297,7 @@ M.d = { -- config-default-start persist = false, }, experimental = { + session_restore_nvim = false }, log = { enable = false, From 5cebd0854b302f9063b62b518ab98ac0c70695ce Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 6 Jul 2026 20:24:30 -0300 Subject: [PATCH 2/2] docs: restoring cwd requires sessionoptions 'globals' flag --- doc/nvim-tree-lua.txt | 4 +++- lua/nvim-tree/_meta/config/experimental.lua | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index cf32ebf45fe..d906734f89d 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -1914,7 +1914,9 @@ Config: experimental *nvim-tree-config-experimental* Fields: ~ • {session_restore_nvim}? (`boolean`, default: `false`) Restore nvim-tree buffers when restoring vim sessions - (requires 0.13+). + (requires 0.13+). To restore the working + directory, ensure that |'sessionoptions'| + includes "globals" ============================================================================== Config: log *nvim-tree-config-log* diff --git a/lua/nvim-tree/_meta/config/experimental.lua b/lua/nvim-tree/_meta/config/experimental.lua index 2d48842ee2c..3cdda90647d 100644 --- a/lua/nvim-tree/_meta/config/experimental.lua +++ b/lua/nvim-tree/_meta/config/experimental.lua @@ -10,6 +10,7 @@ error("Cannot require a meta file") ---@class nvim_tree.config.experimental --- ---Restore nvim-tree buffers when restoring vim sessions (requires 0.13+). +---To restore the working directory, ensure that |'sessionoptions'| includes "globals" ---(default: `false`) ---@field session_restore_nvim? boolean --Example below for future reference: