1

It seems subr-x's string-trim-right only operates on the last line.

How to strip trailing spaces on all lines in a variable?

Drew
  • 77,472
  • 10
  • 114
  • 243
ideasman42
  • 8,786
  • 1
  • 32
  • 114
  • ITYM it operates on a string, not lines inside a string. By that definition it does indeed "trim whitespace from the end of a multi-line string". I'd be surprised if it trimmed it in the middle of it, just because there happened to be a newline at that place. – wasamasa Jul 29 '17 at 19:59

1 Answers1

1

This can be done by splitting and joining strings:

(require 'subr-x)
(defun string-trim-right-multiline (str)
  (mapconcat 'string-trim-right
   (split-string str "\n") "\n"))

Note that if you want to perform other operations on the string you can define a function:

(require 'subr-x)
(defun string-trim-right-multiline (str)
  (mapconcat (function (lambda (s) (string-trim-right s)))
   (split-string str "\n") "\n"))
ideasman42
  • 8,786
  • 1
  • 32
  • 114