Step 2 of 6

18. Ex Commands Addresses And Ranges

Addresses

Most Ex commands can be optionally preceded by either an address or range that the command should operate on. We’re going to talk about addresses in this section. An address is just a line specifier — they’re used to tell Vim “I’m talking about this line”.

Addresses

To specify an address, you prefix the command like so:

:[address]command

If command supports an address, this will cause it to operate on the address you’ve specified.

For example, if you wanted to delete the 10th line in the buffer, you could use:

:10d

The most basic example of an address is a literal line number, like what we’ve done above. But there are various ‘special’ addresses that are well worth remembering. We’re going to look at these in the next section.

In a sentence, addresses can be used to precisely specify lines.

Let’s give it a try in the editor. Work through the checklist below to delete lines 30, 20, and 10. It runs bottom-up on purpose: every deletion shifts the lines below it, so deleting line 30 first keeps the other two addresses honest.

  1. Delete line 30 first No need to move the cursor — type :30d and hit Enter.
  2. Delete line 20 second Line 20 is still line 20, because everything below it is already gone — type :20d and hit Enter.
  3. Delete line 10 last Type :10d and hit Enter. Had you deleted this one first, lines 20 and 30 would have shifted up under different numbers.
Delete line 30 first No need to move the cursor — type :30d and hit Enter.

Next: Delete line 20 second

Loading editor…