Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to change the cwd in nvim when yazi exits? #83

Closed
bertradio opened this issue May 22, 2024 · 16 comments
Closed

Is there a way to change the cwd in nvim when yazi exits? #83

bertradio opened this issue May 22, 2024 · 16 comments

Comments

@bertradio
Copy link

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.

@mikavilpas
Copy link
Owner

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.

@mikavilpas
Copy link
Owner

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.

@bertradio
Copy link
Author

bertradio commented May 22, 2024 via email

@mikavilpas
Copy link
Owner

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?

@bertradio
Copy link
Author

bertradio commented May 22, 2024 via email

@mikavilpas
Copy link
Owner

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.

@bertradio
Copy link
Author

bertradio commented May 22, 2024 via email

@bertradio
Copy link
Author

bertradio commented May 22, 2024 via email

@mikavilpas
Copy link
Owner

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.

@bertradio
Copy link
Author

bertradio commented May 23, 2024 via email

@bertradio
Copy link
Author

bertradio commented May 23, 2024 via email

@mikavilpas
Copy link
Owner

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!

@bertradio
Copy link
Author

bertradio commented May 23, 2024 via email

@bertradio
Copy link
Author

bertradio commented May 23, 2024 via email

@mikavilpas
Copy link
Owner

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.

@bertradio
Copy link
Author

bertradio commented May 24, 2024 via email

Repository owner locked and limited conversation to collaborators May 26, 2024
@mikavilpas mikavilpas converted this issue into discussion #95 May 26, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants