Skip to content

Tokei for Mere Mortals

Every codebase eventually gets asked the same question: how much code is in this thing? The answers that fly back — a line count from wc, a number off a dashboard — usually mix code, comments and blank lines into one figure and call it code. tokei is a small, fast program that walks a folder of source code and counts the three separately, per language: the lines that carry the program, the lines that carry only explanation, and the empty lines in between.

One rule governs everything it reports, and this guide states it in chapter 1 and keeps pointing back to it: a line count is an answer to a definition. Tokei's rules are the definition — how it decides what a file is, what a comment is, what belongs to which column. When a number surprises you, a rule is speaking. This guide's business is to make the rules visible, one specimen at a time.

The lab behind this guide ran the real thing: tokei 15.0.0, built from the project's own source on a small Linux machine, against fixture files the lab wrote for the purpose — every one counted by hand before tokei was allowed near it. Against them the lab verified the plain counts in C, Python and Rust; watched a comment marker inside a string fool nobody; proved nested block comments nest; found Python docstrings counted as code and flipped them to comments with one configuration line; saw Rust's doc comments reappear as embedded Markdown; split a web page into its three languages; changed the totals three ways with flags while changing no files; woke a .gitignore by giving it a repository; fed the JSON output to a second program and caught the summing trap in it; and checked a rigged "20 lines of code" claim down to the 6 lines that were code. It also counted an executable renamed .py as 94 lines of Python, because the detector believes names — that is in chapter 13, where it belongs.

What the lab could not make work, it did not fake. The -i option that claims to re-read saved counts never changed a table in four attempts, and chapter 9 says so. Output formats beyond JSON need a build the lab did not make. Other platforms' installs are named from the project's documentation, not from transcripts. criteria.md keeps the full ledger of what was run and what was only read.

What this guide covers

  • What tokei is — the three numbers, the speed, and the one rule about definitions
  • The moving parts — the walker, the detector, the counter, the reporter
  • Installing tokei — the cargo route, the toolchain that was too old, and why the release page stops at 12.1.2
  • Your first count — the table column by column, the per-file receipts, and prose counted as comments
  • How tokei knows — filenames, extensions, shebangs, the file that was not counted, and the flag that names it
  • The counting rules — code wins ties, block comments are a state, strings are not comments, nesting nests
  • Docstrings and embedded languages — the string that looks like a comment, doc comments as Markdown, and the 12-line page that totals 16
  • Flags that change the answer — hidden files, exclusions, the three ignore switches, and the .gitignore that needed a repository
  • Output formats — JSON and its summing trap, the stream, formats as a build-time choice, and the option that did nothing
  • Making it yours — tokei.toml, the docstring flip, the types filter, and configuration that follows where you stand
  • Checking a claim — wc against tokei on a rigged tree, and a procedure that scales to a real one
  • The neighbours — wc, cloc, scc, Linguist, and the hand-rolled counter
  • Limits — believing names, prose as comments, double-counted embeds, and syntax without sense
  • Troubleshooting — the missing file, the moved total, the ignored ignore file
  • Glossary — the terms, collected in one place

What it doesn't cover

Judging code. A count says how much text there is, sorted three ways; nothing here turns it into a verdict on quality, difficulty or value, and chapter 13 is blunt about the difference. Counting languages tokei does not know (its table holds 334; yours may hold one more) is out — the hand-rolled route is discussed in chapter 12, with its hazards. Using tokei as a library from Rust is a programmer's topic and is left to the project's own documentation.

How to use this guide

Read chapter 1 for the rule and chapter 2 for the map of parts. Chapters 4 to 8 are the rules themselves, each on specimens you can recount by hand; chapters 9 and 10 put the counts to work and bend them on purpose. Chapter 11 is the one to have open when somebody quotes you a number. The lab's fixtures live in tokei-lab/fixtures/, every claim's evidence in tokei-lab/transcripts/, the distilled commands in examples/commands.txt and the lab's own notes in examples/lab-notes.txt.


Part of the series

This guide is one of the For Mere Mortals guides: plain-language guides to the tools programmers and makers use.

Related guides:

License

The writing in this guide is licensed under CC BY 4.0. Code examples and lab scripts are licensed under the MIT License.