lazygit.nvim icon indicating copy to clipboard operation
lazygit.nvim copied to clipboard

[Feat] Command: LazygitToggle

Open Dimfred opened this issue 1 year ago • 8 comments

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 avatar Aug 20 '23 13:08 Dimfred

Thanks for sharing your config @Dimfred!

Jasha10 avatar Oct 04 '23 18:10 Jasha10

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'"

teocns avatar Jan 09 '24 08:01 teocns

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

Dimfred avatar Jan 12 '24 09:01 Dimfred

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>'"

khris190 avatar Apr 06 '24 21:04 khris190

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.

dudicoco avatar Jul 09 '24 17:07 dudicoco

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 ?

vroussel avatar Jul 19 '24 11:07 vroussel

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

Dimfred avatar Jul 19 '24 13:07 Dimfred

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

vroussel avatar Jul 19 '24 14:07 vroussel