Trick to shift lines in Vim

It’s easy to select lines in visual mode, delete them, then paste them a few lines lower. However, this won’t adjust the indentation to match surrounding code. Instead, add the following to your vimrc (or init.vim): " Move visual selectionvnoremap K :m '<-2<cr>gv=gvvnoremap J :m '>+1<cr>gv=gvThen select lines using visual mode (v or V) and press J or K to shift them down or up one line, respectively. This will also adjust the indentation properly (the same as pressing = while selecting the lines)....

May 21, 2021 · 1 min · Kian Kasad