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

alt key bindings no longer work #5576

Closed
glensc opened this issue Nov 9, 2016 · 8 comments
Closed

alt key bindings no longer work #5576

glensc opened this issue Nov 9, 2016 · 8 comments
Labels
closed:question issues that are closed as usage questions

Comments

@glensc
Copy link

glensc commented Nov 9, 2016

" nice bindings for navigating between files
map <Esc>n :bnext<CR>
map <Esc>p :bprevious<CR>
map <Esc>l :ls<CR>
map <Esc>1 :buffer 1<CR>
map <Esc>2 :buffer 2<CR>
  • nvim --version: 0.1.5, 0.1.6
  • Vim (version: all versions) behaves differently: works
  • Operating system/version: Linux, OSX
  • Terminal name/version: gnome-terminal, mate-terminal, screen, iterm2
  • $TERM: xterm, screen

Actual behaviour

Expected behaviour

Steps to reproduce using nvim -u NORC

nvim -u NORC
:map <Esc>l :ls<CR>

Now press alt+l -> nothing happens in nvim

@glensc glensc changed the title alt bindings no longer work alt key bindings no longer work Nov 9, 2016
@justinmk
Copy link
Member

justinmk commented Nov 9, 2016

ESC is ESC. Try <A-foo> instead.

@justinmk justinmk closed this as completed Nov 9, 2016
@glensc
Copy link
Author

glensc commented Nov 9, 2016

why did you close this? it's regression vim->neovim! and alt key sends esc+foo sequence.

is this documented in neovim incompatibilities list?

@justinmk
Copy link
Member

justinmk commented Nov 9, 2016

Just change the mappings to:

map <A-n> :bnext<CR>
map <A-p> :bprevious<CR>
map <A-l> :ls<CR>
map <A-1> :buffer 1<CR>
map <A-2> :buffer 2<CR>

it's regression

It's not a regression, <Esc> means "escape" in the context of a mapping (the fact that a terminal sends that byte for meta/alt isn't relevant). <A-...> means "alt". This is mentioned in :help nvim-features, FWIW.

is this documented in neovim incompatibilities list?

Not even gVim works with the mappings you have. Specifying <Esc>a as a mapping only works by accident in terminal Vim, because of an implementation detail. <M-a> (or <A-a>) is the correct way to specify a meta/alt mapping. And that works everywhere in Neovim (whereas in Vim it only works in gVim). Also, mapping <Esc> at all in Vim will cause subtle bugs (though it seems you didn't notice them yet), I don't recommend it. (Mapping <Esc> in Neovim is perfectly safe.)

@justinmk justinmk added the usage label Nov 9, 2016
@glensc
Copy link
Author

glensc commented Nov 9, 2016

thanks for the detailed reply!

however, what is vim/nvim compatible way? You say <M-a>/<A-a> do not work in vim? and my local test seems to prove that. also what's the preferred way <M-a> or <A-a>?

@glensc
Copy link
Author

glensc commented Jan 20, 2017

@justinmk care to give example how to define the bindings compatible with vim/nvim? perhaps some function?

glensc added a commit to pld-linux/neovim that referenced this issue Jan 20, 2017
@justinmk
Copy link
Member

if has('nvim')
  ...
else
  ...
endif

@glensc
Copy link
Author

glensc commented Oct 14, 2017

is there way to reuse adding definitions? duplicating large blocks between if and else is just bad practice. what i have in mind, is either add function that adds definition either for vim or nvim, and defining bindings go via that function.

@glensc
Copy link
Author

glensc commented Oct 14, 2017

least i was able to come up with.
this could probably improved by vim/nvim best practices (which i'm not aware of):

" vim/nvim compatible alt key binding
" https://github.com/neovim/neovim/issues/5576
if has('nvim')
    " map <A-n> :bnext<CR>
    function! s:alt_key(key)
        return "<A-". a:key . ">"
    endfun
else
    " map <Esc>n :bnext<CR>
    function! s:alt_key(key)
        return "<Esc>". a:key
    endfun
endif

function! s:map_alt(key, action)
    exec "map " . s:alt_key(a:key). " " a:action
endfun

function! s:imap_alt(key, action)
    exec "imap " . s:alt_key(a:key). " " a:action
endfun
-map <Esc>n :bnext<CR>
-map <Esc>p :bprevious<CR>
-map <Esc>l :ls<CR>
-map <Esc>1 :buffer 1<CR>
-map <Esc>2 :buffer 2<CR>
-map <Esc>3 :buffer 3<CR>
-map <Esc>4 :buffer 4<CR>
-map <Esc>5 :buffer 5<CR>
-map <Esc>6 :buffer 6<CR>
-map <Esc>7 :buffer 7<CR>
-map <Esc>8 :buffer 8<CR>
-map <Esc>9 :buffer 9<CR>
-map <Esc>0 :buffer 10<CR>
-map <Esc>u :bunload<CR>
-map <Esc>e :e <C-D>
+call s:map_alt("n", ":bnext<CR>")
+call s:map_alt("p", ":bprevious<CR>")
+call s:map_alt("l", ":ls<CR>")
+call s:map_alt("1", ":buffer 1<CR>")
+call s:map_alt("2", ":buffer 2<CR>")
+call s:map_alt("3", ":buffer 3<CR>")
+call s:map_alt("4", ":buffer 4<CR>")
+call s:map_alt("5", ":buffer 5<CR>")
+call s:map_alt("6", ":buffer 6<CR>")
+call s:map_alt("7", ":buffer 7<CR>")
+call s:map_alt("8", ":buffer 8<CR>")
+call s:map_alt("9", ":buffer 9<CR>")
+call s:map_alt("0", ":buffer 10<CR>")
+call s:map_alt("u", ":bunload<CR>")
+call s:map_alt("e", ":e <C-D>")

-       map <Esc>\ :call CommentToggle()<CR>j
-       imap <Esc>\ <ESC>:call CommentToggle()<CR>j
-       map <Esc>' :call CommentToggle()<CR>j
-       imap <Esc>' <ESC>:call CommentToggle()<CR>j
+       call s:map_alt("\\", ":call CommentToggle()<CR>j")
+       call s:imap_alt("\\", "<ESC>:call CommentToggle()<CR>j")
+       call s:map_alt("'", ":call CommentToggle()<CR>j")
+       call s:imap_alt("'", "<ESC>:call CommentToggle()<CR>j")

wincent added a commit to wincent/wincent that referenced this issue Jun 12, 2019
@justinmk justinmk added the closed:question issues that are closed as usage questions label Aug 30, 2020
@neovim neovim deleted a comment from MartyMcFlyInTheSky Sep 16, 2024
@neovim neovim locked as resolved and limited conversation to collaborators Sep 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
closed:question issues that are closed as usage questions
Projects
None yet
Development

No branches or pull requests

2 participants