Emacs has several commands to delete whitespace.

Built-in point-based commands

Fixup whitespace around point

To fixup white space between objects around point according to the context, use ‘M-x fixup-whitespace’

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 "*")
                  (re-search-backward "[^ \t\r\n]" nil t)
                  (re-search-forward "[ \t\r\n]+" nil t)
                  (replace-match "" nil nil))))))

Just one space

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

Cycle spacing around point

To cycle the whitespace around point in a smart way (between one or N spaces, no space, and its original value, use ‘M-x cycle-space’.

Delete extra blank lines between paragraphs

To delete extra blank lines between paragraphs, leaving just zero or one blank lines, use ‘M-x delete-blank-lines’.

Join lines and fix up whitespace

To join this line to the previous one and fix up whitespace and fill prefixes, use `M-^’ or ‘M-x delete-indentation’.

Built-in region/buffer-based commands

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

Delete trailing whitespace

To delete trailing whitespace from the entire buffer, use ‘M-x delete-trailing-whitespace’.

Perform general whitespace cleanup

To perform a general whitespace cleanup, use one of these commands from the WhiteSpace package:

Miscellaneous commands

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"
              (goto-char (point-max))
              (let ((trailnewlines (abs (skip-chars-backward "\n\t"))))
                (if (> trailnewlines 0)
                      (delete-char trailnewlines)))))))

With PictureMode


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’.

Simple function

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



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

When working with version-control, you may not want Emacs to modify lines which you didn’t edit. The following libraries automatically remove unwanted trailing whitespace from (only) the lines you’ve edited.

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 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."
          (goto-char (point-max))

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’.

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
      (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))))


    (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-h f’ for function reference clears this up.


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


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


Show trailing whitespace

To show trailing whitespace, see ShowWhiteSpace.