Search code examples
emacsspacemacs

how to set width of truncate lines in Emacs


I'm aware of auto-fill in emacs, this is great and I use it when I want text to have a line break at a particular width (e.g. when coding). However, when editing and reading text I don't want to reformat the text (M-q) and insert line breaks as this then looks odd when exporting the text e.g. to an email or Microsoft Word.

Is emacs 'truncate lines' the tool for the job? Seems to be but I can't figure out how to set the width for truncate lines, it seems to only be able to truncate at the screen width - is there a setting for this or another mode?

(I'm using spacemacs, but this shouldn't have much relevance)


Solution

  • There's a code addressing truncating a buffer less than the wincow's wicth in the emacs exchange here. Look at @JeanPierre answer. It uses set-window-margins see the emacs manual here.

    It is an interactive function, so use M-x my/change-window-width to call it after evaluating in scratch.

    (defun my/change-window-width (width)
    "Adjust margins so that window is centered"
      (interactive "NWindow width: ")
      (if (= 0 width)
          (set-window-margins nil 0 0)
        (let* ((cur-width (window-width))
               (cur-m (window-margins))
               (cur-l (if (and cur-m (car cur-m)) (car cur-m) 0))
               (cur-r (if (and cur-m (cdr cur-m)) (cdr cur-m) 0))
               (lr (- (+ cur-l cur-r cur-width) width))
               (left (/ lr 2))
               (right (if (= 0 (% lr 2)) left (1+ left))))
          (set-window-margins nil (max left 0) (max right 0)))))
    

    It is a variation of a function in visual-line-mode see doc here

    To truncate only a part of the buffer, it looks like fill-column