Emacs has several commands to delete whitespace.
To fixup white space between objects around point according to the context, use ‘M-x fixup-whitespace’
To join two words (or any non-space characters) together by deleting all whitespace between them, use any of the following:
'M-\'
or ‘M-x delete-horizontal-space’
(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 make sure two words are separated by just one space, use ‘M-SPC’
or ‘M-x just-one-space’
.
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’
.
To delete extra blank lines between paragraphs, leaving just zero or one blank lines, use ‘M-x delete-blank-lines’
.
To join this line to the previous one and fix up whitespace and fill prefixes, use `M-^’ or ‘M-x delete-indentation’
.
To indent (or unindent) a block of lines together rigidly (adding or removing whitespace at the line beginning):
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 whitespaceTo delete trailing whitespace from the entire buffer, use ‘M-x delete-trailing-whitespace’
.
To perform a general whitespace cleanup, use one of these commands from the WhiteSpace package:
‘whitespace-cleanup’
: Cleanup some blank problems in all buffer or at region.‘whitespace-cleanup-region’
: Cleanup some blank problems at region. By default the cleanup includes both deleting and other fixes.(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)))))))
‘M-x nuke-trailing-whitespace’
‘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’
.‘C-x C-h’
):(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 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.
To remove whitespace at the end of a document, use any of the following:
‘C-x C-o’
(‘delete-blank-lines’
) at the end of the buffer (`M->
’).‘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." (interactive) (save-excursion (save-restriction (widen) (goto-char (point-max)) (delete-blank-lines))))
To prevent adding newlines when you move the cursor forward at the end of the buffer, customize ‘next-line-add-newlines’
to ‘nil’
.
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))))
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
To show trailing whitespace, see ShowWhiteSpace.