Vim Tips Wiki
(Cleaned up tip and merged with 924)
No edit summary
Line 23: Line 23:
 
Hold down the <Alt> (Meta) key, and the <j> and <k> keys to move the lines down and up respectivley. Moved lines will be automatically formatted so as to enable correct indentation in their new position.
 
Hold down the <Alt> (Meta) key, and the <j> and <k> keys to move the lines down and up respectivley. Moved lines will be automatically formatted so as to enable correct indentation in their new position.
   
[[Category:Usage]]
+
[[Category:Moving]]

Revision as of 06:32, 22 November 2007

Tip 646 Printable Monobook Previous Next

created January 28, 2004 · complexity basic · author Frank Butler · version 5.7


The following mappings in vimrc provide a quick way to move a lines of text up or down within a file. The tip work in Visual, Normal and Insert mode, allowing you to move single lines or whole blocks. Eclipse has this feature as well and it is quite popular, this tip works the same as it does in eclipse.

" enable eclipse style moving of lines
nmap <M-j> mz:m+<CR>`z==
nmap <M-k> mz:m-2<CR>`z==
imap <M-j> <Esc>:m+<CR>==gi
imap <M-k> <Esc>:m-2<CR>==gi
vmap <M-j> :m'>+<CR>gv=`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<CR>gv=`>my`<mzgv`yo`z

Hold down the <Alt> (Meta) key, and the <j> and <k> keys to move the lines down and up respectivley. Moved lines will be automatically formatted so as to enable correct indentation in their new position.