Vim Tips Wiki
Register
Advertisement

Previous TipNext Tip

Tip: #646 - Moving lines up or down

Created: January 28, 2004 22:33 Complexity: basic Author: Frank Butler Version: 5.7 Karma: 80/42 Imported from: Tip#646

The following mappings in .vimrc provide a quick way to move a line of text up or down within a file:

 map <C-Up> dd-P 
 map <C-Down> ddp 

Hold down the Control key, and the <Up> and <Down> arrow keys move the line. Check it out!

This is particularly useful when editing a file consisting of single-line items in a particular order (such as priority) - it makes it easy to change the relative position of items in the list.

Comments

This works everywhere except at the top or bottom of the buffer. There was a discussion about this on the Vim list a while back and a swap line plugin came out of it -- I don't know who produced it in the first place and I won't want to post it here without giving due credit, but basically it mapped the key combinations to two different functions that would first store the current column the cursor was in, then swap the line (behaving differently at the beginning/end of the buffer -- try doing the combination in the original tip here to see what I mean about the need to behave differently at the edges of the buffer) and then restore the cursor position, leaving you where you started, but with the lines swapped.

salmanhalim--AT--hotmail.com , January 29, 2004 7:00


I've been using the following for a while. I like them because:

  • they work in any mode.
  • in visual mode they operate on the entire block.
  • the indentation is adjusted automagically.
  • they can handle the tops and bottoms of files.
" move the current line up or down 
nmap <C-Down> :m+<CR>== 
nmap <C-Up> :m-2<CR>== 
imap <C-Down> <C-O>:m+<CR><C-O>== 
imap <C-Up> <C-O>:m-2<CR><C-O>== 

" move the current line left or right 
nmap <C-Left> << 
nmap <C-Right> >> 
imap <C-Left> <C-O><< 
imap <C-Right> <C-O>>> 

" move the selected block up or down 
vmap <C-Down> :m'>+<CR>gv=gv 
vmap <C-Up> :m'<-2<CR>gv=gv 

" move the selected block left or right 
vmap <C-Right> >gv 
vmap <C-Left> <gv

jcm314--AT--hotmail.com , January 30, 2004 13:22


A variation on the above, <f11> stores one line <f12> restores it, also allows copying one line to another file

map <f11> :.w! c:/aaa/xr<CR> 
map! <f11> <ESC>:.w! c:/aaa/xr<CR> 
map <f12> :r c:/aaa/xr<CR> 
map! <f12> <ESC>:r c:/aaa/xr<CR>

zzapper--AT--ntlworld.com , February 2, 2004 11:12


Cool! I took jcm314's nmap's for up/down (you get an error at top/bottom of file) and left/right, except dropped the == from the end (don't want line to get reformatted when I move it).

frankbutler--AT--ieee.org , February 9, 2004 16:42


jcm314's mapping is great, thanks. I also remove the reformatting "==" and "=gv" since it cause some confusion when moving a line/block through complex code.

vimmiv--AT--msn.com , March 22, 2004 21:40


another strategy for moving visual mode blocks is:

:vmap <f6> xkP1v 
:vmap <f7> xp1v 

translation: "x = cut, k = move up one line, P = paste, 1v = reselect last visual area"

I haven't tested this extensively, so I don't know how well it works under heavy use.

--Robert

ramses0--AT--yahoo.com , May 20, 2004 9:22


Advertisement