In VSCode I had a keybinding I really liked. Basically you could use Alt+Shift+Up/Down arrow to copy the selected block of code above or below the active selection. Now that I switched to Vim I thought certainly it's possible to remap it so it behaves exactly the same. The behaviour in VSCode is the following:
- Look at all the lines that are touched by the selection. Let's call this the block.
- Copy the block above/below the block, selection and cursor position will also go to the newly inserted block.
I want to map the behviour to Shift+Alt+j or Shift+Alt+k. I figured I could use the :t command, and this is how far I got:
" Copy the current line up
nnoremap <ESC>K :t .-1<CR>==
vnoremap <ESC>K :t '<-1<CR>gv
" Copy the current lines down
nnoremap <ESC>J :t .<CR>==
vnoremap <ESC>J :t '>+1<CR>gv
However this solution has some problems. First of all, the cursor position is always reset. Then, the selection stays on the old block instead of jumping to the new one. Also there's this weird behaviour of undo (u
) that if executed after copying a block of lines up/down in visual mode it will first convert to lowercase, then uppercase, and only then remove the block (undo the copy), so I have to press u
three times. This really seems a little buggy.
How to do these keybindings correctly?