Buffer Yard
Language setup

Setting Up Emacs for Your Programming Language: Eleven Guides

Updated

Open a Go file in a fresh Emacs and you get syntax colours and not much else. Open the same file after adding six lines to your configuration and you get completion from the compiler’s own view of the code, errors underlined as you type, jump-to-definition across the module and formatting on save. The difference is almost never a big package. It is a language server and a hook.

Every guide in this section starts from the starter configuration and adds only what one language needs. That keeps each page short and lets you combine them: a web developer will read the JavaScript and the HTML and CSS pages, somebody maintaining a C library with Python bindings will want both of those, and none of the snippets fight each other.

The same four questions for every language

Each page answers the same questions, in whatever order makes sense for that language.

Which major mode? Emacs decides how to edit a file by its major mode. For many languages there are now two: the classic one, written with regular expressions, and a tree-sitter variant whose name ends in -ts-mode. The tree-sitter modes indent more reliably and highlight more precisely, at the cost of installing a grammar once.

Which language server? A language server is a separate program, usually maintained next to the compiler or by the language community, that understands your project: pyright or basedpyright for Python, gopls for Go, rust-analyzer for Rust, clangd for C, haskell-language-server for Haskell. You install it with the language’s own tools, then tell eglot when to start it.

How is code formatted? Most languages have one formatter everyone agrees on (gofmt, rustfmt, mix format, ruff or black). Either the language server formats on request, or a small package such as apheleia runs the formatter on save without moving your cursor.

How do you run it? A REPL, a test runner, a compile command. Emacs is at its best when the edit, run, read-the-error loop never leaves the editor, and every guide shows the shortest path to that loop for its language.

One hook, many languages

Because the pattern repeats, many readers end up with a single eglot block covering every language they use:

(use-package eglot
  :ensure nil
  :hook ((python-ts-mode go-ts-mode rust-ts-mode c-ts-mode js-ts-mode) . eglot-ensure)
  :custom
  (eglot-autoshutdown t))

eglot-autoshutdown stops a server when you close the last buffer that used it, which keeps memory in check on laptops. If a server needs special arguments, eglot-server-programs is where they go; the individual guides show it where a language needs it.

Where to start

If you write one language most of the day, set that one up first and leave the others alone. Python, JavaScript and Go have the smoothest path today: their servers install with one command and work with zero project configuration. Haskell and C ask a little more of you, Haskell because of toolchain management and C because clangd needs to know how your project compiles. Racket stands apart: its mode by Greg Hendershott brings its own REPL integration and does not depend on a language server at all.

Every one of these setups also benefits from a good completion framework, since eglot hands its candidates to whatever completion UI you run, and from knowing how use-package loads packages.

A last practical point. When a server misbehaves, M-x eglot-events-buffer shows the raw conversation between Emacs and the server. It looks intimidating, but the first error in that buffer usually names the missing binary or the wrong path in plain words, which is faster than any forum search.

Questions readers ask

Do I need a separate package for every language?

Usually one or two. Emacs 29 and later ship tree-sitter modes for many languages and the eglot client for language servers, so a typical language block installs at most a major mode from MELPA and points eglot at a server you installed outside Emacs.

Why eglot and not lsp-mode?

eglot is built in since Emacs 29.1, needs almost no configuration and uses Emacs's own completion, diagnostics (Flymake) and cross-reference commands. lsp-mode does more out of the box, with breadcrumbs and a UI layer, and some people prefer it for that. Everything on these pages works with either; the snippets use eglot.

What are the -ts-mode major modes?

Modes such as python-ts-mode or rust-ts-mode parse the buffer with tree-sitter instead of regular expressions, which gives more accurate highlighting and indentation. They need the matching grammar installed; M-x treesit-install-language-grammar fetches and compiles one.