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

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

Open promitheas17j opened this issue 1 year ago • 6 comments

Is there an existing issue for this?

  • [X] 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)

promitheas17j avatar Apr 05 '24 22:04 promitheas17j

I have a similar setup (Alacritty, Ubuntu 22) and I had this issue. I found that the chord <C-> <C-n> 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 <C-n> to get into normal mode.

bkolligs avatar Apr 20 '24 17:04 bkolligs

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.

promitheas17j avatar Apr 20 '24 17:04 promitheas17j

The only mapping I have is

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

and using shift just seems to work.

bkolligs avatar Apr 21 '24 01:04 bkolligs

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.

promitheas17j avatar Apr 21 '24 02:04 promitheas17j

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 \.

bkolligs avatar Apr 21 '24 04:04 bkolligs

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.

programandoconro avatar May 05 '24 23:05 programandoconro