Skip to content
Tutorials Center
My learning

Topics

Lesson 27 of 38 ยท Packages and Programming Modes

Completion at Point, Snippets and Diagnostics in Emacs

Set up in-buffer completion with Corfu or Company, expand templates with snippets, and see errors with Flymake while you type, using real Emacs configuration.

Writing code or prose is faster when the editor completes names, expands boilerplate and underlines mistakes as you type. Emacs has all three, built from a few cooperating pieces: completion-at-point, snippets, and Flymake diagnostics. In this lesson you configure each one and learn how they fit together.

Configuration snippets are real Emacs Lisp. The runnable blocks are JavaScript stand-ins that model a snippet expander and a diagnostics list, because the editor itself cannot run in the page.

Completion at point

The key C-M-i (or M-TAB on some systems) calls completion-at-point. Emacs asks each function in the buffer-local list completion-at-point-functions (the capfs) for candidates until one answers. Major modes, Eglot, dabbrev-capf and many packages contribute capfs, so one mechanism serves Elisp symbols, language-server results, file names and more.

By default the result appears in a *Completions* buffer. A popup package makes it automatic:

ELISP
(use-package corfu
  :ensure t
  :custom
  (corfu-auto t)              ; popup while typing
  (corfu-auto-delay 0.2)
  (corfu-auto-prefix 2)       ; after two characters
  (corfu-cycle t)
  :init (global-corfu-mode 1))

(setq tab-always-indent 'complete)   ; TAB indents, then completes

Corfu is small and uses the standard capf machinery. Company (company-mode) is the older, larger alternative with its own backends. Both work with Eglot. Pair either with Orderless (see minibuffer completion) for flexible matching, and with Cape to add extra sources, for example (add-hook 'completion-at-point-functions #'cape-file).

Built in and always available: M-/ (dabbrev-expand) completes the word before point from words already in your open buffers. It is a surprisingly good default for prose and code alike; repeat M-/ to cycle through candidates.

Snippets

A snippet is a template with fields you fill in by pressing TAB. Two popular packages are YASnippet (a large collection of ready-made snippets, yasnippet-snippets) and Tempel (lighter, templates written in Lisp data). A YASnippet template is a text file:

Text
# -*- mode: snippet -*-
# name: for loop
# key: for
# --
for (int ${1:i} = 0; $1 < ${2:n}; $1++) {
    $0
}

Type for and press TAB: the template expands, the cursor lands on i (field 1, with default text), typing replaces it and mirrors into the other $1 occurrences, TAB moves to field 2, and $0 marks the final cursor position.

ELISP
(use-package yasnippet
  :ensure t
  :config
  (yas-global-mode 1)
  (setq yas-snippet-dirs (list (expand-file-name "snippets" user-emacs-directory))))
(use-package yasnippet-snippets :ensure t :after yasnippet)

Create your own snippet with M-x yas-new-snippet, save with C-c C-c, and insert with M-x yas-insert-snippet or by typing the key then TAB.

JS Stand-in: snippet fields and mirrors

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When programming languages are involved, Eglot can also expand LSP-provided snippets; install YASnippet so those work with tab stops.

Diagnostics with Flymake

Flymake is the built-in on-the-fly checker. It runs checks (a syntax checker, a linter, or the language server through Eglot) and underlines problems: errors in red, warnings in yellow, notes in green. Turn it on for programming buffers:

ELISP
(add-hook 'prog-mode-hook #'flymake-mode)
(with-eval-after-load 'flymake
  (keymap-set flymake-mode-map "M-n" #'flymake-goto-next-error)
  (keymap-set flymake-mode-map "M-p" #'flymake-goto-prev-error))

Useful commands:

  • M-x flymake-show-buffer-diagnostics lists every problem in the buffer; flymake-show-project-diagnostics covers the project.
  • flymake-goto-next-error / flymake-goto-prev-error jump between problems.
  • Hovering with the mouse, or eldoc in the echo area, shows the message for the problem at point.
  • The mode line indicator shows counts, such as [2 1 0] for errors, warnings and notes.

Flycheck is a popular alternative with many ready-made checkers. Choose one system per buffer. In Elisp buffers Flymake already works out of the box with the byte compiler.

JS Stand-in: collecting and sorting diagnostics

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Formatting on save

Formatting is part of the same ecosystem. With Eglot, M-x eglot-format-buffer asks the server to format. To format on every save for languages with a formatter:

ELISP
(add-hook 'before-save-hook
          (lambda () (when (bound-and-true-p eglot--managed-mode) (eglot-format-buffer))))

Packages such as apheleia run external formatters (black, prettier, gofmt) asynchronously and keep the cursor in place. Pick one approach per language so two formatters never fight.

Many packages named here are third-party and their option names change between releases. If a variable below is not found in your version, check its documentation with C-h v after loading the package.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does completion-at-point-functions contain?

    Each capf is asked for candidates; the first one that answers provides them.
  2. 2. In a YASnippet template, what does $0 mean?

    $0 marks where the cursor ends after you have visited all other fields.
  3. 3. Which command lists all problems found by Flymake in the current buffer?

    flymake-show-buffer-diagnostics opens a list of the buffer's diagnostics.
  4. 4. What does M-/ (dabbrev-expand) do?

    Dynamic abbrev expansion looks for matching words in your buffers; repeat to cycle.

Summary

  • completion-at-point collects candidates from capfs; Corfu or Company turn it into an automatic popup.
  • M-/ is a built-in word completer that needs no setup.
  • Snippets (YASnippet, Tempel) expand templates with tab stops, defaults and mirrors; $0 is the end point.
  • Flymake underlines problems and lists them; Eglot feeds it language-server diagnostics.
  • Use one formatter per language and format on save with a hook.

Next: Magit: Git Inside Emacs