Vim Tips Wiki
(→‎Comments: reply)
Tag: rollback
(15 intermediate revisions by 6 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
Programmers often want to move a line of text up or down, or to some other position. We start by explaining the basics (cut and paste, as well as move), and show how to append to a register. An amazing trick with the redo register is then presented (useful for reordering up to nine lines), and there is a set of mappings so you can press a key to move the current line, or a block of selected lines, up or down.
+
Programmers often want to move a line of text up or down, or to some other position. We start by explaining the basics (cut and paste, as well as move), and show how to append to a register. An amazing trick with the redo register is then [[#Reordering up to nine lines|presented]] (useful for reordering up to nine lines), and there is a set of mappings so you can press a key to move the current line, or a block of selected lines, up or down.
   
 
==Cut and paste==
 
==Cut and paste==
Cut the line that you want to move by typing <tt>dd</tt>, or visually select some lines (press <tt>V</tt> then move the cursor) and type <tt>d</tt> to cut the selected block.
+
Cut the line that you want to move by typing <code>dd</code>, or visually select some lines (press <code>V</code> then move the cursor) and type <code>d</code> to cut the selected block.
   
Then move the cursor, and paste the text at the new position (press <tt>p</tt> to paste after the line with the cursor, or <tt>P</tt> to paste before).
+
Then move the cursor, and paste the text at the new position (press <code>p</code> to paste after the line with the cursor, or <code>P</code> to paste before).
   
 
You can cut several lines, or blocks of text, by appending to a register using an uppercase letter, for example:
 
You can cut several lines, or blocks of text, by appending to a register using an uppercase letter, for example:
*Move to the first line; type <tt>"add</tt> to delete it to register <tt>a</tt>.
+
*Move to the first line you wish to move; type <code>"add</code> to delete it to register <code>a</code>.
*Move to the next line; type <tt>"Add</tt> to delete it and ''append'' to the register.
+
*Move to another line; type <code>"Add</code> to delete it and ''append'' to the same register.
*Move to the next line; type <tt>"Add</tt> to delete it and ''append'' to the register.
+
*Move to another line; type <code>"Add</code> to delete it and ''append'' to the same register.
  +
**or type "." for the same effect
  +
*Select a range of lines vith V; type <code>"Ad</code> to delete the entire range and append it to the same register.
 
*Continue in this fashion.
 
*Continue in this fashion.
 
*Move to the wanted destination.
 
*Move to the wanted destination.
*Press <tt>"ap</tt> to paste after the line with the cursor, or <tt>"aP</tt> to paste before.
+
*Press <code>"ap</code> to paste after the line with the cursor, or <code>"aP</code> to paste before.
   
 
==Move command==
 
==Move command==
You can move a line, or a block of lines, with the <tt>:m</tt> command. Examples:
+
You can move a line, or a block of lines, with the <code>:m</code> command. Examples:
 
{| class="cleartable"
 
{| class="cleartable"
| <tt>:m 12</tt> || move current line to after line 12
+
| <code>:m 12</code> || move current line to after line 12
 
|-
 
|-
| <tt>:m 0</tt> || move current line to before first line
+
| <code>:m 0</code> || move current line to before first line
 
|-
 
|-
| <tt>:m $</tt> || move current line to after last line
+
| <code>:m $</code> || move current line to after last line
 
|-
 
|-
| <tt>:m 'a</tt> || move current line to after line with mark <tt>a</tt> (see [[using marks]])
+
| <code>:m 'a</code> || move current line to after line with mark <code>a</code> (see [[using marks]])
 
|-
 
|-
| <tt>:m 'a-1</tt> || move current line to before line with mark <tt>a</tt>
+
| <code>:m 'a-1</code> || move current line to before line with mark <code>a</code>
 
|-
 
|-
| <tt>:m '}-1</tt> || move current line to the end of the current paragraph
+
| <code>:m '}-1</code> || move current line to the end of the current paragraph
 
|}
 
|}
   
To move a block of lines, visually select the lines before entering the move command. For clarity, a space is shown after the <tt>:m</tt> command but that space is not required.
+
For clarity, a space is shown after the <code>:m</code> commands above, but that space is not required.
  +
  +
To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary [[ranges]] with the move command. Examples:
  +
{| class="cleartable"
  +
| <code>:5,7m 21</code> || move lines 5, 6 and 7 to after line 21
  +
|-
  +
| <code>:5,7m 0</code> || move lines 5, 6 and 7 to before first line
  +
|-
  +
| <code>:5,7m $</code> || move lines 5, 6 and 7 to after last line
  +
|-
  +
| <code>:.,.+4m 21</code> || move 5 lines starting at current line to after line 21
  +
|-
  +
| <code>:,+4m14</code> || same (<code>.</code> for current line is assumed)
  +
|}
   
 
==Reordering up to nine lines==
 
==Reordering up to nine lines==
Line 58: Line 73:
 
</pre>
 
</pre>
   
Move the cursor to "line&nbsp;1" and type <tt>dd</tt> to delete the line. Go to "line&nbsp;2" and press <tt>.</tt> to repeat (delete another line). Repeat this on "line&nbsp;3", and so on, until everything has been deleted in order.
+
Move the cursor to "line&nbsp;1" and type <code>dd</code> to delete the line. Go to "line&nbsp;2" and press <code>.</code> to repeat (delete another line). Repeat this on "line&nbsp;3", and so on, until everything has been deleted in order.
   
Now type <tt>"1P</tt> to paste the contents of register 1 before the cursor.
+
Now type <code>"1P</code> to paste the contents of register 1 before the cursor.
   
 
Repeat with the dot command, eight times:
 
Repeat with the dot command, eight times:
Line 69: Line 84:
 
The first dot command pastes register 2, and the next pastes register 3, and so on. The result is that all the lines are pasted, in the correct order.
 
The first dot command pastes register 2, and the next pastes register 3, and so on. The result is that all the lines are pasted, in the correct order.
   
You have to press <tt>.</tt> eight times (using a count like <tt>8.</tt> will insert the same line eight times). See {{help|redo-register}}
+
You have to press <code>.</code> eight times (using a count like <code>8.</code> will insert the same line eight times). See {{help|redo-register}}
   
 
==Mappings to move lines==
 
==Mappings to move lines==
 
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.
 
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.
 
<pre>
 
<pre>
nnoremap <A-j> :m+<CR>==
+
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m-2<CR>==
+
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m+<CR>==gi
+
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m-2<CR>==gi
+
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m'>+<CR>gv=gv
+
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m-2<CR>gv=gv
+
vnoremap <A-k> :m '<-2<CR>gv=gv
 
</pre>
 
</pre>
   
 
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.
 
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 <tt>V</tt> then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.
+
After visually selecting a block of lines (for example, by pressing <code>V</code> then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.
   
 
===Explanation===
 
===Explanation===
The command <tt>:m+</tt> is an abbreviation for <tt>:m&nbsp;.+1</tt> which moves the current line to after line number <tt>.+1</tt> (the current line number + 1). That is, the current line is moved down one line.
+
The command <code>:m .+1</code> (which can be abbreviated to <code>:m+</code>) moves the current line to after line number <code>.+1</code> (current line number + 1). That is, the current line is moved down one line.
   
The command <tt>:m-2</tt> is an abbreviation for <tt>:m&nbsp;.-2</tt> which moves the current line to after line number <tt>.-2</tt> (the current line number − 2). That is, the current line is moved up one line.
+
The command <code>:m .-2</code> (which can be abbreviated to <code>:m-2</code>) moves the current line to after line number <code>.-2</code> (current line number − 2). That is, the current line is moved up one line.
   
After visually selecting some lines, entering <tt>:m'>+</tt> (that is <tt>:m&nbsp;'>+1</tt>), will move the selected lines to after line number <tt>'>+1</tt> (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.
+
After visually selecting some lines, entering <code>:m '>+1</code> moves the selected lines to after line number <code>'>+1</code> (one line after the last selected line; <code>'></code> is a mark assigned by Vim to identify the selection end). That is, the block of selected lines is moved down one line.
   
The <tt>==</tt> re-indents the line to suit its new position. For the visual-mode mappings, <tt>gv</tt> reselects the last visual block and <tt>=</tt> re-indents that block.
+
The <code>==</code> re-indents the line to suit its new position. For the visual-mode mappings, <code>gv</code> reselects the last visual block and <code>=</code> re-indents that block.
   
 
==See also==
 
==See also==
 
*[[VimTip191|Transposing]] enhanced mappings (better at maintaining cursor position?)
 
*[[VimTip191|Transposing]] enhanced mappings (better at maintaining cursor position?)
 
*[[VimTip344|Cut or copy lines without counting the lines]] ''(needs to be fixed)''
 
*[[VimTip344|Cut or copy lines without counting the lines]] ''(needs to be fixed)''
  +
*[http://stackoverflow.com/questions/741814/move-entire-line-up-and-down-in-vim stackoverflow - Move entire line up and down in Vim]
   
 
==Comments==
 
==Comments==
Woah! You surprised me with the merge of the content in [[#Reordering up to nine lines]]! This content relies on a very different method than the rest, and is a surprising enough behavior that I think it may deserve its own tip, or at least a more prominent section. The trick to use '.' to cycle throught all 9 deletion registers is something I would never have learned without seeing that new tip. I might see it when looking at a random page, but I certainly wouldn't be looking for it. On seeing a page like "moving lines up or down", I would be unlikely to read the entire thing, thinking I'd seen all the content. I would have missed a gem like this. --[[User:Fritzophrenic|Fritzophrenic]] 18:25, March 26, 2010 (UTC)
 
:I do not have much emotional investment in what I did, so by all means reverse it. By way of explanation: (1) We noticed [[Reordering lines]] because we watch everything, but if someone were just browsing this wiki how would they find such a tip? Even if they looked at it (see [http://vim.wikia.com/index.php?title=Reordering_lines&oldid=24898 old version]), they would be quite likely to skip it given the introduction that might not appeal to busy readers. (2) No solution is perfect: separate tips is more maintenance for editors and overwhelming for readers; one merged tip is overly dense for readers and they might miss useful parts. (3) In the current tip above, I put "An amazing trick with the redo register..." in the lead specifically to alert a reader that they might want to read that section.
 
:To reverse: Undo my last edit at [[Reordering lines]]; add suitable "see also" here and there; remove text from here; update [[Vim Tips Wiki:New tips/200908|200908]]. I'll do that if you like. [[User:JohnBeckett|JohnBeckett]] 22:34, March 26, 2010 (UTC)
 

Revision as of 04:24, 29 October 2013

Tip 646 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0


Programmers often want to move a line of text up or down, or to some other position. We start by explaining the basics (cut and paste, as well as move), and show how to append to a register. An amazing trick with the redo register is then presented (useful for reordering up to nine lines), and there is a set of mappings so you can press a key to move the current line, or a block of selected lines, up or down.

Cut and paste

Cut the line that you want to move by typing dd, or visually select some lines (press V then move the cursor) and type d to cut the selected block.

Then move the cursor, and paste the text at the new position (press p to paste after the line with the cursor, or P to paste before).

You can cut several lines, or blocks of text, by appending to a register using an uppercase letter, for example:

  • Move to the first line you wish to move; type "add to delete it to register a.
  • Move to another line; type "Add to delete it and append to the same register.
  • Move to another line; type "Add to delete it and append to the same register.
    • or type "." for the same effect
  • Select a range of lines vith V; type "Ad to delete the entire range and append it to the same register.
  • Continue in this fashion.
  • Move to the wanted destination.
  • Press "ap to paste after the line with the cursor, or "aP to paste before.

Move command

You can move a line, or a block of lines, with the :m command. Examples:

:m 12 move current line to after line 12
:m 0 move current line to before first line
:m $ move current line to after last line
:m 'a move current line to after line with mark a (see using marks)
:m 'a-1 move current line to before line with mark a
:m '}-1 move current line to the end of the current paragraph

For clarity, a space is shown after the :m commands above, but that space is not required.

To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary ranges with the move command. Examples:

:5,7m 21 move lines 5, 6 and 7 to after line 21
:5,7m 0 move lines 5, 6 and 7 to before first line
:5,7m $ move lines 5, 6 and 7 to after last line
:.,.+4m 21 move 5 lines starting at current line to after line 21
:,+4m14 same (. for current line is assumed)

Reordering up to nine lines

The following example lines can be moved to a different order by deleting each line in turn (starting with the line that will be first when the move is complete):

line 3
line 9
line 8
line 1
line 5
line 7
line 2
line 6
line 4

Move the cursor to "line 1" and type dd to delete the line. Go to "line 2" and press . to repeat (delete another line). Repeat this on "line 3", and so on, until everything has been deleted in order.

Now type "1P to paste the contents of register 1 before the cursor.

Repeat with the dot command, eight times:

........

The first dot command pastes register 2, and the next pastes register 3, and so on. The result is that all the lines are pasted, in the correct order.

You have to press . eight times (using a count like 8. will insert the same line eight times). See :help redo-register

Mappings to move lines

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 .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<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 .+1 (which can be abbreviated to :m+) moves the current line to after line number .+1 (current line number + 1). That is, the current line is moved down one line.

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

After visually selecting some lines, entering :m '>+1 moves 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