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!
Written by Alexey Astafyev
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Vim
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#