From: Lukas Hägele Date: Sun, 29 Mar 2026 15:51:48 +0000 (+0200) Subject: fix cwd after changing working directories X-Git-Url: https://git.lhaegele.de/?a=commitdiff_plain;h=ca256846726502866b7fc5cde15498f5a57dde55;p=dotfiles_nvim.git fix cwd after changing working directories --- diff --git a/lua/snippets/vsplit.lua b/lua/snippets/vsplit.lua index 7c68b2f..59b5954 100644 --- a/lua/snippets/vsplit.lua +++ b/lua/snippets/vsplit.lua @@ -4,6 +4,7 @@ local M = {} M.reset = function(opts) opts = opts or {} + -- make sure that both windows exist if not (M.left_id and M.right_id) then -- remember window id of left pane for later M.left_id = vim.api.nvim_get_current_win() @@ -19,15 +20,24 @@ M.reset = function(opts) M.right_id = vim.api.nvim_open_win(0, true, win_config) end - if opts.start_explorer then - local start_explorer = function(win_id, cwd) + -- set working directory and start file browser (for each window) + do + local set_cwd = function(win_id, cwd) vim.api.nvim_set_current_win(win_id) - vim.cmd.Explore(cwd) + + if cwd then + vim.cmd("cd" ..cwd) + else + cwd = vim.fn.getcwd() + end + + if opts.start_explorer then + vim.cmd.Explore(cwd) + end end - local cwd = vim.fn.getcwd() - start_explorer(M.left_id, cwd) - start_explorer(M.right_id, cwd) + set_cwd(M.left_id, opts.path) + set_cwd(M.right_id, opts.path) end end diff --git a/plugin/sessionmanager.lua b/plugin/sessionmanager.lua index 18504ec..f6f36e2 100644 --- a/plugin/sessionmanager.lua +++ b/plugin/sessionmanager.lua @@ -43,18 +43,13 @@ local function session_picker(opts) -- close all buffers vim.cmd("bufdo bdelete") + -- determine new directory local selection = action_state.get_selected_entry() local entry = selection.value - -- change the current directory - vim.cmd("cd" ..entry.path) - while vim.fn.getcwd() ~= entry.path do - print("waiting...") - end - -- split window local vsplit = require("snippets.vsplit") - vsplit.reset{ start_explorer = true } + vsplit.reset{ path = entry.path, start_explorer = true } end) return true end