Emacs: Formatting Integer Ranges With Tiny

Today I want to introduce you to Tiny, yet another Emacs package by the prolific author abo-abo. His most well-known works—such as Hydra, avy, Lispy—allow you to accomplish a lot with a minimal amount of input. Tiny is no different, as it lets you quickly create and manipulate ranges of numbers. In this short post I’ll show you how Tiny works and why I consider it useful.


After installing Tiny all you need to do is bind tiny-expand to a key. This is my personal setup, using John Wiegley’s terrific use-package:

(use-package tiny
  (global-set-key (kbd "C-s-t") 'tiny-expand))

Or you can call (tiny-setup-default) which binds the command to C-;, and off the top of my head I can’t think of another mode which conflicts over binding that key.

Either way, once you have tiny-expand bound to a key you’re ready to go.

Making a Numbered List

Here’s a very simple but practical example: let’s say I want to make a numbered list of five items. With Tiny I can type m1\n5 and then, with the point at the end, I can press the key to which I’ve bound tiny-expand and I’ll end up with this:


The syntax may look cryptic at first, but is quite simple.

m[range start][separator(s)][range end]

So we can breakdown my example of m1\n5 like so:

  1. The initial m is mandatory.

  2. Next is the initial value of the range, in this case one. But this is optional and will default to zero.

  3. Then comes any separator characters to insert between each number. I use \n to add a newline after each number.

  4. And finally is the final value for the range, five in this example.

Fancier Formatting

Do you know how I generated that numbered list of four elements? I used m1\n\n4|%d., which Tiny expands into:





This demonstrates that you can have multiple separator characters; here I use two \n’s for the extra space. But there is that |%d. at the end. Tiny lets you use Emacs Lisp format strings, which you write at the end of your expression and delimit with a vertical bar. The formatting string %d. prints each number as a digit (%d) with a period after it. Used in conjunction with the newline separators it gives me exactly the output I want for a numbered list in Markdown.

Getting Even Fancier

Immediately following the end-range value we can write abbreviated Lisp. The current value is bound to x, so for example m1, 10*xx will expand to 1, 4, 9, 16, 25, 36, 49, 64, 81, 100. That is, the markup loops from one to ten, and at every step executes the Lisp expression (* x x) where x is each number in the range.

Format strings can follow any Lisp expressions, and those strings can themselves contain Lisp expressions wrapped in %(…). Here we see Tiny at its most complete syntax. One way I’ve found this functionality useful is for easily generating numeric data in code, using a combination of Lisp to calculate values and format strings to create the desired output. For example, consider this:

local e = {
m1,\n10|    [%d] = %(exp x)

When I run tiny-expand on the markup inside the braces I end up with this:

local e = {
    [1] = 2.718281828459045,
    [2] = 7.38905609893065,
    [3] = 20.085536923187668,
    [4] = 54.598150033144236,
    [5] = 148.4131591025766,
    [6] = 403.4287934927351,
    [7] = 1096.6331584284585,
    [8] = 2980.9579870417283,
    [9] = 8103.083927575384,
    [10] = 22026.465794806718

Nifty, right?


I hope this shows how useful Tiny can be for rapidly working with ranges of integers. Since you can use any amount of Lisp for crunching numbers and formatting output you can do more with Tiny than create lists of numbers. For example, the source code for Tiny has examples of generating sequences of letters, URLs, shell commands, and more. So I recommend you check it out to stir your imagination, as the examples in the code are written in such a way that you can execute them in place to get an even better idea of how the package works.

If you want to create some output based on a range of integers then it’s hard to beat the efficient expressiveness of Tiny.

(Update: 9th of July 2015) Check out this useful cheat-sheet of Tiny’s syntax submitted by Kaushal Modi


3 thoughts on “Emacs: Formatting Integer Ranges With Tiny

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s