mikavilpas / yazi.nvim

A Neovim Plugin for the yazi terminal file manager
MIT License
217 stars 7 forks source link

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

Closed bertradio closed 1 month ago

bertradio commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

I do move around a lot in nvim and it's often convenient to change the cwd.

The yazi site has this code to create a function in .bashrc to do exactly that:

function yy() { local tmp tmp="$(mktemp -t "yazi-cwd.XXXXXX")" yazi "$@" --cwd-file="$tmp" if cwd="$(cat -- "$tmp")" && [ -n "$cwd" ] && [ "$cwd" != "$PWD" ]; then cd -- "$cwd" || exit fi rm -f -- "$tmp" }

On Wed, May 22, 2024, at 12:34, Mika Vilpas wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2124465166, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRL5TIPBZACMY2YMOVTZDRYFFAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRUGQ3DKMJWGY. You are receiving this because you authored the thread.Message ID: @.***>

mikavilpas commented 1 month ago

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 commented 1 month ago

I've had no problems at all. i'm not a programmer, but I use nvim for all my text editing, especially markdown. Accessing yazi from within nvim has been a big help to me.

On Wed, May 22, 2024, at 14:43, Mika Vilpas wrote:

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?

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2124699574, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRNJMK2KJJMIT4KH4JDZDSHHRAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRUGY4TSNJXGQ. You are receiving this because you authored the thread.Message ID: @.***>

mikavilpas commented 1 month ago

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 commented 1 month ago

Attached is my yazi config. When I enter yazi and change directory then exit it does not change the cwd. Instead I get this notification. .{ [2] = "/home/bert/.config/nvim" } On Wed, May 22, 2024, at 20:24, Mika Vilpas wrote:

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 = { { "", function() require("yazi").yazi() end, desc = "Open the file manager", }, { "", function() require("yazi").yazi(nil, vim.fn.getcwd()) end, desc = "Open the file manager in the cwd", }, }, @. YaziConfig opts = { open_for_directories = true, enable_mouse_support = true, -- log_level = vim.log.levels.DEBUG, @. 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 Cto your lazy.nvim spec if you're using that.

—m Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2125476467, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRJCL6S4I3LELMZW5O3ZDTPHVAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRVGQ3TMNBWG4. You are receiving this because you authored the thread.Message ID: @.***>

bertradio commented 1 month ago

The directory in the notification is the directory to which I have changed in yazi.

On Wed, May 22, 2024, at 21:59, Bert wrote:

Attached is my yazi config. When I enter yazi and change directory then exit it does not change the cwd. Instead I get this notification. .{ [2] = "/home/bert/.config/nvim" } On Wed, May 22, 2024, at 20:24, Mika Vilpas wrote:

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 = { { "", function() require("yazi").yazi() end, desc = "Open the file manager", }, { "", function() require("yazi").yazi(nil, vim.fn.getcwd()) end, desc = "Open the file manager in the cwd", }, }, @. YaziConfig opts = { open_for_directories = true, enable_mouse_support = true, -- log_level = vim.log.levels.DEBUG, @. 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 Cto your lazy.nvim spec if you're using that.

—m Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2125476467, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRJCL6S4I3LELMZW5O3ZDTPHVAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRVGQ3TMNBWG4. You are receiving this because you authored the thread.Message ID: @.***>

Attachments: • yazi.lua

mikavilpas commented 1 month ago

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 commented 1 month ago

Works now. Cool!

Only issue is that it changes cwd if I open a file from yazi. Not sure if this is what I want. I do want it to change directory if I exit yazi without picking a file. What behavior do you think is best?

On Thu, May 23, 2024, at 07:46, Mika Vilpas wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2126281678, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYROJTXSXQRGMECT7FHTZDV7BRAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRWGI4DCNRXHA. You are receiving this because you authored the thread.Message ID: @.***>

bertradio commented 1 month ago

I've played with this awhile and I think the behavior I would like is to change CWD if I exit yazi without opening a file. If I open a file and yazi exits, I would not want to change the cwd. Does this make sense? Is it an easy change?

On Thu, May 23, 2024, at 08:33, Bert wrote:

Works now. Cool!

Only issue is that it changes cwd if I open a file from yazi. Not sure if this is what I want. I do want it to change directory if I exit yazi without picking a file. What behavior do you think is best?

On Thu, May 23, 2024, at 07:46, Mika Vilpas wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2126281678, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYROJTXSXQRGMECT7FHTZDV7BRAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRWGI4DCNRXHA. You are receiving this because you authored the thread.Message ID: @.***>

mikavilpas commented 1 month ago

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 commented 1 month ago

Perfect!

How about a setting on opt for this with these options for no cwd change, cwd change always, cwd change except on file open?

On Thu, May 23, 2024, at 08:50, Mika Vilpas wrote:

Ah, good point! I think this does what you want:

  ***@***.*** 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!

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2126357665, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRK3N6PYGUDX4S5VAGDZDWGTZAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRWGM2TONRWGU. You are receiving this because you authored the thread.Message ID: @.***>

bertradio commented 1 month ago

Another idea, pass the options when yazi is called. That way one could have different keymaps for different options.

On Thu, May 23, 2024, at 09:17, Bert wrote:

Perfect!

How about a setting on opt for this with these options for no cwd change, cwd change always, cwd change except on file open?

On Thu, May 23, 2024, at 08:50, Mika Vilpas wrote:

Ah, good point! I think this does what you want:

  ***@***.*** 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!

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2126357665, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRK3N6PYGUDX4S5VAGDZDWGTZAVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRWGM2TONRWGU. You are receiving this because you authored the thread.Message ID: @.***>

mikavilpas commented 1 month ago

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 commented 1 month ago

Thank you so much for what you've already done. Works great for me. As for the rest, not critical... maybe some day/

On Fri, May 24, 2024, at 17:18, Mika Vilpas wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/mikavilpas/yazi.nvim/issues/83#issuecomment-2129798272, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALLYYRLQLNBPKKE3IWXVZ33ZD5K27AVCNFSM6AAAAABIDDQX36VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRZG44TQMRXGI. You are receiving this because you authored the thread.Message ID: @.***>