1

Given the following buffer:

foo bar

    nee nope

If I place point on the 2nd line and execute

(just-one-space -1)

the result will be:

foo bar nee nope

Is there a way to modify the behavior such that the newline after 'bar' will be kept, e.g. with this result:

foo bar
neee nope

?

Or do I have to write my own function for this?

Thanks in advance, Tom

  • I do not use this package (shrink-whitespace), but you can check it out to see if it fits your needs. – Kaushal Modi May 18 '16 at 21:23
  • 2
    Instead of updating the question, you can add your own answer and mark it as a solution that worked for you. That way this question will have a clean closure. – Kaushal Modi May 19 '16 at 13:22
1

The command delete-blank-lines (bound to C-x C-o by default) does what you want, at least in this case. Of course, it doesn't act like just-one-space in other cases, so you may want to write a function that dispatches appropriately for your preferences.

| improve this answer | |
  • delete-blank-lines will not remove the leading whitespace, so you still need a couple commands (e.g. delete-blank-lines and delete-horizontal-space). In the OP's specific example I'd probably put point at the beginning ofneee and hit C-^. – glucas May 18 '16 at 20:57
  • Thanks for the answer. I wrote something myself, see updated question above. – Tom May 19 '16 at 11:19
0

Here's what I came up with so far:

  (defun viking--kill-space()
  "Kill space around point, including newline(s), but the first."
  (interactive)
  (let* ((lineA 0) (lineB 0)
         (beg (save-excursion
                (skip-chars-backward " \t\r\n")
                (cond ((looking-at "\r\n")
                       (forward-char 2))
                      ((looking-at "\n")
                       (forward-char 1)))
                (setq lineA (line-number-at-pos))
                (point)))
         (end (save-excursion
                (skip-chars-forward " \t\r\n")
                (setq lineB (line-number-at-pos))
                (point)))
         )
    (viking--blink-region beg end)
    (if (= lineA lineB)
        (just-one-space)
      (delete-region beg end))
    (message "spaces cleared")))

This accomplishes the job, it could a little bit more efficient though.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.