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.)
It also includes an optional feature (stripspace-only-if-initially-clean
, disabled by default), which, when enabled, ensures that trailing whitespace is removed only if the buffer was initially clean. This prevents unintended modifications to buffers that already contain changes, making it useful for preserving intentional whitespace or avoiding unnecessary edits in files managed by version control.
Installation from MELPA
- If you haven’t already done so, add MELPA repository to your Emacs configuration.
- Add the following code to your Emacs init file to install stripspace from MELPA:
(use-package stripspace
:ensure t
:commands stripspace-local-mode
;; Enable for prog-mode-hook, text-mode-hook, prog-mode-hook
: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))
Code language: PHP (php)
Features
Here are the features of (stripspace-local-mode)
:
- Before saving buffer: Automatically removes all trailing whitespace.
- After saving buffer: Restores 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. This behavior can be controller by the
stripspace-restore-column
variable (default:t
). - Even if the buffer is narrowed, stripspace removes trailing whitespace from the entire buffer. This behavior, controlled by the
stripspace-ignore-restrictions
variable (default:t
). - An optional feature
stripspace-only-if-initially-clean
(default:nil
), which, when set to non-nil, instructs stripspace to only delete whitespace when the buffer is clean initially. The check for a clean buffer is optimized using a single regex search for trailing whitespace and another for blank lines. - 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 functions for deleting whitespace are customizable, allowing the user to specify a custom function for removing trailing whitespace from the current buffer.
- The
stripspace-clean-function
variable allows specifying 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. (For example, this can be set to a built-in function such asdelete-trailing-whitespace
(default) orwhitespace-cleanup
.)