Last Updated: February 25, 2016
·
672
· av-ast

Vim: Moving Lines Ain't Hard

If you need to move line (or a block of selected lines) all you have to do is to add the following lines to you .vimrc:

" Normal mode
nnoremap <C-j> :m .+1<CR>==
nnoremap <C-k> :m .-2<CR>==
" Insert mode
inoremap <C-j> <ESC>:m .+1<CR>==gi
inoremap <C-k> <ESC>:m .-2<CR>==gi
" Visual mode
vnoremap <C-j> :m '>+1<CR>gv=gv
vnoremap <C-k> :m '<-2<CR>gv=gv

Now, you can move you lines up and down with <Ctrl+k> and <Ctrl+j> respectively.

Many thanks to Doug Yun for this example!

Original article