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

[Feat] Command: LazygitToggle #106

Open
Dimfred opened this issue Aug 20, 2023 · 8 comments
Open

[Feat] Command: LazygitToggle #106

Dimfred opened this issue Aug 20, 2023 · 8 comments
Labels
enhancement New feature or request

Comments

@Dimfred
Copy link

Dimfred commented Aug 20, 2023

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

In my lazygit config I am using:

os:
  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<C-space>:q<CR><Esc>:lua vim.cmd(\"e \" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<C-space>:q<CR><Esc>:lua vim.cmd(\"e \" .. {{filename}})<CR>{{line}}G'"

This allows me to open the file from lazygit inside my main window and not in lazygit. As you can see for that before opening the file, I am closing lazygit with q, what would be nice if I could just toggle then window away. Then way I could resume lazygit at the same spot I closed it.

Describe the solution you'd like

A LazygitToggle command, which allows to toggle the Lazygit window, so an extension of the currrent command.

That would be awesome :) Thx for the plugin btw, appreciate it.

EDIT:

Meanwhile I found how to better make use of <CMD> so this is the updated version, I'll leave the old one for reference. Then you also won't get annoying cmd popups n stuff.

os:
  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CMD>q<CR><CMD>lua vim.cmd(\"e \" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<CMD>q<CR><CMD>lua vim.cmd(\"e \" .. {{filename}})<CR>{{line}}G'"
@Dimfred Dimfred added the enhancement New feature or request label Aug 20, 2023
@Jasha10
Copy link

Jasha10 commented Oct 4, 2023

Thanks for sharing your config @Dimfred!

@teocns
Copy link

teocns commented Jan 9, 2024

That won't resolve path well if you find yourself in a sub-directory, away from the .git root, and try editing a file; how about...

  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>'"
  editAtLine: "nvr --remote-send '<CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>{{line}}G'"

@Dimfred
Copy link
Author

Dimfred commented Jan 12, 2024

@teocns man this is awesome, very nice idea, didn't now how to do that properly, thanks a lot!

@khris190
Copy link

khris190 commented Apr 6, 2024

for some reason i get shown a window with my command and press ENTER to go back to lazygit and adding <CR> at the start of the command fixes it if anyone has the same problem.

  editPreset: 'nvim-remote'
  edit: "nvr --remote-send '<CR><CMD>q<CR><CMD>lua local root = vim.fn.trim(vim.fn.system(\"git rev-parse --show-toplevel\")); vim.cmd(\"e \" .. root .. \"/\" .. {{filename}})<CR>'"

@dudicoco
Copy link

dudicoco commented Jul 9, 2024

lazygit now has a native integration with nvim remote: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#configuring-file-editing

In my case I didn't even have to open nvim with the --listen flag, it just worked out of the box.

@vRoussel
Copy link

Native integration does indeed work great!

However (correct me if I'm wrong), there's still no way to "resume lazygit at the same spot" as @Dimfred was asking, right ?

@Dimfred
Copy link
Author

Dimfred commented Jul 19, 2024

So I wrote my own "lazygit" function, which allows me to do that.

local term = nil

M.toggle = function()
    local Terminal = require("toggleterm.terminal").Terminal

    local size = 90
    local direction = "float"

    if not term then
        term = Terminal:new({
            cmd = "lazygit",
            hidden = true,
            count = 10,
            on_exit = function()
                term = nil
            end,
        })
        term:toggle(size, direction)

        vim.cmd("set ft=lazygit")
        map("nvit", "<C-w>", function()
            term:toggle(size, direction)
        end, { buffer = true })
    else
        term:toggle(size, direction)
    end
end

@vRoussel
Copy link

vRoussel commented Jul 19, 2024

Nice, thanks ! I'll try that

Edit: It works pretty well :)
Here's my adapted version so that when you edit a file with e from lazygit, it doesn't get closed but hidden.

Don't forget to change my keymaps if needed:
<leader>gg to open
<a-q> to hide

Somewhere in nvim conf:

local term = nil

local function lg_toggle()
	local Terminal = require("toggleterm.terminal").Terminal

	local size = 90
	local direction = "float"

	if not term then
		term = Terminal:new({
			cmd = "lazygit",
			hidden = true,
			on_exit = function()
				term = nil
			end,
		})
		if term then
			term:toggle(size, direction)

			vim.cmd("set ft=lazygit")
			vim.keymap.set("t", "<a-q>", function()
				term:toggle(size, direction)
			end, { buffer = true })
		end
	else
		term:toggle(size, direction)
	end
end

vim.api.nvim_create_user_command("LazyGitToggle", lg_toggle, {})
vim.keymap.set("n", "<leader>gg", "<cmd>LazyGitToggle<cr>", {})

lazygit conf:

os:
  editPreset: 'nvim-remote'
  edit: '[ -z "$NVIM" ] && (nvim -- {{filename}}) || (nvim --server "$NVIM" --remote-send "<CMD>LazyGitToggle<CR>" && nvim --server "$NVIM" --remote-tab {{filename}})'
  editAtLine: '[ -z "$NVIM" ] && (nvim +{{line}} -- {{filename}}) || (nvim --server "$NVIM" --remote-send "<CMD>LazyGitToggle<CR>" &&  nvim --server "$NVIM" --remote-tab {{filename}} && nvim --server "$NVIM" --remote-send ":{{line}}<CR>")'

promptToReturnFromSubprocess: false

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

6 participants