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

[BUG] Remapping escape to <C-\><C-n> doesn't work #559

Open
1 task done
promitheas17j opened this issue Apr 5, 2024 · 6 comments
Open
1 task done

[BUG] Remapping escape to <C-\><C-n> doesn't work #559

promitheas17j opened this issue Apr 5, 2024 · 6 comments

Comments

@promitheas17j
Copy link

promitheas17j commented Apr 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

When i open a horizontal terminal (for example) and press my escape button it does not exit terminal mode.

Expected Behavior

When pressing the escape key to exit terminal mode and enter normal mode.

Steps To Reproduce

  1. In an alacritty terminal, running neovim version 0.9.5
  2. Here is my config:
local status_ok, toggleterm = pcall(require, "toggleterm")
if not status_ok then
	return
end

toggleterm.setup({
	size = 20,
	open_mapping = [[<C-\>]],
	hide_numbers = true,
	shade_filetypes = {},
	shade_terminals = true,
	shading_factor = 2,
	start_in_insert = true,
	insert_mappings = false,
	persist_size = true,
	direction = "horizontal",
	close_on_exit = false,
	terminal_mappings = false,
	shell = "/bin/zsh",
	float_opts = {
		border = "curved",
		winblend = 0,
		highlights = {
			border = "Normal",
			background = "Normal",
		},
		title_pos = "center",
	},
})

function _G.set_terminal_keymaps()
	local opts = {noremap = true}
	vim.api.nvim_buf_set_keymap(0, 't', '<esc>', [[<C-\><C-n>]], opts)
	vim.api.nvim_buf_set_keymap(0, 't', '<C-h>', [[<C-\><C-n><C-W>h]], opts)
	vim.api.nvim_buf_set_keymap(0, 't', '<C-j>', [[<C-\><C-n><C-W>j]], opts)
	vim.api.nvim_buf_set_keymap(0, 't', '<C-k>', [[<C-\><C-n><C-W>k]], opts)
	vim.api.nvim_buf_set_keymap(0, 't', '<C-l>', [[<C-\><C-n><C-W>l]], opts)
end
  1. Open a terminal with <C-\>, press the escape key to exit terminal mode and nothing happens
  2. No error is shown

Note: I have swapped the mapping of escape key with Capslock key at the OS level.

Environment

- OS: Arch Linux
- neovim version: 0.9.5
- Shell: zsh
- terminal emulator: alacritty
- Escape key and Capslock keys swapped at OS level with following commands ran at login: 

setxkbmap -option caps:swapescape

Anything else?

I have looked at this closed issue (#93) as well as this one (#77) and while I understand the concept and reasoning behind why we need a mapping to get back into normal mode if we want to open multiple terminals, I'm starting to not be sure that I mapped it correctly. Here is the specific line in my config for getting out of terminal mode:
vim.api.nvim_buf_set_keymap(0, 't', '<esc>', [[<C-\><C-n>]], opts)
Is this because I have swapped my escape key with the capslock? If so, how can I get the same functionality?
I have also tried the following two variations of the line above with the same result
vim.api.nvim_buf_set_keymap(0, 't', 'jk', [[<C-\><C-n>]], opts)
vim.api.nvim_buf_set_keymap(0, 't', '<leader>jk', [[<C-\><C-n>]], opts)

@bkolligs
Copy link

I have a similar setup (Alacritty, Ubuntu 22) and I had this issue. I found that the chord <C-> doesn't work if I have the terminal open already, and the first <C-> just closes the terminal. However if I press shift the chord works. So the keymap is actually <C-S-> and to get into normal mode.

@promitheas17j
Copy link
Author

Could you please clarify how you made it work in your setup? If you could paste your mapping line here it would really help me :)

Thanks.

@bkolligs
Copy link

bkolligs commented Apr 21, 2024

The only mapping I have is

require("toggleterm").setup({
    open_mapping = [[<c-\>]],
})

and using shift just seems to work.

@promitheas17j
Copy link
Author

I think I understand what you mean. If I press Ctrl+Shift followed by Ctrl+n it does exit terminal mode, but my problem is that it does not work if I map it to escape, probably because I swapped my caps lock and escape keys. I want to just press the escape key and have it execute the necessary stuff to exit terminal mode.

@bkolligs
Copy link

bkolligs commented Apr 21, 2024

Ah yeah unfortunately I think the terminal captures the escape so it can’t be remapped.

I should’ve clarified I was unable to get the escape mapping to work. I also have my caps lock and escape key swapped, and was also unable to set this map so I have had to retrain myself to use the ctrl shift \.

@programandoconro
Copy link

I am not sure if this is 100% related, but in my case I remap it to jk using:

map("t", "jk", "<C-\\><C-n>") -- exit terminal mode

And it does the job done of exiting terminal mode.

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

No branches or pull requests

3 participants