Function: string-fill

string-fill is an autoloaded and byte-compiled function defined in subr-x.el.gz.

Signature

(string-fill STRING WIDTH)

Documentation

Try to word-wrap STRING so that it displays with lines no wider than WIDTH.

STRING is wrapped where there is whitespace in it. If there are individual words in STRING that are wider than WIDTH, the result will have lines that are wider than WIDTH.

Other relevant functions are documented in the string group.

View in manual

Probably introduced at or before Emacs version 28.1.

Shortdoc

;; string
(string-fill "Three short words" 12)
    => "Three short\nwords"
  (string-fill "Long-word" 3)
    => "Long-word"

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/subr-x.el.gz
;;;###autoload
(defun string-fill (string width)
  "Try to word-wrap STRING so that it displays with lines no wider than WIDTH.
STRING is wrapped where there is whitespace in it.  If there are
individual words in STRING that are wider than WIDTH, the result
will have lines that are wider than WIDTH."
  (declare (important-return-value t))
  (with-temp-buffer
    (insert string)
    (goto-char (point-min))
    (let ((fill-column width)
          (adaptive-fill-mode nil))
      (fill-region (point-min) (point-max)))
    (buffer-string)))