I often get into a spot where I have a bunch of whitespace and newlines separating a couple of closed parentheses.
(defun fib (n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2))
)))
Lets assume your cursor is anywhere between the end of line 4 and the first close parentheses on line 6 and you want to delete all of the whitespace around your cursor.
You have several options if you are willing to first navigate.
If you move to line 6, you can use join-line (M-^)
to remove indentation and the preceding newline from your current line. Perform that operation twice and problem solved.
If you move to the end of line 4, you can use zap-up-to-char
to delete everything from your cursor up to, but not including, the next close parentheses.
But if you are unwilling or unable to perform any movement operations, then, as far as I know, we are going to need to write our own function.
(defun delete-surrounding-whitespace ()
(interactive)
(let ((skip-chars "\t\n\r "))
(skip-chars-backward skip-chars)
(let* ((start (point))
(num (progn
(skip-chars-forward skip-chars)
(point))))
(delete-region start num))))
In the spirit of teaching fishing over providing fish, I'll share how to discover this from inside Emacs.
If you know the anything about the name of the function or variable that you want more information about, then you can use apropos
to dig deeper. But what if you don't know the what the command might be called?
For example, I might use apropos and search for del.*white
, zap.*space
, del.*space
, etc... and never come across a helpful whitespace function like just-one-space
.
To widen the range of your search, you can search the documentation of Emacs from inside of Emacs by pressing C-h i
to get to the Texinfo docs. Press mEmacs<Return>
to enter the Emacs-specific part of the documentation (there will also be sections for some packages). Once in the Emacs section, press s
to search and perform the search for something like delete.*white
and you'll be taken to the Deletion section of the docs where you'll see all sorts of helpful methods of deletion.
12.1.1 Deletion
---------------
Deletion means erasing text and not saving it in the kill ring. For the
most part, the Emacs commands that delete text are those that erase just
one character or only whitespace.
‘<DEL>’
‘<BACKSPACE>’
Delete the previous character, or the text in the region if it is
active (‘delete-backward-char’).
‘<Delete>’
Delete the next character, or the text in the region if it is
active (‘delete-forward-char’).
‘C-d’
Delete the next character (‘delete-char’).
‘M-\’
Delete spaces and tabs around point (‘delete-horizontal-space’).
‘M-<SPC>’
Delete spaces and tabs around point, leaving one space
(‘just-one-space’).
‘C-x C-o’
Delete blank lines around the current line (‘delete-blank-lines’).
‘M-^’
Join two lines by deleting the intervening newline, along with any
indentation following it (‘delete-indentation’).
I didn't see anything there that did exactly what I was looking for. But, by using apropos
to search and pull up the help buffers for some of the functions, I could see how they were implemented and use those same techniques to write the exact function I need.
Looking in simple.el.gz
at the function just-one-space
, I saw a nearby function called cycle-spacing
that looked like it was close to having the functionality that I needed.
(defun cycle-spacing (&optional n preserve-nl-back mode)
"Manipulate whitespace around point in a smart way.
In interactive use, this function behaves differently in successive
consecutive calls.
The first call in a sequence acts like `just-one-space'.
It deletes all spaces and tabs around point, leaving one space
\(or N spaces). N is the prefix argument. If N is negative,
it deletes newlines as well, leaving -N spaces.
\(If PRESERVE-NL-BACK is non-nil, it does not delete newlines before point.)
The second call in a sequence deletes all spaces.
The third call in a sequence restores the original whitespace (and point).
If MODE is `single-shot', it only performs the first step in the sequence.
If MODE is `fast' and the first step would not result in any change
\(i.e., there are exactly (abs N) spaces around point),
the function goes straight to the second step.
Repeatedly calling the function with different values of N starts a
new sequence each time."
(interactive "*p")
(let ((orig-pos (point))
(skip-characters (if (and n (< n 0)) " \t\n\r" " \t"))
(num (abs (or n 1))))
(skip-chars-backward (if preserve-nl-back " \t" skip-characters))
(constrain-to-field nil orig-pos)
(cond
;; Command run for the first time, single-shot mode or different argument
((or (eq 'single-shot mode)
(not (equal last-command this-command))
(not cycle-spacing--context)
(not (eq (car cycle-spacing--context) n)))
(let* ((start (point))
(num (- num (skip-chars-forward " " (+ num (point)))))
(mid (point))
(end (progn
(skip-chars-forward skip-characters)
(constrain-to-field nil orig-pos t))))
(setq cycle-spacing--context ;; Save for later.
;; Special handling for case where there was no space at all.
(unless (= start end)
(cons n (cons orig-pos (buffer-substring start (point))))))
;; If this run causes no change in buffer content, delete all spaces,
;; otherwise delete all excess spaces.
(delete-region (if (and (eq mode 'fast) (zerop num) (= mid end))
start mid) end)
(insert (make-string num ?\s))))
;; Command run for the second time.
((not (equal orig-pos (point)))
(delete-region (point) orig-pos))
;; Command run for the third time.
(t
(insert (cddr cycle-spacing--context))
(goto-char (cadr cycle-spacing--context))
(setq cycle-spacing--context nil)))))
I was able to simplify it a bit since I didn't need to handle conditionally removing newlines and leaving n
number of remaining spaces.
(defun delete-surrounding-whitespace ()
(interactive)
(let ((skip-chars "\t\n\r "))
(skip-chars-backward skip-chars)
(let* ((start (point))
(num (progn
(skip-chars-forward skip-chars)
(point))))
(delete-region start num))))
Map that to a convenient shortcut and viola!