How to Get Tree-sitter to Work in Emacs (29 and Above) in 6 Minutes
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
bash "https://github.com/tree-sitter/tree-sitter-bash"
cmake "https://github.com/uyha/tree-sitter-cmake"
json "https://github.com/tree-sitter/tree-sitter-json"
make "https://github.com/alemuller/tree-sitter-make"
python "https://github.com/tree-sitter/tree-sitter-python"
yaml "https://github.com/ikatyang/tree-sitter-yaml"))
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
https://www.masteringemacs.org/article/how-to-get-started-tree-sitter
https://github.com/emacs-mirror/emacs/blob/master/admin/notes/tree-sitter/starter-guide
https://lists.gnu.org/archive/html/help-emacs-windows/2023-09/msg00000.html
https://archive.casouri.cc/note/2023/tree-sitter-in-emacs-29/index.html
https://www.reddit.com/r/emacs/comments/16ey3hq/is_treesitter_worth_it/