← All lessons

18. Ex Commands Addresses And Ranges

Step 5 of 6

Default ranges

If you don’t remember ever having encountered addresses and ranges before, don’t worry; Ex commands have default addresses and ranges, so most of the time you don’t have to think about the lines a command acts over. Without passing a range to a command, Vim generally does the intuitive thing. For example, the default range for the :d (delete line) command is . — so running :d on its own will delete the line under the cursor.

This leads us to an interesting fact: did you know that the :w command accepts a range? The default range for :w is all of the lines in the file. That makes sense; when you’re using :w, you typically want to write the entire buffer content to disk. But if you were so inclined, you could specify a range to write a just one section of the file to disk. For example, :1,10w foo will write lines 1 through 10 to a file called foo. This can be useful if you have a large file that you want to break up into smaller sub-files.

Loading editor…