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/gchanges all old to new in the current line
:11,15s/old/new/gchanges lines 11 to 15 inclusive
:%s/old/new/gchanges all lines
A range can be specified using line numbers or special characters, as in these examples:
| ||line 21|| |
| ||first line|| |
| ||last line|| |
| ||current line|| |
| || all lines (same as || |
| ||lines 21 to 25 inclusive|| |
| ||lines 21 to end|| |
| ||current line to end|| |
| ||line after current line to end|| |
| ||six lines (current to current+5 inclusive)|| |
| || same (|| |
: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.
If you know you want to substitute in six lines, starting from the current line, you can use either of the ranges shown above. An easier method is to enter a count value (type
6), then enter the colon command with no range (type
:s/old/new/g). Because you entered a count, Vim displays the command:
For most commands, the default range is
. (the current line, for example,
:s/// substitutes in the current line). However, for
:w the default is
% (all lines).
| || ||substitute in current line|
| || || list all lines matching |
| || || write all lines to file |
A command like
:123,145s/old/new/g substitutes in lines 123 to 145 inclusive, but what if you're not sure what the line numbers are? One method is to use marks: Type
ma in the first line, then type
mb in the last line (to set marks
b). Then enter command
:'a,'bs/old/new/g to substitute in lines from mark
Another method is to visually select lines, then enter a colon command (for example,
:s/old/new/g). Note that you do not enter a range. However, because the command was entered while lines were selected, Vim displays the command as:
'<,'> is entered automatically to identify the lines that were last visually selected (they do not need to be visually selected now).
For example, you might type
vip to visually select "inner paragraph" (the paragraph holding the cursor). Then type
:s/old/new/g to substitute in all lines in the selected paragraph.
Deleting, copying and movingEdit
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),
:co (copy lines), and
:m (move lines).
| ||delete lines 21 to 25 inclusive|
| ||delete the last line|
| ||delete all lines before the current line|
| ||delete all lines after the current line|
| ||copy lines 21 to 25 inclusive to just after line 30|
| ||copy the last line to before the first line|
| ||move lines 21 to 25 inclusive to just after line 30|
| ||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 searchesEdit
In a range, a line number can be given as:
- A mark (for example,
'xis the line containing mark
- A search (for example,
/pattern/is the next line matching pattern).
When using a mark, it must exist in the current buffer.
| || delete lines from mark |
| || delete lines from the current line to mark |
| || move lines from mark |
Here are some examples using searches:
- Copy the lines from the current line to the next line containing 'green' (inclusive), to the end of the buffer.
- Replace all "old" in the next line in which the "apples" occurs, and the line following it.
- Same (
.+1, and because
;was used, the cursor position is set to the line matching "apples" before interpreting the
- 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).
To do a replace in blocks identified by an initial and a final pattern:
- Replace all "old" in the first block that starts with "apples" and ends with "peaches".
/apples/identifies the first line after the cursor containing "apples".
/peaches/is similar (first line after the current line, not the first after "apples"). Be aware of backwards ranges.
- The block is all lines from "apples" to "peaches", inclusive.
- Same, but "peaches" identifies the first occurrence after "apples".
:/apples/,/peaches/ s/^/# /g
- Insert "
#" at the start of each line in the first block.
:/apples/+1,/peaches/-1 s/^/# /g
- Insert "
#" at the start of each line inside the block.
To do a global replace in all blocks with the same patterns, use
:g/apples/,/peaches/ s/^/# /g
- Insert "
#" at the start of each line in all identified blocks.
:g/apples/identifies each line containing "apples".
- In each such line,
.,/peaches/ s/^/# /gis executed
.is assumed; it means the current line, where "apples" occurs).
:g/^function!\? \(s:\)\?My/;/^endfunction/s/^/" /
- This example is for a Vim script where functions start with
function!and end with
- Insert "
"" at the start of each line in each block.
- All functions that start with
function s:Mywill be commented out.
- The last line in each block is where
endfunctionfirst occurs (at the left margin), after where
function Myis found.
Even more tricks are available; see :help cmdline-ranges. Summary:
| ||next line where pattern matches|
| ||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|
| ||first line containing "that" (also matches in the first line)|
| ||first line after line 1 containing "that"|