Navigating large source files containing thousands of lines of code with Emacs makes it difficult to perceive the underlying structure. For a software engineer spending the majority of the day reading and writing code, reliable folding is a requirement for maintaining focus and managing complexity.
Before we dive in, please consider sharing this article on your website/blog, Mastodon, Reddit, X, or your preferred social media platforms. Sharing it will help fellow Emacs users discover better ways to manage code folding.
In this article, we explore:
- A folding Frontend: Consolidating folding commands into a single, predictable keymap that operates consistently across all code folding modes.
- Folding Backends: Ready-to-use hooks to activate the most effective folding backend for the following major modes: C, C++, Java, Rust, Go, Python, JavaScript, TypeScript, Emacs Lisp, shell scripts, Lua, Haskell, YAML, Org-mode, Markdown…
- Editor Integration: Using indirect buffers to maintain independent folding states, configuring search operations to strictly ignore folded text, and setting up display-line-numbers-mode…
- Discouraged Folding Engines: A review of legacy or poorly performing packages to avoid.

Why code folding?
Code folding is about managing cognitive load, preserving spatial memory, and controlling screen real estate:
- Navigating through code (e.g., with LSP) can create a vacuum of context. Folding an entire file to its top-level headings allows the manipulation of the file skeleton directly in the main buffer. Revealing only a specific entry and its parents provides an immediate understanding of the hierarchy without losing position.
- When tasked with debugging a 20,000 line legacy file, immediate refactoring is rarely an option. Folding enables the visual modularization of massive files on the fly, making hostile codebases readable.
- Every visible line of code on the screen requires a fraction of subconscious attention to ignore. During debugging sessions, folding adjacent functions or complex implementations acts as a visual garbage collector.
- Moving or deleting a massive function or block is prone to selection errors. When a block is folded, it behaves as a single logical unit that can be cut, copied, or moved safely and cleanly.
- Folding is effective for tracking progress during extensive pull requests. Collapsing previously examined functions or blocks actively filters out visual noise.
Code Folding Frontend
The primary drawback of code folding modes is inconsistency. For example, hs-minor-mode and outline-minor-mode use entirely different functions and keybindings to perform the exact same logical action.
The solution is a package called kirigami, which acts as a universal frontend for text folding. You define your keybindings once, and kirigami automatically detects the active folding and routes your commands to the appropriate engine, whether that is outline-minor-mode, outline-indent-minor-mode, org-mode, markdown-mode, gfm-mode, treesit-fold-mode, hs-minor-mode (hideshow), and many others…
To install and configure kirigami, add the following code to your Emacs init file:
(use-package kirigami
:commands (kirigami-open-fold
kirigami-open-fold-rec
kirigami-close-fold
kirigami-toggle-fold
kirigami-open-folds
kirigami-close-folds-except-current
kirigami-close-folds)
:bind
;; Vanilla Emacs keybindings
(("C-c z o" . kirigami-open-fold) ; Open fold at point
("C-c z O" . kirigami-open-fold-rec) ; Open fold recursively
("C-c z r" . kirigami-open-folds) ; Open all folds
("C-c z c" . kirigami-close-fold) ; Close fold at point
("C-c z m" . kirigami-close-folds) ; Close all folds
("C-c z a" . kirigami-toggle-fold))) ; Toggle fold at pointCode language: Lisp (lisp)
If you are an evil-mode user, add the following keybindings to your init file:
;; Uncomment the following if you are an `evil-mode' user:
(with-eval-after-load 'evil
(define-key evil-normal-state-map "zo" #'kirigami-open-fold)
(define-key evil-normal-state-map "zO" #'kirigami-open-fold-rec)
(define-key evil-normal-state-map "zc" #'kirigami-close-fold)
(define-key evil-normal-state-map "za" #'kirigami-toggle-fold)
(define-key evil-normal-state-map "zr" #'kirigami-open-folds)
(define-key evil-normal-state-map "zm" #'kirigami-close-folds))Code language: Lisp (lisp)
In addition to providing a unified interface, the kirigami package enhances folding behavior in outline, markdown-mode, and org-mode packages. It ensures that deep folds and sibling folds open and close reliably.
Code Folding Backends
A code folding backend is the underlying engine that handles the logic of identifying and hiding specific blocks of text. While the kirigami package provides the user interface and keybindings, it requires a backend, such as outline-minor-mode or hs-minor-mode, to perform the folding.
NOTE: When configuring folding backends, ensure that only one folding minor mode is active concurrently in a single buffer, as conflicts and unexpected behavior may occur. For this reason, adding folding hooks to broad categories like prog-mode-hook or text-mode-hook is discouraged. Instead, hooks should be applied specifically to individual language modes, such as emacs-lisp-mode-hook.
Below are ready-to-use hooks to activate the optimal folding backend for each major mode:
Outline (built-in)
outline-minor-mode relies on hierarchical headings to determine collapsible sections. It is effective for structured text and is my default choice for Elisp, Lisp, Markdown, Diff, and configuration files.
(add-hook 'emacs-lisp-mode-hook #'outline-minor-mode)
(add-hook 'lisp-interaction-mode-hook #'hs-minor-mode) ; scratch
(add-hook 'lisp-mode-hook #'outline-minor-mode)
(add-hook 'conf-mode-hook #'outline-minor-mode)
(add-hook 'markdown-mode-hook #'outline-minor-mode)
(add-hook 'diff-mode-hook #'outline-minor-mode)Code language: Lisp (lisp)
Hideshow (built-in)
hs-minor-mode parses buffer syntax to accurately detect the start and end of blocks. It is the best tool for C-style languages, or anything using braces {} and explicit block structures like sh/Bash shell scripts.
;; Systems and General Purpose
(add-hook 'c-mode-hook #'hs-minor-mode)
(add-hook 'c++-mode-hook #'hs-minor-mode)
(add-hook 'java-mode-hook #'hs-minor-mode)
(add-hook 'rust-mode-hook #'hs-minor-mode)
(add-hook 'go-mode-hook #'hs-minor-mode)
(add-hook 'ruby-mode-hook #'hs-minor-mode)
(add-hook 'php-mode-hook #'hs-minor-mode)
(add-hook 'perl-mode-hook #'hs-minor-mode)
;; Web and Frontend
(add-hook 'js-mode-hook #'hs-minor-mode)
(add-hook 'typescript-mode-hook #'hs-minor-mode)
(add-hook 'css-mode-hook #'hs-minor-mode)
;; Scripting, Data, and Infrastructure
(add-hook 'sh-mode-hook #'hs-minor-mode) ; for bash/shell scripts
(add-hook 'json-mode-hook #'hs-minor-mode)
(add-hook 'lua-mode-hook #'hs-minor-mode)
(add-hook 'nxml-mode-hook #'hs-minor-mode)
(add-hook 'html-mode-hook #'hs-minor-mode) ;; mhtml and htmlCode language: Lisp (lisp)
hs-minor-mode folds a single level at a time, such as entire functions, without providing convenient access to nested blocks. This makes it less practical for languages that require deep folding, such as YAML, where multiple nested levels are common. Even in languages like Python, Hideshow can be impractical, because it allows folding classes but does not provide convenient folding for the functions within those classes for example.
Outline-indent
The outline-indent package provides code folding based on indentation levels. It is recommended for Python, Haskell, and YAML because it supports an unlimited number of folding levels. For instance, it allows folding an entire function or specific nested blocks within that function, such as if statements inside while loops.
(use-package outline-indent
:commands outline-indent-minor-mode
:custom
(outline-indent-ellipsis " ▼"))
;; Python
(add-hook 'python-mode-hook #'outline-indent-minor-mode)
(add-hook 'python-ts-mode-hook #'outline-indent-minor-mode)
;; Yaml
(add-hook 'yaml-mode-hook #'outline-indent-minor-mode)
(add-hook 'yaml-ts-mode-hook #'outline-indent-minor-mode)
;; Haskell
(add-hook 'haskell-mode-hook #'outline-indent-minor-mode)Code language: Lisp (lisp)
In addition to code folding, outline-indent allows moving indented blocks up and down, indenting/unindenting to adjust indentation levels, inserting a new line with the same indentation level as the current line, and moving backward/forward to the indentation level of the current line.
Treesit-fold
The treesit-fold package provides Intelligent code folding by using the structural understanding of the built-in tree-sitter parser. Unlike traditional folding methods that rely on regular expressions or indentation, treesit-fold uses the actual syntax tree of the code to accurately identify foldable regions such as functions, classes, comments, and documentation strings.
(use-package treesit-fold
:commands (treesit-fold-close
treesit-fold-close-all
treesit-fold-open
treesit-fold-toggle
treesit-fold-open-all
treesit-fold-mode
global-treesit-fold-mode
treesit-fold-open-recursively
treesit-fold-line-comment-mode)
:custom
(treesit-fold-line-count-show t)
(treesit-fold-line-count-format " ▼")
:config
(set-face-attribute 'treesit-fold-replacement-face nil
:foreground "#808080"
:box nil
:weight 'bold))
;; Systems and General Purpose
(add-hook 'c-ts-mode-hook #'treesit-fold-mode)
(add-hook 'c++-ts-mode-hook #'treesit-fold-mode)
(add-hook 'java-ts-mode-hook #'treesit-fold-mode)
(add-hook 'rust-ts-mode-hook #'treesit-fold-mode)
(add-hook 'go-ts-mode-hook #'treesit-fold-mode)
(add-hook 'ruby-ts-mode-hook #'treesit-fold-mode)
(add-hook 'php-ts-mode-hook #'treesit-fold-mode)
(add-hook 'csharp-ts-mode-hook #'treesit-fold-mode)
(add-hook 'go-mod-ts-mode-hook #'treesit-fold-mode)
(add-hook 'lua-ts-mode-hook #'treesit-fold-mode)
;; Web and Frontend
(add-hook 'js-ts-mode-hook #'treesit-fold-mode)
(add-hook 'typescript-ts-mode-hook #'treesit-fold-mode)
(add-hook 'tsx-ts-mode-hook #'treesit-fold-mode)
(add-hook 'css-ts-mode-hook #'treesit-fold-mode)
(add-hook 'html-ts-mode-hook #'treesit-fold-mode)
(add-hook 'heex-ts-mode-hook #'treesit-fold-mode)
(add-hook 'xml-ts-mode-hook #'treesit-fold-mode)
;; Scripting and Infrastructure
(add-hook 'bash-ts-mode-hook #'treesit-fold-mode)
(add-hook 'cmake-ts-mode-hook #'treesit-fold-mode)
(add-hook 'dockerfile-ts-mode-hook #'treesit-fold-mode)
(add-hook 'awk-ts-mode-hook #'treesit-fold-mode)
(add-hook 'vimscript-ts-mode-hook #'treesit-fold-mode)
(add-hook 'nix-ts-mode-hook #'treesit-fold-mode)
;; Data and Configuration
(add-hook 'json-ts-mode-hook #'treesit-fold-mode)
(add-hook 'toml-ts-mode-hook #'treesit-fold-mode)
;; Build Systems and Makefiles
(add-hook 'makefile-ts-mode-hook #'treesit-fold-mode)
;; Hardware Description and Shaders
(add-hook 'verilog-ts-mode-hook #'treesit-fold-mode)
(add-hook 'vhdl-ts-mode-hook #'treesit-fold-mode)
(add-hook 'hlsl-ts-mode-hook #'treesit-fold-mode)
;; Scientific, Data Science, and Academic
(add-hook 'latex-ts-mode-hook #'treesit-fold-mode)
(add-hook 'beancount-ts-mode-hook #'treesit-fold-mode)
;; Documentation and Diagrams
(add-hook 'markdown-ts-mode-hook #'treesit-fold-mode)
(add-hook 'mermaid-ts-mode-hook #'treesit-fold-mode)
;; Other
(add-hook 'gdscript-ts-mode-hook #'treesit-fold-mode)
(add-hook 'clojure-ts-mode-hook #'treesit-fold-mode)
(add-hook 'caml-ts-mode-hook #'treesit-fold-mode)
(add-hook 'ocaml-ts-mode-hook #'treesit-fold-mode)
(add-hook 'erlang-ts-mode-hook #'treesit-fold-mode)
(add-hook 'elixir-ts-mode-hook #'treesit-fold-mode)
(add-hook 'scala-ts-mode-hook #'treesit-fold-mode)
(add-hook 'dart-ts-mode-hook #'treesit-fold-mode)
(add-hook 'haskell-ts-mode-hook #'treesit-fold-mode)
(add-hook 'julia-ts-mode-hook #'treesit-fold-mode)
(add-hook 'kotlin-ts-mode-hook #'treesit-fold-mode)
(add-hook 'gleam-ts-mode-hook #'treesit-fold-mode)
(add-hook 'noir-ts-mode-hook #'treesit-fold-mode)
(add-hook 'swift-ts-mode-hook #'treesit-fold-mode)
(add-hook 'zig-ts-mode-hook #'treesit-fold-mode)Code language: Lisp (lisp)
For the treesit-fold block to function, you must be using Emacs 29.1 or newer, and you must have the actual Tree-sitter grammars installed on your machine for those specific languages.
Markdown-mode
The markdown-mode package provides a major mode for syntax highlighting, editing commands, and preview support for Markdown documents. It supports core Markdown syntax as well as extensions like GitHub Flavored Markdown (GFM). Markdown-mode and gfm-mode support outline-minor-mode folding.
(use-package markdown-mode
:commands (gfm-mode
gfm-view-mode
markdown-mode
markdown-view-mode)
:mode (("\\.markdown\\'" . markdown-mode)
("\\.md\\'" . markdown-mode)
("README\\.md\\'" . gfm-mode))
:bind
(:map markdown-mode-map
("C-c C-e" . markdown-do)))
;; Hooks
(add-hook 'markdown-mode-hook #'outline-minor-mode)Code language: Lisp (lisp)
Maintaining independent folding states in separate windows via indirect buffers (clones)
Opening the same buffer in multiple windows results in synchronized folding states; any folding or unfolding action performed in one window is immediately reflected in all others.
This occurs because folding engines use buffer-local overlays, which are shared across all windows associated with that specific buffer.
Indirect buffers provide a robust solution to this limitation. An indirect buffer shares the underlying text of its parent buffer but maintains an independent set of overlays. This distinction allows for the maintenance of different folding configurations for the same file simultaneously.
To create an indirect buffer (clone) of the current buffer in a separate window, execute:
M-x clone-indirect-buffer-other-windowCode language: plaintext (plaintext)
Creating an indirect buffer provides a separate buffer object that references the same text while maintaining its own isolated set of opened/closed folds.
Persisting and restoring folding state across sessions with savefold
By default, Emacs does not preserve the state of your folds when you close a buffer or exit the editor. The savefold package resolves this limitation. It records the positions of opened and closed folds to disk, automatically restoring them when a file is reopened.
The package integrates with most overlay-based folding frameworks. Currently supported systems include outline, org-mode, markdown-mode, hideshow, treesit-fold, ts-fold, etc.
To install and activate savefold globally using use-package, add the following snippet to your initialization file.
(use-package savefold
:init
;; list of symbols indicating active backends. Default: '(outline)
(setq savefold-backends '(outline org hideshow treesit-fold markdown))
;; The directory path where the serialization files are stored.
(setq savefold-directory (locate-user-emacs-file "savefold"))
;; When using `savefold' alongside `org-mode', configure the default Org startup
;; visibility to ensure that the saved state applies correctly without
;; conflicting with internal Org visibility cycles:
(setq org-startup-folded 'showeverything)
:config
(savefold-mode 1))Code language: Lisp (lisp)
Preventing Emacs search from matching text within folded blocks
Note: This setting is for users who want search operations to ignore folded blocks instead of expanding them. This behavior is subjective and may not suit every workflow.
By default, search operations can match text within folded blocks, which often causes Emacs to automatically expand the hidden content.
To instruct Emacs to strictly ignore invisible text during search operations, add the following configuration to your init file:
(setq-default search-invisible nil)Code language: Lisp (lisp)
Alternatively, to restrict this behavior to specific modes, apply a buffer-local configuration via a mode hook:
(add-hook 'prog-mode-hook (lambda ()
(setq-local search-invisible nil)))Code language: Lisp (lisp)
Integrating display-line-numbers-mode with code folding
The built-in display-line-numbers-mode renders line numbers in the side margin of the window. By default, it uses absolute line numbering, which tracks the absolute line count in the buffer. Consequently, when a block is folded, the line numbers skip the hidden range (e.g., jumping from 15 to 120).
For users who prefer visual line numbering, display-line-numbers-mode can be configured to ignore collapsed content and assign numbers sequentially based only on what is currently rendered on the screen.
To implement visual line numbering as your global default, set the following variable in your configuration:
(setq-default display-line-numbers-type 'visual)Code language: Lisp (lisp)
(Note that you must still enable the mode itself using M-x global-display-line-numbers-mode for the line numbers to appear.)
Discouraged Emacs Folding Engines
Choosing an appropriate folding engine is important for maintaining performance and stability within Emacs. While several third-party and legacy options exist, the following packages and methods are generally discouraged in favor of more modern or integrated alternatives:
- Origami: This package is slow and largely unmaintained. Origami uses a non-standard API and a complex implementation that frequently conflicts with other overlay-based minor modes. Its overhead can lead to performance degradation, especially when handling large buffers or deeply nested code. (Modern alternatives to origami: outline-indent, treesit-fold, outline-minor-mode, hs-minor-mode)
- Yafolding: This package is also unmaintained and suffers from performance issues. (Modern alternative to yafolding: outline-indent)
- Semantic (CEDET): Part of the legacy CEDET suite, Semantic folding is widely regarded as heavyweight. The parsing overhead required for its operation often introduces noticeable latency, making it vastly less efficient than modern built-in alternatives like Tree-sitter. (Modern alternatives to CEDET code folding: treesit-fold, outline-minor-mode, hs-minor-mode, outline-indent)
- Selective Display (
set-selective-display): This is Emacs’ oldest built-in folding method (often bound toC-x $). It causes unpredictable cursor jumping, and lacks any contextual awareness. - Folding-mode: This ancient package relies on explicit structural markers placed manually inside code comments (e.g.,
{{{and}}}). While robust for the user, markers pollute the source code with editor-specific metadata. This is heavily frowned upon in modern collaborative environments where team members use varying IDEs. - Vimish-fold: Although useful for manual, ad-hoc text folding, vimish-fold is not recommended as a primary automated folding engine. Unlike Vim’s
set foldmethod=marker, the vimish-fold implementation does not support recursive markers, such as{{{inside of{{{. Additionally, like folding-mode, vimish-fold also uses markers that pollute the source code with editor-specific markers, a practice discouraged in collaborative environments where team members use a variety of editors and IDEs.
Conclusion
Establishing a unified folding interface in Emacs converts a buffer into a structured environment. Whether you are refactoring complex Python classes or navigating extensive Org documents, relying on a standardized command set simplifies the experience. Integrating the hooks outlined in this article ensures you enable the optimal backend for each major mode, allowing you to focus on logic rather than editor mechanics.