Emacs: Delete Whitespace around Cursor

By Xah Lee. Date: . Last updated: .

In emacs, the following commands lets you delete whitespaces around cursor.

Here's a command xah-shrink-whitespaces that combine most of them into one.

(defun xah-delete-blank-lines ()
  "Delete all newline around cursor.

URL `http://ergoemacs.org/emacs/emacs_shrink_whitespace.html'
Version 2018-04-02"
  (interactive)
  (let ($p3 $p4)
          (skip-chars-backward "\n")
          (setq $p3 (point))
          (skip-chars-forward "\n")
          (setq $p4 (point))
          (delete-region $p3 $p4)))

(defun xah-shrink-whitespaces ()
  "Remove whitespaces around cursor to just one, or none.

Shrink any neighboring space tab newline characters to 1 or none.
If cursor neighbor has space/tab, toggle between 1 or 0 space.
If cursor neighbor are newline, shrink them to just 1.
If already has just 1 whitespace, delete it.

URL `http://ergoemacs.org/emacs/emacs_shrink_whitespace.html'
Version 2018-04-02T14:38:04-07:00"
  (interactive)
  (let* (
         ($eol-count 0)
         ($p0 (point))
         $p1 ; whitespace begin
         $p2 ; whitespace end
         ($charBefore (char-before))
         ($charAfter (char-after ))
         ($space-neighbor-p (or (eq $charBefore 32) (eq $charBefore 9) (eq $charAfter 32) (eq $charAfter 9)))
         $just-1-space-p
         )
    (skip-chars-backward " \n\t")
    (setq $p1 (point))
    (goto-char $p0)
    (skip-chars-forward " \n\t")
    (setq $p2 (point))
    (goto-char $p1)
    (while (search-forward "\n" $p2 t )
      (setq $eol-count (1+ $eol-count)))
    (setq $just-1-space-p (eq (- $p2 $p1) 1))
    (goto-char $p0)
    (cond
     ((eq $eol-count 0)
      (if $just-1-space-p
          (delete-horizontal-space)
        (progn (delete-horizontal-space)
               (insert " "))))
     ((eq $eol-count 1)
      (if $space-neighbor-p
          (delete-horizontal-space)
        (progn (xah-delete-blank-lines) (insert " "))))
     ((eq $eol-count 2)
      (if $space-neighbor-p
          (delete-horizontal-space)
        (progn
          (xah-delete-blank-lines)
          (insert "\n"))))
     ((> $eol-count 2)
      (if $space-neighbor-p
          (delete-horizontal-space)
        (progn
          (goto-char $p2)
          (search-backward "\n" )
          (delete-region $p1 (point))
          (insert "\n"))))
     (t (progn
          (message "nothing done. logic error 40873. shouldn't reach here" ))))))

Give it a easy key. [see Emacs: How to Define Keys]

Whitespace Topic

  1. Clean Empty Lines
  2. Delete Trailing Whitespace
  3. Delete Whitespace around Cursor
  4. Tabs, Space, Indentation Setup
  5. Make Whitespaces Visible
  6. Newline Representation ^M ^J ^L
  7. Batch Convert Line Ending in Dired

Enhanced Editing Commands Topic

  1. Select Line, between Quotes, Extend Selection
  2. Copy/Cut Current Line If No Selection
  3. Copy/Cut All or Selection
  4. Paste or Paste Previous
  5. Delete Whitespace around Cursor
  6. Move Cursor to Beginning of Line/Paragraph
  7. Move Cursor by Text Block
  8. Move Cursor to Bracket/Quote
  9. Move Cursor to Punctuation
  10. Delete Text Block
  11. Toggle Comment Current Line
  12. Insert Date Time

Working with Brackets

  1. Highlight Brackets ()[]{}
  2. Insert Brackets by Pair
  3. Delete Brackets ()[]{} by Pair
  4. Select Line, between Quotes, Extend Selection
  5. Move Cursor to Bracket/Quote
  6. Change Brackets/Quotes
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.