The Mastering Emacs ebook cover art

Emacs 28 Edition is out now!

Read a Free Sample

Learn More

Spotlight: Flycheck, a Flymake replacement

Spotlight on Flycheck, an excellent as-you-type syntax checker.
Updated for emacs 27

Today’s spotlight is Flycheck, an excellent drop-in replacement for Flymake.

If you’re a regular Emacs user you may have heard of Flymake, a built-in package in Emacs that adds you as-you-type syntax checking and error highlighting. It’s been a part of Emacs for a long time (around twenty years.) But there is also a drop-in replacement called Flycheck. It’s a third-party package, and you can find it on MELPA.

Both Flymake and Flycheck use the same, simple, principle of calling out to an external tool – be it a linter, compiler or interpreter – with a file copy of the active buffer you’re editing, parsing the resulting errors and warnings, and highlighting them in your buffer.

So why switch away from Flymake? Well, before the advent of Emacs 27 and the renewed interest in Flymake, I’d argue because Flymake had atrophied for a long time. Nowadays the distinction is less clear. If running M-x flymake-mode works as you expect – this is doubly true if you’re using a Language Server client like eglot or LSP-mode – then you can just stick with Flymake. It works well. But, if you’re using an older version of Emacs, or if you want a package with nearly a decade’s worth of attention lavished on it, you may want to give Flycheck a shot.

Flycheck works with most major and minor programming languages and environments – both C and C++, with or without Clang, for instance – out-of-the-box. For most Emacs users it’s easy to download and install and have it up and running in minutes. You simply invoke M-x flycheck-mode in a buffer, or globally with M-x global-flycheck-mode. Another benefit of Flycheck is a wider array of third-party plugins to Flycheck – available on your local Emacs package repository – if your favorite language is not supported out of the box. As Flycheck’s an established package with a large body of third-party packages, it’s easy to find a package that works well with obscure languages or tools.

Flycheck also has its own keybinding, C-c ! by default, with a handful of utility commands you may find useful:

C-c ! ?Describe a Flycheck Checker
C-c ! C-cCompile using checker
C-c ! C-wCopy error point is on to kill ring
C-c ! CClear all highlights from buffer
C-c ! VReport Flycheck version
C-c ! cStart syntax checking current buffer
C-c ! eChange Flycheck executable
C-c ! iOpen Flycheck info manual
C-c ! lList all Flycheck errors
C-c ! nJump to next error
C-c ! pJump to previous error
C-c ! sChange Flycheck checker
C-c ! vVerifies the Flycheck checker works
C-c ! xDisable Flycheck checker in buffer

Of particular note is C-c ! v as a diagnostic tool. When you run the command Flycheck will display diagnostic information about the active checker in your current buffer. Useful if you have $PATH issues or are missing the checker entirely on your system.

The command C-c ! C-c is also handy. Instead of checking the buffer source code you can also instruct Flycheck to compile the file; whether that works or not depends entirely on your programming language and the checker used.

Another major benefit is the ease of which you can add your own checkers. Using an elisp macro called flycheck-define-checker you can construct a checker with just a few arguments and Flycheck will handle the rest. I particularly like the use of LISP macros’ pattern matching and rx instead of the far more unreadable and brittle string-based regular expressions Flymake used.

Take a look at the XML linter. It’s very easy to understand and modify:

(flycheck-define-checker xml-xmllint
  "A XML syntax checker and validator using the xmllint utility.

The xmllint is part of libxml2, see URL
  :command ("xmllint" "--noout" source)
  ((error line-start (file-name) ":" line ": " (message) line-end))
  :modes (xml-mode nxml-mode))

So, if you’re using a newer version of Emacs, and if the default Flymake doesn’t tick all the boxes, you should give Flycheck a shot.

Further Reading

Have you read my Reading Guide yet? It's a curated guide to most of my articles and I guarantee you'll learn something whether you're a beginner or an expert. And why not check out my book?

Subscribe to the Mastering Emacs newsletter

I write infrequently, so go on — sign up and receive an e-mail when I write new articles

There are no comments. Why not write one?