Skip to content

Instantly share code, notes, and snippets.

@benley
Last active August 12, 2020 23:42
Show Gist options
  • Save benley/5d26fc399c18bade77a3ff1432a30a74 to your computer and use it in GitHub Desktop.
Save benley/5d26fc399c18bade77a3ff1432a30a74 to your computer and use it in GitHub Desktop.
(require 's)
(defun count-leading-whitespace (str)
"Count leading indentation chars in STR."
(let ((trimmed (string-trim-left str)))
(- (length str) (length trimmed))))
(defun unindent-string (str)
"Remove common leading whitespace from each line in STR."
(let* ((str-as-lines (s-lines str))
(n-to-trim (apply #'min
(mapcar #'count-leading-whitespace str-as-lines))))
(mapconcat 'identity
(mapcar (lambda (s) (substring s n-to-trim)) str-as-lines)
"\n")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment