sponsor Vim development Vim logo Vim Book Ad

basic Tip #646: moving lines up/down in a file

 tip karma   Rating 57/32, Viewed by 1690 

created:   January 28, 2004 22:33      complexity:   basic
author:   Frank Butler      as of Vim:   5.7

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.

 rate this tip  Life Changing Helpful Unfulfilling 

<<Enabling Windows shortcuts (eg alt+space, F10 etc) for gvim window | Single letter insert >>

Additional Notes

[email protected], January 29, 2004 7:00
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.
[email protected], January 30, 2004 13:22
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
[email protected], February 2, 2004 11:12
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>
[email protected], February 9, 2004 16:42
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).
[email protected], March 22, 2004 21:40
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.
[email protected], May 20, 2004 9:22
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
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo