How to Get Tree-sitter to Work in Emacs (29 and Above) in 6 Minutes

Watch the Video Here

Steps

  • Install Tree-sitter into Linux
  • Install Tree-sitter development libraries: sudo apt install libtree-sitter-dev
  • Compile Emacs 29+ and configure with tree-sitter support --with-tree-sitter

In Emacs

  • Set where to look for grammar libraries:

(setq treesit-extra-load-path '("~/.emacs.d/tree-sitter"))

Install Language Grammars

M-x treesit-install-language-grammar

List of Github Repos

Remap modes to tree-sitter modes

(setq major-mode-remap-alist
 '((python-mode . python-ts-mode)
   (sh-mode . bash-ts-mode)
   (json-mode . json-ts-mode)
   (yaml-mode . yaml-ts-mode)))

Set a maximum tree-sitter font-lock level for richer syntax highlighting

(customize-set-variable 'treesit-font-lock-level 3)

Ensure any python-mode hooks also run in python-ts-mode

(add-hook 'python-ts-mode-hook
          (lambda ()
            (run-hooks 'python-mode-hook)))

Test that tree-sitter is working

M-: major-mode

Resources