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.

paradroid
  • 367
  • 2
  • 10
  • Depending on the version of Vim your are using there are key combination that are mapped to the same code. Ctrl-Shift-s and Ctrl-s is a standard example for Vim 7.3. Both Vim and Neovim are working on that. What version of Vim are you using, – Vivian De Smedt Sep 24 '22 at 10:48
  • 1
    @VivianDeSmedt I have added more info above. Thanks. – paradroid Sep 24 '22 at 20:00
  • 1
    I'll check if I can reproduce your problem and let you know :-) – Vivian De Smedt Sep 24 '22 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. – Vivian De Smedt Sep 24 '22 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. – Vivian De Smedt Sep 24 '22 at 22:18
  • @VivianDeSmedt I deleted everything from .vimrc apart from those key mappings and the problem remained. – paradroid Sep 24 '22 at 22:53
  • I also just had the original version from this page in my .vimrc with nothing else and it still didn't work. Very strange. https://vim.fandom.com/wiki/Moving_lines_up_or_down#Mappings_to_move_lines – paradroid Sep 24 '22 at 23:02
  • 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 Oct 13 '22 at 11:34
  • @Rich I couldn't find that answer when asking the question. I personally think that keeping it open would help people find the solution if they are have the same query. – paradroid Oct 14 '22 at 10:53
  • @Rich They are working in gvim as well, using a conditional statement for both variants. – paradroid Oct 14 '22 at 10:54
  • 1
    @paradroid Closing the question doesn’t remove it: it just adds a prominent link to the older question. i.e. It should help future readers find the existing answers. – Rich Oct 14 '22 at 12:18
  • @paradroid Not 100% sure I’m understanding you right, but you shouldn’t need a conditional statement to get your mappings working in gVim: they should work as is. – Rich Oct 14 '22 at 12:22
  • @Rich In my comments below. I use the same config for both Windows and Linux (git repo), so I needed to use conditional statements to use both ways of setting the mappings so that they work on all versions. I closed the question. – paradroid Oct 14 '22 at 20:22

1 Answers1

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.

r_31415
  • 576
  • 4
  • 13
  • 1
    Yes. It now works in vim as well as nvim now. Thanks. – paradroid Sep 24 '22 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 Sep 25 '22 at 00: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 Sep 25 '22 at 01:47
  • Working on all platforms with if has('unix')...elseif has('win32'). Cheers. – paradroid Sep 25 '22 at 02:27
  • That's great :) – r_31415 Sep 25 '22 at 02:47
  • I have another related question here: https://vi.stackexchange.com/q/38848/37532 – paradroid Oct 12 '22 at 01:25