stripspace.el - Ensure Emacs Automatically removes trailing whitespace before saving a buffer, with an option to preserve the cursor column
The stripspace Emacs package provides stripspace-local-mode
, which automatically removes trailing whitespace and blank lines at the end of the buffer when saving.
(Trailing whitespace refers to any spaces or tabs that appear at the end of a line, beyond the last non-whitespace character. These characters serve no purpose in the content of the file and can cause issues with version control, formatting, or code consistency. Removing trailing whitespace helps maintain clean, readable files.)
- Automatically removes trailing whitespace before saving buffers.
- An optional feature controlled by the
stripspace-restore-column
variable (enabled by default), which, when set to non-nil, preserves the cursor's column position even after stripping spaces. This is useful when extra spaces are added and the file is saved: - An optional feature
stripspace-only-if-initially-clean
(disabled by default), which, when set to non-nil, instructs stripspace to only delete whitespace when the buffer is clean initially. - The
stripspace-verbose
variable, when non-nil, shows in the minibuffer whether trailing whitespaces have been removed or, if not, provides the reason for their retention. - The
(stripspace-clean)
function forces the deletion of trailing whitespace in the current buffer. When thestripspace-only-if-initially-clean
variable is non-nil, this function also marks the buffer as clean, ensuring thatstripspace-local-mode
will remove trailing whitespace the next time the buffer is saved. The function that checks if the buffer is clean is optimized by performing a single regex search for trailing whitespace and another for blank lines.- Saved file: Removes all trailing whitespace.
- Currently edited file (buffer): Removes all trailing whitespace but preserves the cursor's column position on the current line, including any spaces before the cursor. This ensures a consistent editing experience and prevents unintended cursor movement when saving a buffer and removing trailing whitespace.
- The functions for deleting whitespace are customizable, allowing the user to specify a custom function for removing trailing whitespace from the current buffer.
- Even if the buffer is narrowed,
stripspace
removes trailing whitespace from the entire buffer. This behavior, controlled bystripspace-ignore-restrictions
(default:t
), can be adjusted as needed. - The
stripspace-clean-function
customization allows the user to specify a function for removing trailing whitespace from the current buffer. This function is called to eliminate any extraneous spaces or tabs at the end of lines. Alternative functions includedelete-trailing-whitespace
(default) orwhitespace-cleanup
.
To install stripspace with straight.el
:
- It if hasn't already been done, add the straight.el bootstrap code to your init file.
- Add the following code to the Emacs init file:
(use-package stripspace
:ensure t
:straight (stripspace
:type git
:host github
:repo "jamescherti/stripspace.el")
:commands stripspace-local-mode
;; Enable for prog-mode and text-mode
:hook ((prog-mode . stripspace-local-mode)
(text-mode . stripspace-local-mode)
(conf-mode . stripspace-local-mode))
:custom
;; The `stripspace-only-if-initially-clean' option:
;; - nil to always delete trailing whitespace.
;; - Non-nil to only delete whitespace when the buffer is clean initially.
;; (The initial cleanliness check is performed when `stripspace-local-mode'
;; is enabled.)
(stripspace-only-if-initially-clean nil)
;; Enabling `stripspace-restore-column' preserves the cursor's column position
;; even after stripping spaces. This is useful in scenarios where you add
;; extra spaces and then save the file. Although the spaces are removed in the
;; saved file, the cursor remains in the same position, ensuring a consistent
;; editing experience without affecting cursor placement.
(stripspace-restore-column t))
To install stripspace with use-package
and :vc
(Emacs >= 30):
(use-package stripspace
:ensure t
:vc (:url "https://github.com/jamescherti/stripspace.el"
:rev :newest)
:commands stripspace-local-mode
;; Enable for prog-mode and text-mode
:hook ((prog-mode . stripspace-local-mode)
(text-mode . stripspace-local-mode)
(conf-mode . stripspace-local-mode))
:custom
;; The `stripspace-only-if-initially-clean' option:
;; - nil to always delete trailing whitespace.
;; - Non-nil to only delete whitespace when the buffer is clean initially.
;; (The initial cleanliness check is performed when `stripspace-local-mode'
;; is enabled.)
(stripspace-only-if-initially-clean t)
;; Enabling `stripspace-restore-column' preserves the cursor's column position
;; even after stripping spaces. This is useful in scenarios where you add
;; extra spaces and then save the file. Although the spaces are removed in the
;; saved file, the cursor remains in the same position, ensuring a consistent
;; editing experience without affecting cursor placement.
(stripspace-restore-column t))
The ws-butler tracks modified lines and removes trailing whitespace only from those lines. However, it is slightly more complex, as it employs custom functions to track buffer changes, triggered by the following hooks: after-change-functions
, before-save-hook
, after-save-hook
, before-revert-hook
, after-revert-hook
, and edit-server-done-hook
.
In contrast, the stripspace package is lightweight. It operates solely on the before-save-hook
to remove whitespace from the entire buffer using built-in Emacs functions, and on the after-save-hook
to restore the cursor.
Optionally, when stripspace-local-mode
is enabled, it can check if the buffer is already clean (with no whitespace) to determine whether trailing whitespace should be deleted automatically.
The stripspace Emacs package has been written by James Cherti and is distributed under terms of the GNU General Public License version 3, or, at your choice, any later version.
Copyright (C) 2025 James Cherti
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.
Other Emacs packages by the same author:
- minimal-emacs.d: This repository hosts a minimal Emacs configuration designed to serve as a foundation for your vanilla Emacs setup and provide a solid base for an enhanced Emacs experience.
- compile-angel.el: Speed up Emacs! This package guarantees that all .el files are both byte-compiled and native-compiled, which significantly speeds up Emacs.
- outline-indent.el: An Emacs package that provides a minor mode that enables code folding and outlining based on indentation levels for various indentation-based text files, such as YAML, Python, and other indented text files.
- vim-tab-bar.el: Make the Emacs tab-bar Look Like Vim’s Tab Bar.
- easysession.el: Easysession is 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).
- elispcomp: A command line tool that allows compiling Elisp code directly from the terminal or from a shell script. It facilitates the generation of optimized .elc (byte-compiled) and .eln (native-compiled) files.
- tomorrow-night-deepblue-theme.el: The Tomorrow Night Deepblue Emacs theme is a beautiful deep blue variant of the Tomorrow Night theme, which is renowned for its elegant color palette that is pleasing to the eyes. It features a deep blue background color that creates a calming atmosphere. The theme is also a great choice for those who miss the blue themes that were trendy a few years ago.
- Ultyas: A command-line tool designed to simplify the process of converting code snippets from UltiSnips to YASnippet format.
- dir-config.el: Automatically find and evaluate .dir-config.el Elisp files to configure directory-specific settings.
- flymake-bashate.el: A package that provides a Flymake backend for the bashate Bash script style checker.
- flymake-ansible-lint.el: An Emacs package that offers a Flymake backend for ansible-lint.
- inhibit-mouse.el: A package that disables mouse input in Emacs, offering a simpler and faster alternative to the disable-mouse package.
- quick-sdcv.el: This package enables Emacs to function as an offline dictionary by using the sdcv command-line tool directly within Emacs.
- enhanced-evil-paredit.el: An Emacs package that prevents parenthesis imbalance when using evil-mode with paredit. It intercepts evil-mode commands such as delete, change, and paste, blocking their execution if they would break the parenthetical structure.