Generators for Vim
The simplest way to use Nexus is through the :List command. Visually select a range of lines and type :List to have them numbered from 1 with leading 0 padding where appropriate.
Tip
|
If you want to change the default behaviour, :List accepts the same arguments as :Nexus below. |
:List adds numbers to the start of every line in the provided range. If you need to number elements within a line then you need to use :Nexus instead.
The :Nexus command generates a callback function called Nexus() which can be used in code or substitutions to yield the next value in the associated series.
:Nexus
Will create a simple numeric sequence starting at 0 and incrementing by 1 with each call to Nexus(). To replace all 'foo' in your file with this series you could then use:
:%s/foo/\=Nexus()/g
:Nexus 10 1 %d, | %s/^\(\d*,\)\?/\=Nexus()/
Note
|
The above command renumbers lines in the file starting from a count of 10, incrementing by 1 each line, replacing existing numbers if present. It uses a format of ``%d,'' to separate the new number from the rest of the line text with a comma. |
Numeric arguments will be parsed as follows:
:Nexus [step] :Nexus start step
So:
:Nexus 2 " yields a counter starting at 0 with a step of 2: 0, 2, 4 :Nexus 3 2 " yields a counter starting at 3 with a step of 2: 3, 5, 7,
A function name will change the generator used in the Nexus() callback accordingly:
:Nexus nexus#fibonacci
or, if you’ve written your own generator for, say, prime numbers:
:Nexus Primes
An argument that contains a : or % will be taken as the format string. The default format string is: '%d' which simply expands to the (numeric!) value of the next item in the series at each call. If your generator yields non-numeric values (such as the built-in nexus#alpha) then you will need to create your own format string accordingly:
:Nexus %s\ nexus#alpha
Note
|
There are two spaces after the \ — the first is captured as part of the format string and the second separates the two arguments (format string and generator). |
These arguments may appear in any order with the caveat that the step or start and step will always be considered in that order when numbers appear anywhere as standalone arguments. So, although the following layout is not recommended for reader-clarity, it is nonetheless possible:
:Nexus 1 nexus#alpha %s\ 2
and will create an alpha series starting at the first term and step by two with each call to Nexus(). This will generate: b, d, f, h, j, …
Note
|
Nexus generators are zero indexed. The zeroth alpha term is 'a'. |
Nexus comes with a incrementor-object generator called Series(), the objects of which have a method called next() which returns the next number in the series. Create objects like this:
:let even_seq = Series(2)
The command:
:echo even_seq.next()
will print 0 on the first call, then 2, then 4, 6, 8, etc, on subsequent calls.
The Series constructor has the following signatures:
-
Series() → 0, 1, 2, …
-
Series(7) → 0, 7, 14, 21, …
-
Series(7, 2) → 7, 9, 11, …
-
Series(2, 7) → 2, 9, 16, …
-
Series('nexus#alpha') → 'a', 'b', 'c', …
-
Series('(%d) ') → (0)
Nexus’s Series Objects provide the following methods:
-
reset() to set the object back to its original values when created
-
next() to increment and return the next number in the series
-
value() to return the current value without incrementing internal state
Other generators can be written for Nexus and passed to Series() as a string in the first argument:
:let fib = Series('Fibonacci')
Will create a Series object called fib that generates the Fibonacci sequence on each subsequent .next() call. This generator and object exist by default in the Nexus plugin. They are not expected to be useful in themselves but rather serve to demonstrate how user-created generators can be used with Nexus. Consult the code in plugin/nexus.vim to learn how to craft your generator. If you do find a use for the Fibonacci sequence in your Vim life, I would love to hear about it. :-)
Nexus also provides the following generators:
-
nexus#alpha — a, b, c, …, z, aa, ab, ac, …
-
nexus#roman — I, II, III, IV, …