commands that run in command-line mode often accept a range, which specifies which line(s) of the current buffer are to be used as input for the command. The general pattern for specifying a range is:
:[from],[to]{command}
where from and to specify the start and end of the range, respectively, and {command} is the name of the command to be executed on those lines.
For example, to :yank lines 2 through 5 to register a, one could specify:
:2,5yank a
Let's take a look at what happened. Step 1 shows the starting cursor location, and the command to be executed (prior to execution). Step 2 indicates that 4 lines were yanked (2, 3, 4, and 5), and Step 3 confirms that the yanked range started with line 2 (as expected). We will use this format to show which commands are executed and the results throughout this chapter.
One of the powerful features of ranges is that from and to can be specified as expressions that are evaluated when the command is executed. There are several types of range expressions available, which we will review in the following sections.
We already saw the most simple case, where from and to are defined as static line numbers. The next set of range expressions evaluate to dynamically specify lines in the buffer:
Expression | Meaning |
---|---|
$ | the last line of the buffer |
. | the current line |
These expressions can be used directly in place of line numbers in the range. For example, the following range expression defines a range that contains all lines in the buffer:
:1,$
This range is so common that there is an expression for that too, %
. For example, to yank the
entire buffer into register a, the following are equivalent:
:1,$yank a
:%yank a
which is confirmed by comparing to the previous example: