Emacs: buffer-terminator.el – Safely Terminate Emacs Buffers Automatically

The buffer-terminator Emacs package automatically kills buffers to help maintain a clean and efficient workspace, while also improving Emacs’ performance by reducing the number of open buffers, thereby decreasing the number of active modes, timers, and other processes associated with those buffers.

Activating (buffer-terminator-mode) terminates all buffers that have been inactive for longer than the duration specified by buffer-terminator-inactivity-timeout (default: 30 minutes). It checks every buffer-terminator-interval (default: 10 minutes) to determine if a buffer should be terminated.

The following buffers are not terminated by default:

  • Special buffers (buffers whose names start with a space, start and end with *, or whose major mode is derived from special-mode).
  • Modified file-visiting buffers that have not been saved; the user must save them first.
  • Buffers currently displayed in any visible window.
  • Buffers associated with running processes.

(The default parameters above are fully customizable. Users can define specific rules for keeping or terminating certain buffers by specifying a set of rules using buffer-terminator-rules-alist. These rules can include buffer name patterns or regular expressions, major-modes, buffer properties, etc.)

Installation from MELPA

MELPA

To install buffer-terminator from MELPA:

  1. If you haven’t already done so, add MELPA repository to your Emacs configuration.
  2. Add the following code to the Emacs init file:
(use-package buffer-terminator
  :ensure t
  :custom
  (buffer-terminator-verbose nil)
  :config
  (buffer-terminator-mode 1))Code language: CSS (css)

Customizations

To enable verbose mode to log buffer cleanup events:

(setq buffer-terminator-verbose t)Code language: Lisp (lisp)

To set the inactivity timeout (in seconds) after which buffers are considered inactive (default is 30 minutes):

(setq buffer-terminator-inactivity-timeout (* 30 60)) ; 30 minutesCode language: Lisp (lisp)

To define how frequently the cleanup process should run (default is every 10 minutes):

(customize-set-variable 'buffer-terminator-interval (* 10 60)) ; 10 minutesCode language: Lisp (lisp)

(Using customize-set-variable allows buffer-terminator-interval to update the timer dynamically, without the need to restart buffer-terminator-mode.)

There are other customizable options available in the buffer-terminator.el README.md file.

Links

The compile-angel Emacs package: Byte-compile and Native-compile Emacs Lisp libraries Automatically

The compile-angel Emacs package automatically byte-compiles and native-compiles Emacs Lisp libraries. It offers:

  • (compile-angel-on-load-mode): A global mode that compiles .el files before they are loaded.
  • (compile-angel-on-save-local-mode): A local mode that compiles .el files whenever the user saves them.

The compile-angel modes speed up Emacs by ensuring all libraries are byte-compiled and native-compiled. Byte-compilation reduces the overhead of loading Emacs Lisp code at runtime, while native compilation optimizes performance by generating machine code specific to your system.

Why use compile-angel?

Because you are likely running a significant amount of interpreted, slow Elisp code. Ensuring that Elisp is native-compiled significantly improves Emacs’ performance. This is because functions like package-install and package-recompile-all do not compile .el files in the load-path paths that were not installed using package.el. Since these files are not byte-compiled, the Emacs JIT compiler does not native-compile them either, as a byte-compiled file signals the JIT compiler to perform native compilation. In contrast, compile-angel modes ensure that all loaded .el files are compiled transparently, regardless of whether they are part of a package.

Install compile-angel from MELPA

  1. If you haven’t already done so, add MELPA repository to your Emacs configuration.
  2. Add the following code at the very beginning of your Emacs init file, before all other packages:
(use-package compile-angel
  :ensure t
  :demand t
  :custom
  (compile-angel-verbose nil)
  :config
  (compile-angel-on-load-mode)
  (add-hook 'emacs-lisp-mode-hook 
            #'compile-angel-on-save-local-mode)) Code language: Lisp (lisp)

Links

Essential Emacs Packages for Efficient Software Development and General Text Editing

In the pursuit of an optimized Emacs setup, I focused on enhancing defaults and minimizing the number of installed packages to maintain simplicity and efficiency:

  • The initial step involved creating minimal-emacs.d, a project that has resonated with the Emacs community, providing a foundational template for many users’ init.el and early-init.el vanilla Emacs configuration.
  • Next, I experimented with hundreds of Emacs packages, carefully selecting the most valuable ones that, ideally, leverage built-in Emacs functions. (This is why I chose corfu over company, eglot over lsp-mode, and flymake over flycheck, etc.)

In this article, I will share the Emacs packages I use daily for software development and general text editing. Please share in the comments the Emacs packages you are using!

Where can I find the third-party packages listed below?

The following Emacs packages installed whether from MELPA or ELPA.

Category: Code completion

  • corfu: A completion framework that integrates with the built-in completion system in Emacs. For example, it can complete Python code when using the eglot package and a Python language server such as Pylsp.
  • prescient: Provides smart completion suggestions based on history and context. For example, it can enable fuzzy completion with Corfu/Cape or anticipate your next input based on previous selections.
  • cape: A completion-at-point extension for various completion frameworks in Emacs, enhancing Corfu.
  • nerd-icons-corfu: Integrates Nerd Icons with the Corfu completion framework, enhancing the appearance of completion candidates.

Category: Software development (General)

  • eglot (built-in): An LSP client that provides features like code completion, diagnostics, formatting, and more, powered by language servers. For example, it can be used to add Python code completion using the language server Pylsp. There are many language servers available for many other programming languages.
  • outline-indent: Enables code folding based on indentation levels. This package is useful for editing indentation-based text files, such as YAML, Python, and other indented text files.
  • treesit (built-in): This package provides a way to work with tree-sitter, a syntax code parser that performs syntax highlighting, code navigation, and structural editing across various programming languages.
  • highlight-symbol: Highlight occurrences of symbols in your code. (The author is now using built-in functions to toggle symbol highlighting in Emacs )
  • reformatter: Define commands which run reformatters on the current Emacs buffer. (Good alternative to this package: apheleia)
  • flymake (built-in): An on-the-fly syntax checking system that works well with eglot.
  • indent-bars: Provides indentation guide-bars.
  • paren (built-in): Matching parenthesis highlighting. (Modes: show-paren-mode or show-paren-local-mode).
  • ws-butler: Automatically trim extraneous white-space only in edited lines.
  • yasnippet: A template system for Emacs that allows for easy insertion of code snippets, improving coding efficiency. (The author is also using ultyas to share the same code snippets in Emacs and Vim)
  • dtrt-indent: Automatically adjusts indentation based on the surrounding context in code files, improving code readability.

Category: Better minibuffer

  • consult: Provides intelligent search and navigation commands, powered by the Emacs completion function, completing-read.
  • consult-dir: insert paths into minibuffer prompts in Emacs. Similar to fasd.
  • vertico: minimalistic vertical completion UI. (EDIT: There is also built-in alternative to vertico: fido-vertical-mode, but the author prefers vertico because it provides more features.)
  • marginalia: Enhances the display of completion candidates in the minibuffer.
  • embark: Enhances minibuffer completion and interaction with various Emacs commands and actions.

Category: Session management / persist and restore

  • easysession: A lightweight Emacs session manager that can persist and restore file editing buffers, indirect buffers/clones, Dired buffers, the tab-bar, and the Emacs frames (with or without the Emacs frames size, width, and height).
  • saveplace (built-in): Persist and restore your current cursor position.
  • savehist (built-in): Persist and restore your Emacs command history.

Category: Themes

  • ef-themes: A collection of light and dark themes for GNU Emacs whose goal is to provide colorful themes.
  • modus-themes: Highly accessible themes for GNU Emacs, conforming with the highest standard for color contrast between background and foreground values (WCAG AAA).
  • doom-themes: An megapack of popular themes, including aesthetic extensions for popular packages (e.g., Tomorrow Night/Day, Solarized, Gruvbox…).
  • tomorrow-night-deepblue-theme: A blue color theme for Emacs inspired by the Tomorrow Night color scheme.

Category: Vim emulation

  • evil: An extensible vi layer for Emacs, providing a modal editing experience similar to Vim.
  • evil-collection: A collection of Emacs packages that integrate with Evil mode to provide consistent keybindings across multiple modes.
  • evil-snipe: Provides enhanced search and jump functionality for Evil mode.
  • evil-surround: Enhances text object handling in Evil mode, allowing for easier manipulation of surrounding characters.
  • vdiff: Provides a visual interface for comparing two versions of a file that is similar to the Vim editor.
  • vim-tab-bar: Provides a tab-bar interface reminiscent of Vim.

Category: Terminal Emulators

  • eat: A terminal emulator, written in Elisp.
  • vterm: A terminal emulator for Emacs, written in C.

Category: Undo/Redo

  • undo-fu: An advanced undo/redo system that enhances the default undo behavior in Emacs.
  • undo-fu-session: Integrates with undo-fu to provide session management for undo history.

Category: Elisp

  • easy-escape: Improve readability of escape characters.
  • aggressive-indent: Automatically keeps your code indented as you type. The author mainly uses this package with Elisp.
  • package-lint: Lints Emacs Lisp packages to ensure compliance with best practices and package standards.
  • compile-angel: Speed up Emacs by ensuring all libraries are byte-compiled and native-compiled. Byte-compilation reduces the overhead of loading Emacs Lisp code at runtime, while native compilation optimizes performance by generating machine code specific to your system.
  • helpful: An improved help system for Emacs.
  • Paredit: A minor mode that enforces balanced parentheses while editing Lisp code. (In addition to Paredit, the author uses enhanced-evil-paredit.)

Category: File Manager

  • dired (built-in): File manager. (EDIT: The author is also using the built-in modes dired-hide-details-mode and dired-omit-mode.)
  • nerd-icons-dired: Enhances Dired mode with icons from Nerd Fonts, improving file browsing.

Category: Other packages

  • buffer-terminator: A package that automatically and safely kills buffers to help maintain a clean and efficient workspace, while also improving Emacs’ performance by reducing the number of open buffers, thereby decreasing the number of active modes, timers, and other processes associated with those buffers.
  • expand-region: Expands the selected region in code, making it easier to select logical blocks of code or text.
  • inhibit-mouse: Disables mouse support within Emacs, encouraging keyboard-centric navigation and editing. This can be beneficial for some users who prefer a more traditional text editor experience.
  • project.el (built-in): A package for managing and navigating projects, providing utilities for project-based operations like searching, switching, and file management within defined project directories.
  • visual-fill-column: A package that allows wrapping lines at a specified fill-column width.
  • quick-sdcv: Bring the Stardict’s dictionary functionality directly into your Emacs workflow. This will turn Emacs into a dictionary.
  • fasd: Offers fast access to files and directories based on your history and usage patterns, optimizing file navigation.
  • dir-config: Automatically find and evaluate .dir-config.el Elisp files to configure directory-specific settings.
  • exec-path-from-shell: Ensures Emacs uses the same environment variables as your shell.
  • wgrep: Allows for in-buffer editing of grep results, improving the usability of search results. (It can be used to modify the occurrences returned by the Embark package embark-export function.)
  • flyspell-mode (built-in): An interface for spell-checking text using external programs like ispell, aspell, or hunspell for checking and correcting spelling in buffers. (EDIT: The author replaced Jinx with Flyspell because Jinx was slowing down Emacs.)
  • which-key (built-in): Displays available keybindings in a popup, helping users learn and remember key combinations in Emacs.

Category: Miscellaneous file types

  • diff-hl: Displays git diff information in the fringe of your Emacs window. (Alternative to diff-hl: git-gutter.)
  • hl-todo: highlight TODO, FIXME… (Here is a simple function that can highlight codetags such as TODO or FIXME)
  • org (built-in): A powerful mode for organizing notes, tasks, and project planning within Emacs.
  • org-appear: Improves the visibility of Org mode elements in the buffer by automatically toggling visibility based on context.
  • org-bullets: Replace org-mode leading stars with UTF-8 bullets (EDIT: The author is now using the org-ibullets fork)
  • toc-org: Automatically generates a table of contents for Org mode documents.
  • markdown-mode: Provides major mode support for editing Markdown files.
  • markdown-toc: Automatically generates and manages a table of contents for Markdown files, making navigation easier.
  • lua-mode: Provides major mode support for editing Lua files.
  • php-mode: Provides major mode support for editing PHP files. (EDIT: The author is now using the built-in php-ts-mode.)
  • dockerfile-mode: Provides syntax highlighting and editing support for Dockerfile files. (EDIT: The author is now using the built-in dockerfile-ts-mode.)
  • yaml-mode: Provides major mode support for editing YAML files, complete with syntax highlighting and formatting commands. (EDIT: The author is now using the built-in yaml-ts-mode.)
  • ansible-doc: Provides documentation lookup for Ansible modules.
  • flymake-ansible-lint: Provides on-the-fly syntax checking for Ansible playbooks and roles, ensuring code quality.
  • flymake-bashate: Integrates bashate for syntax checking of Bash scripts in real-time within Emacs. (Emacs also offers a built-in Flymake backend for ShellCheck.)

By focusing on these packages, the Emacs configuration was simplified while still maintaining a highly functional setup.

dir-config.el – Automatically find and evaluate .dir-config.el Elisp files to configure directory-specific settings

The dir-config.el Emacs package automatically loads and evaluates Elisp code from a .dir-config.el file found in the buffer’s current directory or its closest parent directory. This facilitates adjusting settings or executing functions specific to the directory structure of each buffer.

For instance, you can use the dir-config package to:

  • Configure project-specific settings: Automatically set up environment variables, keybindings, or modes unique to each project.
  • Apply directory-specific customizations: Set specific behaviors or preferences for files in different directories, such as enabling or disabling certain minor modes based on security considerations. For example, you might disable linters that execute code in directories where you handle untrusted code.
  • Manage multiple environments: Switch between different coding environments or workflows by loading environment-specific configurations.

Installation

To install the dir-config from MELPA:

  1. If you haven’t already done so, add MELPA repository to your Emacs configuration.
  2. Add the following code to your Emacs init file to install the dir-config repository from MELPA:
(use-package dir-config
  :ensure t
  :custom
  (dir-config-file-names '(".dir-config.el"))
  (dir-config-allowed-directories '("~/src" "~/projects"))
  :config
  (dir-config-mode))

More information about dir-config.el

easysession.el – Easily persist and restore your Emacs editing sessions

The easysession.el Emacs package is a lightweight session manager for Emacs that can persist and restore file editing buffers, indirect buffers (clones), Dired buffers, the tab-bar, and Emacs frames (including or excluding the frame size, width, and height). It offers a convenient and effortless way to manage Emacs editing sessions and utilizes built-in Emacs functions to persist and restore frames.

Key features include:

  • Minimalistic design focused on performance and simplicity, avoiding unnecessary complexity.
  • Persist and restore file editing buffers, indirect buffers/clones, dired buffers, tab-bar, the Emacs frame.
  • Automatically save sessions by activating the mode with easysession-save-mode to ensure that the current session is automatically saved every easysession-save-interval seconds and when emacs quits.
  • Helper functions: Switch to a session (i.e., load and change the current session) with easysession-switch-to, load the Emacs editing session with easysession-load, save the Emacs editing session with easysession-save and easysession-save-as, delete the current Emacs session with easysession-delete, and rename the current Emacs session with easysession-rename.

Installation

MELPA

The easysession package can be installed from MELPA by adding the following to your Emacs init file:

(use-package easysession
  :ensure t
  :custom
  (easysession-save-interval (* 10 60))
  :init
  (add-hook 'emacs-startup-hook #'easysession-load-including-geometry 98)
  (add-hook 'emacs-startup-hook #'easysession-save-mode 99))Code language: JavaScript (javascript)

Note that:

  • easysession-load-including-geometry is not needed after Emacs is loaded if you do not want EasySession to move or resize the Emacs frame when switching sessions. Instead, use easysession-switch-to or easysession-load to switch to another session or reload the current session without resizing or moving the Emacs frames.
  • The easysession-save-mode ensures that the current session is automatically saved every easysession-save-interval seconds and when emacs quits.
  • The easysession-save-interval variable determines the interval between automatic session saves. Setting it to nil disables timer-based autosaving, causing easysession-save-mode to save only when Emacs exits.

Links

Emacs: YAML file code Folding and Outlining

Recommendation: I recommend you to try a similar package from the same author: outline-indent.el, which provides a minor mode for Emacs that enables code folding and outlining based on indentation levels for various indent-based text files such as YAML, Python, and others. In addition to code folding, it allows moving indented subtrees up/down, customizing the ellipsis, and inserting a heading that creates a new line with the same indentation level/depth as the current line, among other features.

Working with long and complex YAML files can be a daunting task. In such cases, code folding becomes an invaluable tool, allowing the collapse and expansion of sections of the file and enhancing readability and navigation. This makes it easier to focus on specific parts of the code without being overwhelmed by the entire document.

To address this need, I would like to introduce you to outline-yaml.el, a minor mode for Emacs that brings code folding and outlining capabilities to YAML files, making editing YAML files more efficient and enjoyable. Leveraging Emacs’ built-in outline-minor-mode, outline-yaml.el provides a structured and navigable view of YAML content, simplifying the editing of even the most difficult YAML files.

Installation of outline-yaml.el

  1. Install and configure yaml-mode or yaml-ts-mode.
  2. If you haven’t already done so, add the straight.el bootstrap code to your init file.
  3. After that, add the following code to your Emacs init file:
(use-package outline-yaml
  :ensure t
  :straight (outline-yaml
             :type git
             :host github
             :repo "jamescherti/outline-yaml.el")
  :hook
  ((yaml-mode . outline-yaml-minor-mode)
   (yaml-ts-mode . outline-yaml-minor-mode)))Code language: Lisp (lisp)

How to change the Ellipsis (…) to (▼)?

The code snippet in this article can be used to to change the Ellipsis to ▼.

Links