0

Is there a way of calling (end-of-buffer) that excludes trailing empty lines?

1 Answer 1

2

We can go to the end of the buffer, then move backwards over whitespace:

(progn (goto-char (point-max))
       (skip-chars-backward "\n[:space:]"))

If you want to call this interactively, make it into a command:

(defun end-of-buffer-before-whitespace ()
  "Move point to after the last non-whitespace character in the buffer."
  (interactive)
  (goto-char (point-max))
  (skip-chars-backward "\n[:space:]"))
2
  • 2
    Better use "\n[:space:]" instead of "\n\t " in skip-chars-backward.
    – Tobias
    Jun 17, 2019 at 21:45
  • Note, if you don't want the cursor to be at the line end, add (move-beginning-of-line nil)
    – ideasman42
    Jun 18, 2019 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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