:title: Treesitter :date: 2026-01-18 :tags: :identifier: 20260118T133600 :signature: 5=10 Treesitter ========== .. seealso:: `How To Get Started with Tree Sitter `__ Article on Matering Emacs `Combobulate: Structured Movement and Editing with Tree-Sitter `__ Another introducing the ``combobulate`` package As of version ``29.1`` Emacs comes with support out of the box (assuming it has been compiled with the ``--with-tree-sitter`` flag) .. code-block:: elisp :project: emacs :filename: init.el (use-package alc-treesitter :load-path "lisp" :if (treesit-available-p)) Installing Grammars ^^^^^^^^^^^^^^^^^^^ While Emacs supports the tree-sitter library itself, it does not come with any grammars which need to be installed separately. Thankfully, Emacs does provide a few utilities to help with this .. code-block:: elisp :project: emacs :template: elisp-module :filename: lisp/alc-treesitter.el (setq treesit-language-source-alist '((python "https://github.com/tree-sitter/tree-sitter-python" "v0.20.4") (typescript "https://github.com/tree-sitter/tree-sitter-typescript" "v0.20.3" "typescript/src"))) The ``treesit-language-source-alist`` variable tells Emacs where it can find a given grammar and what language it is for. Once defined (and assuming you have the necessary dependencies) you can use ``M-x treesit-install-language-grammar`` command to install one of the grammars you defined. Alternatively this snippet will install them all in one go .. code-block:: elisp (mapc #'treesit-install-language-grammar (mapcar #'car treesit-language-source-alist)) Major Modes ^^^^^^^^^^^ Switching to tree-sitter powered major modes by default would not be backwards compatible, so if we actually want to make use of the tree-sitter support we need to opt into it. .. code-block:: elisp :project: emacs :filename: lisp/alc-treesitter.el (if (treesit-language-available-p 'python) (add-to-list 'major-mode-remap-alist '(python-mode . python-ts-mode))) Combobulate ^^^^^^^^^^^ :gh:`mickeynp/combobulate` provides a nice set of movement and editing commands that leverage the parse tree generated by tree sitter. .. code-block:: elisp :project: emacs :filename: lisp/alc-treesitter.el (use-package combobulate :vc (:url "https://github.com/mickeynp/combobulate" :rev "master") :hook ((python-ts-mode . combobulate-mode) (typescript-ts-mode . combobulate-mode)))