Ex Ranges

Every Ex command takes a range. Learn to speak it fluently.

Keys: :

Ranges precede Ex commands and tell them which lines to act on. They can be line numbers (5,10), marks ('a,'b), patterns (/foo/,/bar/), or shortcuts (%, .).

An Ex range tells the command which lines to operate on. It comes before the command name, separated by no space: :5,10s/foo/bar/g runs :s on lines 5 through 10.

Range Meaning
. Current line
$ Last line
% All lines (= 1,$)
* Last visual selection
5 Just line 5
5,10 Lines 5 through 10
.,+5 Current line and 5 below
.,$ Current line to end of file
1,. Start of file to current line
'a,'b From mark a to mark b
'<,'> Last visual selection (auto-inserted when you press : from Visual)
/foo/,/bar/ From next line matching /foo/ to next line matching /bar/
?foo?,/bar/ From previous line matching /foo/ to next /bar/

Watch

See also: Substitute ({key::s}), Global ({key::g}) and Vglobal ({key::v}), Setting and Jumping to Marks