Is there a standard Elisp function which can return as a substring (without any of the text-properties) the characters from the current position of point to the last nonwhite space character on that line.

For instance if the line is Hello World and the point is on W it should return orld .

Note in particular, if the line contains at least one non-whitespace character, I want everything only upto the last non-whitespace character.

If a line is blank or contains only white space characters I would like the function to return nil

  • @ Drew Fixed! Meant to say last non-white space character Commented Nov 23, 2018 at 1:34

1 Answer 1


I don't believe there is a standard elisp function to do exactly what you ask, but it isn't hard to compose your own. Here is my stab at what you describe:

(defun gw-string-to-end-of-line ()
  "Return a string from the current point to the end of line.

If there are white-space characters at the end of the line, they
will be stripped off.  If the string that would be returned would
be blank, nil is returned."
  (let ((s (string-trim-right               ;; strip off any trailing
                                            ;; white space
            (buffer-substring-no-properties ;; get the text from the
                                            ;; current position to the
                                            ;; end of the line
             (point) (point-at-eol)))))
    (unless (string-blank-p s)          ;; only return the string if
                                        ;; it contains something     other
                                        ;; than white space (otherwise
                                        ;; nil will be returned)

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.