Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Incrementor Objects for Vim

branch: master
README.asciidoc

Nexus

Generators for Vim

Tip
If you like Nexus and want to share the W00t!, I’m grateful for tips or beverages.

:Nexus

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.

Examples

No arguments:
: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 initialisation and substitute as a single command sequence:
: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.
Start and Step arguments

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,
Generator

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
Format Function

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.

Series() Generator for Creating Incrementor Objects

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

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, …

Something went wrong with that request. Please try again.