[Help] How do i make the CWD change when I load a session via the telescope picker? #158
Answered
by
Ajaymamtora
Ajaymamtora
asked this question in
Q&A
-
This is my config: local opts = {
autostart = true, -- Automatically start the plugin on load?
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
silent = true, -- silent nvim message when sourcing session file
-- use_git_branch = false, -- create session files based on the branch of a git enabled repository
-- default_branch = "main", -- the branch to load if a session file is not found for the current branch
autosave = true, -- automatically save session files when exiting Neovim
-- should_autosave = function()
-- return is_only_neotree_and_empty_buffer()
-- end, -- function to determine if a session should be autosaved
-- should_save = function()
-- return true
-- end,
autoload = true,
-- on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
on_autoload_no_session = function()
vim.notify("No existing session to load.")
end,
follow_cwd = true, -- change session file name to match current working directory if it changes
-- allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
-- ignored_dirs = nil,
-- ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
telescope = {
mappings = { -- table of mappings for the Telescope extension
copy_session = "<C-c>",
change_branch = "<C-b>",
delete_session = "<C-d>",
},
icons = { -- icons displayed in the picker, set to nil to disable entirely
branch = icons.common.git .. "",
dir = icons.common.folder_open .. "",
selected = icons.common.trace .. "",
},
},
}
And when i load a session, the CWD stays the same. I assume follow_cwd is used for this? but it doesnt seem to work for me |
Beta Was this translation helpful? Give feedback.
Answered by
Ajaymamtora
Sep 14, 2024
Replies: 2 comments 3 replies
-
This seems to work but idk if the path decode function is fully correct local function decode_session_path(session_file)
local encoded_path = session_file:gsub("^" .. vim.pesc(save_dir), ""):gsub("%.vim$", "")
local decoded_path = encoded_path:gsub("%%", "/")
return "/" .. decoded_path -- Add leading slash
end
local function load_decoded_session()
local session_file = vim.v.this_session
if session_file and session_file ~= "" then
print("SESSION POST:", session_file)
print("OTHER POST", vim.g.persisted_loaded_session)
local decoded_path = decode_session_path(session_file)
print("Decoded session path: " .. decoded_path)
print("Attempting to change directory to: " .. decoded_path)
vim.cmd("cd " .. vim.fn.fnameescape(decoded_path))
print("After forced cd - Current working directory: " .. vim.fn.getcwd())
else
print("No valid session file found")
end
end
vim.api.nvim_create_autocmd({ "User" }, {
pattern = "PersistedTelescopeLoadPost",
group = group,
callback = function()
vim.defer_fn(function()
load_decoded_session()
end, 200)
end,
})
|
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Ajaymamtora
-
No idea. Is there anything in the session file that stores the cwd? Otherwise, I guess you'd need to traverse the folder structure until you find a |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This seems to work but idk if the path decode function is fully correct