Vim Tips Wiki
Advertisement
Tip 646 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0


The following mappings in your vimrc provide a quick way to move lines of text up or down. The mappings work in normal, insert and visual modes, allowing you to move the current line, or a selected block of lines.

nnoremap <A-j> :m+<CR>==
nnoremap <A-k> :m-2<CR>==
inoremap <A-j> <Esc>:m+<CR>==gi
inoremap <A-k> <Esc>:m-2<CR>==gi
vnoremap <A-j> :m'>+<CR>gv=gv
vnoremap <A-k> :m-2<CR>gv=gv

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up.

After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.

Explanation

The command :m+ is an abbreviation for :m .+1 which moves the current line to after line number .+1 (the current line number + 1). That is, the current line is moved down one line.

The command :m-2 is an abbreviation for :m .-2 which moves the current line to after line number .-2 (the current line number − 2). That is, the current line is moved up one line.

After visually selecting some lines, entering :m'>+ (that is :m '>+1), will move the selected lines to after line number '>+1 (one line after the last selected line; '> is a mark assigned by Vim to identify the selection end). That is, the block of selected lines is moved down one line.

The == re-indents the line to suit its new position. For the visual-mode mappings, gv reselects the last visual block and = re-indents that block.

See also

Comments

Advertisement