FANDOM


(major rewrite)
(tweak and add amazing factoid)
Line 60: Line 60:
   
 
==Deleting, copying and moving==
 
==Deleting, copying and moving==
Ex commands are those typed after a colon (<tt>:</tt>). As well as the commands we've seen so far, it's handy to know how to use <tt>:d</tt> (delete lines), <tt>:t</tt> or <tt>:co</tt> (copy lines), and <tt>:m</tt> (move lines).
+
Ranges work with Ex commands (those typed after a colon, for example, <tt>:w</tt>). As well as the commands we've seen so far, it's handy to know how to use <tt>:d</tt> (delete lines), <tt>:t</tt> or <tt>:co</tt> (copy lines), and <tt>:m</tt> (move lines).
   
 
{| class="wikitable"
 
{| class="wikitable"
Line 102: Line 102:
   
 
Here are some examples using searches:
 
Here are some examples using searches:
;<tt>.,/green/co $</tt>
+
;<tt>:.,/green/co $</tt>
 
:copy the lines from the current line to the next line containing 'green' (inclusive), to the end of the buffer
 
:copy the lines from the current line to the next line containing 'green' (inclusive), to the end of the buffer
 
;<tt>:/apples/,/apples/+1s/old/new/g</tt>
 
;<tt>:/apples/,/apples/+1s/old/new/g</tt>
 
:replace all "old" in the next line in which the "apples" occurs, and the line following it
 
:replace all "old" in the next line in which the "apples" occurs, and the line following it
  +
;<tt>:/apples/;.1s/old/new/g</tt>
  +
:same (<tt>.1</tt> is <tt>.+1</tt>, and because <tt>;</tt> was used, the cursor position is set to the line matching "apples" ''before'' interpreting the <tt>.+1</tt>)
  +
;<tt>:/apples/,.100s/old/new/g</tt>
  +
:replace all "old" in the next line in which "apples" occurs, and all lines up to and including 100 lines after the current line (where the command was entered)
   
Even more tricks are available; see {{help|:range}}. Summary:
+
Even more tricks are available; see {{help|cmdline-ranges}}. Summary:
   
 
{| class="wikitable"
 
{| class="wikitable"

Revision as of 02:02, April 12, 2009

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created December 19, 2008 · complexity basic · version 7.0

A range permits a command to be applied to a group of lines in the current buffer. For most commands, the default range is the current line. For example:

  • :s/old/new/g   changes all old to new in the current line
  • :11,15s/old/new/g   changes lines 11 to 15 inclusive
  • :%s/old/new/g   changes all lines

Examples

A range can be specified using line numbers or special characters, as in these examples:

Range Description Example
21 line 21 :21s/old/new/g
1 first line :1s/old/new/g
$ last line :$s/old/new/g
. current line :.w single.txt
% all lines (same as 1,$) :%s/old/new/g
21,25 lines 21 to 25 inclusive :21,25s/old/new/g
21,$ lines 21 to end :21,$s/old/new/g
.,$ current line to end :.,$s/old/new/g
.+1,$ line after current line to end :.+1,$s/old/new/g
.,.+5 six lines (current to current+5 inclusive) :.,.+5s/old/new/g
.,.5 same (.5 is interpreted as .+5) :.,.5s/old/new/g

The :s/// command substitutes in the specified lines. The :w command writes a file. On its own, :w writes all lines from the current buffer to the file name for the buffer. Given a range and a file name, :w writes only the specified lines to the specified file. The example above creates file single.txt containing the current line.

Default range

For most commands, the default range is . (the current line, for example, :s/// substitutes in the current line). However, for :g// and :w the default is % (all lines).

Example Equivalent Description
:s/old/new/g :.s/old/new/g substitute in current line
:g/old/ :%g/old/ list all lines matching old
:w my.txt :%w my.txt write all lines to file my.txt

Deleting, copying and moving

Ranges work with Ex commands (those typed after a colon, for example, :w). As well as the commands we've seen so far, it's handy to know how to use :d (delete lines), :t or :co (copy lines), and :m (move lines).

Command Description
:21,25d delete lines 21 to 25 inclusive
:$d delete the last line
:1,.-1d delete all lines before the current line
:.+1,$d delete all lines after the current line
:21,25t 30 copy lines 21 to 25 inclusive to just after line 30
:$t 0 copy the last line to before the first line
:21,25m 30 move lines 21 to 25 inclusive to just after line 30
:$m 0 move the last line to before the first line

The line numbers in a command are those before the command executes. In the earlier example which moved lines 21..25 to after 30, the "30" refers to the line number before the move occurred.

Ranges with marks and searches

In a range, a line number can be given as:

  • A mark (for example, 'x is the line containing mark x).
  • A search (for example, /pattern/ is the next line matching pattern).

When using a mark, it must exist in the current buffer.

Command Description
:'a,'bd delete lines from mark a to mark b, inclusive
:.,'bd delete lines from the current line to mark b, inclusive
:'a,'bm 0 move lines from mark a to b inclusive, to the beginning

Here are some examples using searches:

:.,/green/co $
copy the lines from the current line to the next line containing 'green' (inclusive), to the end of the buffer
:/apples/,/apples/+1s/old/new/g
replace all "old" in the next line in which the "apples" occurs, and the line following it
:/apples/;.1s/old/new/g
same (.1 is .+1, and because ; was used, the cursor position is set to the line matching "apples" before interpreting the .+1)
:/apples/,.100s/old/new/g
replace all "old" in the next line in which "apples" occurs, and all lines up to and including 100 lines after the current line (where the command was entered)

Even more tricks are available; see :help cmdline-ranges. Summary:

Item Description
/pattern/ next line where pattern matches
?pattern? previous line where pattern matches
\/ next line where the previously used search pattern matches
\? previous line where the previously used search pattern matches
\& next line where the previously used substitute pattern matches
0;/that first line containing "that" (also matches in the first line)
1;/that first line after line 1 containing "that"

Comments

Community content is available under CC-BY-SA unless otherwise noted.