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

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

Open
ulyssessouza opened this issue Apr 12, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@ulyssessouza
Copy link

ulyssessouza commented Apr 12, 2024

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 ulyssessouza added the bug Something isn't working label Apr 12, 2024
@ckt114
Copy link

ckt114 commented Apr 25, 2024

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

@ckt114
Copy link

ckt114 commented Apr 25, 2024

Sovled this issue by adding this key binding.

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

@amritk
Copy link

amritk commented May 14, 2024

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?

@SimonBrandner
Copy link

I ended up doing this FWIW: SimonBrandner/dotfiles@a29de2c

@collinvandyck
Copy link

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

@kdheepak
Copy link
Owner

kdheepak commented Jul 2, 2024

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?

@collinvandyck
Copy link

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

@enheit
Copy link

enheit commented Jul 4, 2024

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 #78 (comment), 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.

@fredrikaverpil
Copy link

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
Copy link

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.

@Diogo364
Copy link

Diogo364 commented Oct 2, 2024

@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. 😁

Hope it helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants