DeletingWhitespace

Emacs has several commands to delete whitespace.

Just one space

To make sure two words are separated by just one space, use ‘M-SPC’ or ‘M-x just-one-space’.

Join two words

To join two words (or any non-space characters) together by deleting all whitespace between them, use any of the following:

        (defun kill-whitespace ()
          "Kill the whitespace between two non-whitespace characters"
          (interactive "*")
          (save-excursion
            (save-restriction
              (save-match-data
                (progn
                  (re-search-backward "[^ \t\r\n]" nil t)
                  (re-search-forward "[ \t\r\n]+" nil t)
                  (replace-match "" nil nil))))))

Remove trailing whitespace

To remove trailing whitespace from the entire buffer, use any of the following:

        (add-hook 'before-save-hook 'delete-trailing-whitespace)
or you can add this hook locally for particular mode:
        (add-hook 'c-mode-hook
                (lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))

Remove even the last trailing newline

       (defun my-other-delete-trailing-blank-lines ()
          "Deletes all blank lines at the end of the file, even the last one"
          (interactive)
          (save-excursion
            (save-restriction
              (widen)
              (goto-char (point-max))
              (delete-blank-lines)
              (let ((trailnewlines (abs (skip-chars-backward "\n\t"))))
                (if (> trailnewlines 0)
                    (progn
                      (delete-char trailnewlines)))))))

With PictureMode

nuke-trailing-whitespace

Note: I’ve had some corruption problems with ‘nuke-trailing-whitespace’ and Unicode files. In addition to the trailing newlines getting deleted, a few non-whitespace characters get deleted too. I’ve done a quick fix by replacing the ‘delete-region’ with ‘delete-blank-lines’.

whitespace.el

Simple function

        (defun my-delete-leading-whitespace (start end)
          "Delete whitespace at the beginning of each line in region."
          (interactive "*r")
          (save-excursion
            (if (not (bolp)) (forward-line 1))
            (delete-whitespace-rectangle (point) end nil)))

ethan-wspace

DeleteNlSpaces

Version control friendly options (modify only the lines you have edited)

When working with version-control, you never want Emacs to modify lines which you didn’t edit, so automatically deleting all trailing whitespace from a file is a bad idea.

The following libraries automatically remove unwanted trailing whitespace from the lines you’ve edited (only).

Show trailing whitespace

To show (highlight) trailing whitespace, see ShowWhiteSpace. There are several ways, depending on what you want and what Emacs version you have.

Remove whitespace at the end of the document

To remove whitespace at the end of a document, use any of the following:

However, be aware that if ‘require-final-newline’ is non-‘nil’, then a (single) newline will be added automatically at the end of the file. (The particular non-‘nil’ value determines when it is added – see ‘C-h v require-final-newline’.)
    (defun my-delete-trailing-blank-lines ()
      "Deletes all blank lines at the end of the file."
      (interactive)
      (save-excursion
        (save-restriction
          (widen)
          (goto-char (point-max))
          (delete-blank-lines))))

Delete extra blank lines between paragraphs

‘C-x C-o’ (‘delete-blank-lines’) also deletes any extra blank lines between two paragraphs, leaving just one blank line.

Prevent adding newlines at end of buffer

To prevent adding newlines when you move the cursor forward at the end of the buffer, customize ‘next-line-add-newlines’ to ‘nil’.

Indent/unindent a block of lines

To indent (or unindent) a block of lines together rigidly (adding or removing whitespace at the line beginning):

    1. Select all the lines to indent
    2. Ensure the TextCursor is at the beginning of the last line to indent.
    3. C-u NUM C-x TAB to indent NUM columns. A negative NUM (e.g., C-- 4 C-x TAB) un-indents, removing a rectangle of whitespace

Whitespace Aware `kill-line'

This handles whitespace a little better when having indentation and you kill a line.

    (defadvice kill-line (after kill-line-cleanup-whitespace activate compile)
      "cleanup whitespace on kill-line"
      (if (not (bolp))
	  (delete-region (point) (progn (skip-chars-forward " \t") (point)))))

Delete adjacent whitespace

To delete whitespace from point to next word:

    (defun whack-whitespace (arg)
      "Delete all white space from point to the next word.  With prefix ARG
    delete across newlines as well.  The only danger in this is that you
    don't have to actually be at the end of a word to make it work.  It
    skips over to the next whitespace and then whacks it all to the next
    word."
      (interactive "P")
      (let ((regexp (if arg "[ \t\n]+" "[ \t]+")))
        (re-search-forward regexp nil t)
        (replace-match "" nil nil)))

To delete adjacent whitespace forward:

    (defun delete-horizontal-space-forward () ; adapted from `delete-horizontal-space'
      "*Delete all spaces and tabs after point."
      (interactive "*")
      (delete-region (point) (progn (skip-chars-forward " \t") (point))))

Backward:

    (defun backward-delete-char-hungry (arg &optional killp)
      "*Delete characters backward in \"hungry\" mode.
    See the documentation of `backward-delete-char-untabify' and
    `backward-delete-char-untabify-method' for details."
      (interactive "*p\nP")
      (let ((backward-delete-char-untabify-method 'hungry))
        (backward-delete-char-untabify arg killp)))

What is the difference between those two functions and the built-in backward-delete-char-untabify-method and delete-horizontal-space? – c-7e5ae353.131-1-64736c10.cust.bredbandsbolaget.se

‘C-h f’ for function reference clears this up.

backward-delete-char-untabify:

Delete characters backward, changing tabs into spaces. The exact behavior depends on ‘backward-delete-char-untabify-method’.

delete-horizontal-space:

Delete all spaces and tabs around point. If BACKWARD-ONLY is non-nil, only delete them before point.

--CH


CategoryEditing