Skip to content

What tokei is

By the end of this chapter you can say what tokei counts, name the three numbers it reports for every language, and state the one rule that keeps those numbers honest.

"How much code is in this thing?"

Sooner or later someone asks the question: a manager pricing a rewrite, a maintainer inheriting a project, a programmer wondering what they have actually written. The tempting answer is a single number — fifty thousand lines — and the tempting tool is whatever counts lines fastest.

tokei is a program that walks a folder of source code and counts, for each programming language it finds, three separate numbers:

  • Code — lines that carry the program: statements, declarations, expressions.
  • Comments — lines that carry only an explanation for humans.
  • Blanks — empty lines, the breathing space between sections.

It reports the three side by side, per language, with a total — and it does it quickly. This guide's lab pointed tokei at its own source code, 7,735 lines across 23 files, and the count took 0.022 seconds (transcript tokei-lab/transcripts/t10-languages-speed.txt).

The rule: a count is an answer to a definition

Three numbers sound like three facts. They are three judgements, and every judgement follows a rule. Is a line holding both code and a comment code, or a comment? Is the empty line inside a long string blank, or part of the string? Is a Python docstring — which looks like a comment and is a string — code or commentary?

There is no answer floating free of a definition. There is only the definition a tool applies, line by line, the same way every time. So the guide's one rule is:

A line count is an answer to a definition. When a number surprises you, a rule is speaking — find the rule before you argue with the number.

Most of this guide is those rules, each one demonstrated on a specimen file whose true count was worked out by hand before tokei was allowed near it. Where tokei's rule differs from the one in your head — and it will, a few times — the guide says so plainly, with the numbers from both sides.

Where it comes from

Tokei is a free, open-source program written in Rust, maintained by XAMPPRocky and contributors; the project lives at github.com/XAMPPRocky/tokei. (The name is Japanese for "statistics".) This guide's lab ran tokei 15.0.0, built from the project's own source with the Rust toolchain, on a small Linux machine. Chapter 3 tells that install story honestly, including the two ways it failed first.

Check: name the three numbers tokei reports, and say the rule out loud once. If you can, you have this chapter.