Emacs has several commands to delete whitespace.
To make sure two words are separated by just one space, use
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))))))
To remove trailing whitespace from the entire buffer, use any of the following:
‘M-x delete-trailing-whitespace’(GnuEmacs version 21 or later). You can put this in
‘before-save-hook’to ensure that your files have no trailing whitespace:
(add-hook 'before-save-hook 'delete-trailing-whitespace)
(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)))))))
‘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
(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)))
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).
To show (highlight) trailing whitespace, see ShowWhiteSpace. There are several ways, depending on what you want and what Emacs version you have.
To remove whitespace at the end of a document, use any of the following:
‘delete-blank-lines’) at the end of the buffer (`
‘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))))
‘C-x C-o’ (
‘delete-blank-lines’) also deletes any extra blank lines between two paragraphs, leaving just one blank line.
To prevent adding newlines when you move the cursor forward at the end of the buffer, customize
To indent (or unindent) a block of lines together rigidly (adding or removing whitespace at the line beginning):
C-u NUM C-x TABto indent
NUMcolumns. A negative
C-- 4 C-x TAB) un-indents, removing a rectangle of whitespace
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)))))
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))))
(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
delete-horizontal-space? – c-7e5ae353.131-1-64736c10.cust.bredbandsbolaget.se
‘C-h f’ for function reference clears this up.
Delete characters backward, changing tabs into spaces. The exact behavior depends on
Delete all spaces and tabs around point. If BACKWARD-ONLY is non-nil, only delete them before point.