Simple Neovim Ranges

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
Prior to Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
COMMAND
40%
4:1
:2,5yank a
After Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
4 lines yanked into "a
Confirm Register Content
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
 
:reg a
Type Name Content
  l  "a   happen that the only thing his eyes still see is that what he searche

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,$
Prior to Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
COMMAND
40%
4:1
:1,$yank b
After Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
9 lines yanked into "b
Confirm Register Content
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
 
:reg b
Type Name Content
  l  "b   "When someone is searching," said Siddhartha, "then it might easily

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:

Prior to Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
COMMAND
40%
4:1
:%yank c
After Execution
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
9 lines yanked into "c
Confirm Register Content
"When·someone·is·searching,"·said·Siddhartha,·"then·it·might·easily
happen·that·the·only·thing·his·eyes·still·see·is·that·what·he·searches
for,·that·he·is·unable·to·find·anything,·to·let·anything·enter·his
mind,·because·he·always·thinks·of·nothing·but·the·object·of·his·search,
because·he·has·a·goal,·because·he·is·obsessed·by·the·goal.·Searching
means:·having·a·goal.·But·finding·means:·being·free,·being·open,·having
no·goal.·You,·oh·venerable·one,·are·perhaps·indeed·a·searcher,·because,
striving·for·your·goal,·there·are·many·things·you·don't·see,·which·are
directly·in·front·of·your·eyes."
NORMAL
40%
4:1
 
:reg c
Type Name Content
  l  "c   "When someone is searching," said Siddhartha, "then it might easily