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

`Esc` closes the floating buffer instead of interacting with LazyGit and cancel the operation

Open ulyssessouza opened this issue 1 year ago • 12 comments

Description

To Reproduce

Here are the steps to reproduce the behavior using MINRC: Ps: Couldn't run with MINRC...

  1. Press Esc, lg to open LazyGit in a floating window/buffer
  2. Tab to browse to Local Branches
  3. Enter to select a branch to see the commits
  4. Press Esc to go back (Cancel)

Expected behavior

What is expected to happen: When pressing Esc that should make the Local Branches section to go back.

But what is happening is that the floating window just closes. This avoids me to cherrypick a commit from a branch and paste it to another

Screenshots

Desktop (please complete the following information):

  • nvim --version:
v0.9.5
  • alacritty --version
alacritty 0.12.2

ulyssessouza avatar Apr 12 '24 08:04 ulyssessouza

Having the same issue. Escape should not close the floating window. Only q should.

ckt114 avatar Apr 25 '24 22:04 ckt114

Sovled this issue by adding this key binding.

vim.keymap.del("t", "<esc>")

ckt114 avatar Apr 25 '24 23:04 ckt114

I tried something like this, but it doesn't pass escape down to lazygit

map("t", "<ESC>", function()
	local win = vim.api.nvim_get_current_win()
	-- Do not close lazygit on escape
	if not string.find(vim.api.nvim_buf_get_name(0), "lazygit") then
		vim.api.nvim_win_close(win, true)
	end
end, { desc = "Terminal Close term in terminal mode" })

does anyone know how to pass the escape key to lazygit in this case?

amritk avatar May 14 '24 16:05 amritk

I ended up doing this FWIW: https://github.com/SimonBrandner/dotfiles/commit/a29de2c408d6157760bbfe815a8b49bcb7f8f230

SimonBrandner avatar Jun 25 '24 08:06 SimonBrandner

Thanks for posting the examples. I learned a lot reading them and then writing my own autocommand to prevent the escape key from bringing me out of the terminal mode. I was able to get it, with some help from ai, to send esc to lazygit, allowing me to use esc like i normally would outside of neovim

collinvandyck avatar Jul 02 '24 01:07 collinvandyck

I'd be open to adding such an autocmd to the floating window terminal buffer in this plugin, enabled by default but with an option to not add the autocmd. Thoughts?

kdheepak avatar Jul 02 '24 07:07 kdheepak

@kdheepak i think that would be helpful for newbies like me that might not know how to go about adding the workaround. thank you 👍

collinvandyck avatar Jul 02 '24 13:07 collinvandyck

I have the same issue. I use Neovim and Lazygit. Any ideas on how to fix it?

Update: I found the root cause of the problem. In this https://github.com/kdheepak/lazygit.nvim/issues/78#issuecomment-1416894837, I learned that a potential key binding might prevent the Esc key from working as expected. In my case, I had:

vim.api.nvim_set_keymap("t", "<esc><esc>", "<c-\\><c-n>", { desc = "Enter Normal Mode" })

After removing this binding, all Lazygit bindings started working as expected.

enheit avatar Jul 04 '24 22:07 enheit

I have this issue too, that I need this for regular terminal use, so that I can hit Esc and then use the terminal as a buffer for e.g. yanking:

vim.api.nvim_set_keymap("t", "<esc><esc>", "<c-\\><c-n>", { desc = "Enter Normal Mode" })

But when opening up lazygit, this setting is now undesirable as when I'm staging hunks I can't hit Esc to exit to the main view. I instead have to hit q to quit lazygit and relaunch it.

I wonder if it's plausible to delete the keymap when running lazygit and re-creating it when opening the terminal... via autocmd? Or does anyone have a better suggestion?

fredrikaverpil avatar Jul 14 '24 08:07 fredrikaverpil

I ended up solving it like this:

function M.setup_terminal_keymaps()
  -- Both <C-/> and <C-_> are mapped due to the way control characters are interpreted by terminal emulators.
  -- ASCII value of '/' is 47, and of '_' is 95. When <C-/> is pressed, the terminal sends (47 - 64) which wraps around to 111 ('o').
  -- When <C-_> is pressed, the terminal sends (95 - 64) which is 31. Hence, both key combinations need to be mapped.

  -- <C-/> toggles the floating terminal
  local ctrl_slash = "<C-/>"
  local ctrl_underscore = "<C-_>"
  local ctrl_alt_slash = "<C-A-/>"
  local ctrl_alt_underscore = "<C-A-_>"
  local floating_term_cmd = function()
    vim.api.nvim_set_keymap("t", "<Esc><Esc>", "<C-\\><C-n>", { noremap = true })
    require("utils.terminal").toggle_fterm()
  end
  local split_term_cmd = function()
    vim.api.nvim_set_keymap("t", "<Esc><Esc>", "<C-\\><C-n>", { noremap = true })
    require("utils.terminal").toggle_terminal_native()
  end
  vim.keymap.set({ "n", "i", "t", "v" }, ctrl_alt_slash, split_term_cmd, { desc = "Toggle terminal" })
  vim.keymap.set({ "n", "i", "t", "v" }, ctrl_alt_underscore, split_term_cmd, { desc = "Toggle terminal" })

  -- C-A-/ toggles split terminal on/off
  vim.keymap.set({ "n", "i", "t", "v" }, ctrl_slash, floating_term_cmd, { desc = "Toggle native terminal" })
  vim.keymap.set({ "n", "i", "t", "v" }, ctrl_underscore, floating_term_cmd, { desc = "Toggle native terminal" })
end


function M.setup_lazygit_keymaps()
  map_normal_mode("<leader>gg", function()
    -- if keymap <Esc><Esc> is set in terminal mode, remove it.
    -- this is to enable <Esc> to navigate in LazyGit which otherwise
    -- is overridden for terminal usage.
    local terminal_keymaps = vim.api.nvim_get_keymap("t")
    vim.notify(vim.inspect(terminal_keymaps))
    for _, keymap in pairs(terminal_keymaps) do
      if keymap.lhs == "<Esc><Esc>" then
        vim.api.nvim_del_keymap("t", "<Esc><Esc>")
      end
    end

    vim.cmd("LazyGit")
  end)
end

Now, I get the benefit of being able to hit Esc in the terminal but also use Esc while in the lazygit UI.

fredrikaverpil avatar Jul 14 '24 14:07 fredrikaverpil

@ulyssessouza Not sure if it helps, but creating an ftplugin file for lazygit fixed the issue for me without major complicated changes or changes in the original keybinding.

-- after/ftplugin/lazygit.lua

-- This maps the esc key to its normal behavior only for lazygit terminal window.
vim.api.nvim_buf_set_keymap(0, 't', '<Esc>', '<Esc>', {noremap = true, silent = true}) 

This exact line is in the telescope/_extensions/lazygit.lua. file from this repo. :grin:

Hope it helps!

Diogo364 avatar Oct 02 '24 13:10 Diogo364