I'm trying to create maps for moving the selection up or down. Moving all selected lines 1 line up is easy:
vnoremap <silent> <M-k> :m--<CR>gv
But moving the lines downwards isn't as easy. The move command requires the same amount of "+" as the amount of lines moved. If I have 10 lines selected, I have to write :m++++++++++
to move them down once.
Alternatively I could write :m line("'>")+1
or in other words, "move selection to the line below the last line in the selection". Unfortunately it just results in "E14: Invalid address" for some reason.
I can't use :exec ...
to build a command because I just get "E481: No range allowed" because Vim automatically inserts :'<,'>
when I hit colon and I don't know how to prevent that.
Any ideas? I just want a map to move the selected lines down once.
Edit: Thanks to the :<C-u>
trick in the accepted answer, I now have these 4 key binds that appear to work perfectly:
" Move lines
nnoremap <silent> <M-j> :m+<CR>
nnoremap <silent> <M-k> :m--<CR>
vnoremap <silent> <M-j> :<C-u>exec "'<,'>m " . (line("'>") + 1)<CR>gv
vnoremap <silent> <M-k> :m--<CR>gv