2

I have these key mappings that I use a lot in nvim, but they don't work in vim:

" Move lines with Alt+Shift+j/k
nnoremap <A-S-j> :m .+1<CR>==                                                                                                                                                               
nnoremap <A-S-k> :m .-2<CR>==                                                                                                                                                               
inoremap <A-S-j> <Esc>:m .+1<CR>==gi                                                                                                                                                        
inoremap <A-S-k> <Esc>:m .-2<CR>==gi                                                                                                                                                        
vnoremap <A-S-j> :m '>+1<CR>gv=gv
vnoremap <A-S-k> :m '<-2<CR>gv=gv

Instead I get the Join Words and Help actions as though the Alt key wasn't being pressed (in Normal Mode).

I have tried using capital J and K instead, but that didn't help.

Does vim not accept key bindings with Alt and Shift together, or is there some option that needs to be enabled or disabled?


MORE INFO: I explicitly set as many options as I can for consistency, as I use the same configuration between nvim, vim and gvim and Linux and Windows, but there are so many that I don't have all of them set.

Currently I am using:

  • Linux x86_64 I use nvim 0.72 (appimage) and vim 8.2 for git

  • Linux aarch64 I am using vim 8.2

  • On Windows I am using vim 9.0 and gvim 9.0

These key mappings are only working on nvim.

13
  • 1
    @VivianDeSmedt I have added more info above. Thanks.
    – paradroid
    Commented Sep 24, 2022 at 20:00
  • 1
    I'll check if I can reproduce your problem and let you know :-) Commented Sep 24, 2022 at 20:25
  • 1
    I have tried successfully to map <A-a> and <A-S-a> to two different key sequence on Windows 10 with gVim 8.1, 8.2 and 9.0. Commented Sep 24, 2022 at 22:12
  • 1
    Maybe could you try with a very slim .vimrc starting Vim with the -U NONE -i NONE options to reduce the impact of previous sessions. Commented Sep 24, 2022 at 22:18
  • 2
    I'm voting to close this as a duplicate. How to map Alt key? doesn't specifically mention the Shift key, but I don't think that changes anything: the answers are the same. (Although it's very weird this doesn't seem to be working for you in gVim: your original mappings should be fine there).
    – Rich
    Commented Oct 13, 2022 at 11:34

1 Answer 1

2

No, you cannot use Alt explicitly in your mappings. Instead of that, you need to press Ctrl+v in insert mode and then press the desired mapping (e.g. alt+shif+j). For your first mapping, it should look as follows:

nnoremap ^[J :m .+1<CR>==

and that should work.

6
  • 1
    Yes. It now works in vim as well as nvim now. Thanks.
    – paradroid
    Commented Sep 24, 2022 at 23:45
  • Actually it doesn't work in vim/gvim for Windows but I can live with that and at least the other behaviour doesn't occur, as that was the main problem due to habit.
    – paradroid
    Commented Sep 25, 2022 at 0:52
  • 1
    @paradroid I believe it depends on whether you're using terminal vim or gvim. In gvim, you need to use <A-S-j>. Not sure if you tested in that way. I don't have a machine with Windows installed, so I cannot verify if there is an additional edge case to take into account.
    – r_31415
    Commented Sep 25, 2022 at 1:47
  • Working on all platforms with if has('unix')...elseif has('win32'). Cheers.
    – paradroid
    Commented Sep 25, 2022 at 2:27
  • That's great :)
    – r_31415
    Commented Sep 25, 2022 at 2:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.