Is there a way to change the cwd in nvim when yazi exits?
bertradio opened this issue · comments
I would like to be able to use yazi to change directories in nvim... similar to yy in bashrc. In other words, if in yazi I change the directory to /bin, when I exit yazi the cwd in nvim would change to /bin.
That's a great idea. I think it could be done if I add a way to know what the last directory was when yazi was quit, and if you then add a custom hook.
It sounds good. I actually started adding this feature (last yazi directory) yesterday because I had an idea for another feature.
My idea was to add an optional keybinding for closing yazi and grepping the selected directory using telescope or whatever the user wants to do.
Let's keep this open.
By the way, when you change the cwd in nvim, does it cause issues for your other tools such as LSPs or linters, etc? I typically don't change the cwd in my workflow right now, so I don't know.
I use the same thing in my shell, actually. It's really useful.
How have you found LSP and other tools to work if you change your cwd in neovim?
Ok, I have something that might work for you in the PR autolinked above.
Here's how I set it up in my config:
{
-- "mikavilpas/yazi.nvim",
dir = "~/git/yazi.nvim/",
dependencies = {
"nvim-lua/plenary.nvim",
},
event = "VeryLazy",
keys = {
{
"<up>",
function()
require("yazi").yazi()
end,
desc = "Open the file manager",
},
{
"<s-up>",
function()
require("yazi").yazi(nil, vim.fn.getcwd())
end,
desc = "Open the file manager in the cwd",
},
},
---@type YaziConfig
opts = {
open_for_directories = true,
enable_mouse_support = true,
-- log_level = vim.log.levels.DEBUG,
---@diagnostic disable-next-line: missing-fields
hooks = {
yazi_closed_successfully = function(chosen_file, config, state)
vim.notify(vim.inspect({ chosen_file, state.last_directory.filename }))
end,
},
},
}
Basically, you can use the yazi_closed_successfully
hook. I added a new parameter for information that is known when yazi has closed.
Do you know how to try that out? You can add branch = "cd-events"
to your lazy.nvim spec if you're using that.
Ok, it seems to be working then.
Try this:
hooks = {
yazi_closed_successfully = function(chosen_file, config, state)
if state.last_directory.filename then
vim.notify("Changing directory to " .. state.last_directory.filename)
vim.fn.chdir(state.last_directory.filename)
end
end,
},
You can remove the notifying once you have made sure it works.
Ah, good point! I think this does what you want:
---@diagnostic disable-next-line: missing-fields
hooks = {
yazi_closed_successfully = function(chosen_file, config, state)
if chosen_file == nil and state.last_directory.filename then
vim.notify("Changing directory to " .. state.last_directory.filename)
vim.fn.chdir(state.last_directory.filename)
end
end,
},
I am thinking this could be a built in feature in yazi.nvim, but I'm not sure how I could make it happen just yet. If I come up with a way, I'll notify you via this issue, but for now that should work!
Those are good suggestions, thanks. I'd like to keep this open for now. They can probably be implemented, but so many things are open about the near future in terms of possibilities that I don't know what to do yet.