Moving lines of code around in Vim
I recently added a handy little set of mappings to my
.vimrc for moving code around.
They’ll take either the line you’re on (in normal mode) or the lines you’ve selected (in visual mode) and move them up or down 1 line.
The nice part is that, as you can see above, when you move lines into or out of parent blocks, their indention level will automatically get updated.
Here are the mappings you’ll need:
1" Move lines around easily2nnoremap <leader>k :m-2<cr>==3nnoremap <leader>j :m+<cr>==4xnoremap <leader>k :m-2<cr>gv=gv5xnoremap <leader>j :m'>+<cr>gv=gv