Skip to content
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

Switching to selected worktree directory #137

Open
voylin opened this issue Aug 10, 2024 · 2 comments
Open

Switching to selected worktree directory #137

voylin opened this issue Aug 10, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@voylin
Copy link

voylin commented Aug 10, 2024

Is your feature request related to a problem? Please describe.

I recently started working with worktrees but when opening LazyVim inside of nvim and go to the worktree section, there is no way to change the current nvim work directory to the one selected. You can press o to open the editor, but then you have a vim instance inside of the lazygit window.

Describe the solution you'd like

When switching to a worktree, it would be nice if the nvim instance would switch to the worktree folder. I do not know in how far this would be possible though and it's not a big issue, it could just save time being able to do it directly inside of lazygit.

@voylin voylin added the enhancement New feature or request label Aug 10, 2024
@zyriab
Copy link

zyriab commented Oct 30, 2024

Would love this feature!
For reference, here's how you can do this with the standalone tool.
It works when selecting a repo as well as when changing worktrees.

https://github.com/jesseduffield/lazygit?tab=readme-ov-file#changing-directory-on-exit

@kryshac
Copy link

kryshac commented Nov 12, 2024

I created this temporary patch

diff --git a/lua/lazygit.lua b/lua/lazygit.lua
index 770e042..3a1760c 100644
--- a/lua/lazygit.lua
+++ b/lua/lazygit.lua
@@ -37,2 +37,19 @@ local function on_exit(job_id, code, event)
   end
+
+  local dir_file = os.getenv("LAZYGIT_NEW_DIR_FILE")
+
+  if dir_file then
+    local file = io.open(dir_file, "r")
+
+    if file then
+      local content = file:read("*all")
+      file:close()
+
+      vim.fn.chdir(content)
+    else
+      print("Failed to open file: " .. file)
+    end
+
+    vim.env.LAZYGIT_NEW_DIR_FILE = nil
+  end
 end
@@ -41,2 +58,4 @@ end
 local function exec_lazygit_command(cmd)
+  vim.env.LAZYGIT_NEW_DIR_FILE = vim.fn.expand("~/.lazygit/newdir")
+
   if LAZYGIT_LOADED == false then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants