3

I'm quite new to emacs (coming from vim), and I'm struggling to figure out how to configure sane line-wrapping/truncation behavior.

For the most part, I'd like to prevent wrapping, so I put this in my init file:

(setq-default truncate-lines t)

But for help/info buffers, I do want wrapping.

Basically, I want truncation in buffers that contain code, and wrapping in buffers that contain text. What's a sensible way to achieve this? I imagine I want to keep the above default, but override it for certain major modes (e.g. Help), but I'm not sure how to do that.

update based on Kaushal Modi's answer

(defun my-truncate-lines-disable () (setq truncate-lines nil))
(add-hook 'help-mode-hook #'my-truncate-lines-disable)
ivan
  • 1,928
  • 10
  • 20

1 Answers1

6
(defun my-truncate-lines-disable ()
  "Disable line truncation, even in split windows."
  (let ((inhibit-message t) ; No messages in the echo area - needs emacs 25+
        message-log-max ; No messages in the *Messages* buffer
        truncate-partial-width-windows) ; No truncation in split windows
    (toggle-truncate-lines 0)))
(add-hook 'help-mode-hook #'my-truncate-lines-disable)
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
  • Though your solution works, it leads to another problem: how to prevent toggle-truncate-lines from displaying "Truncate long lines disabled" in the echo area. Is there another way I could do this using "primitives"? – ivan Jul 23 '16 at 18:15
  • I took a look at the definition of toggle-truncate-lines and it turned out to be pretty simple. – ivan Jul 23 '16 at 18:30
  • @ivan I have updated my answer which works on emacs 25+ (you can try out its [latest pretest version](http://alpha.gnu.org/gnu/emacs/pretest/)). The same code will be fine on older emacsen too, just that setting `inhibit-message` to `t` will not do anything. – Kaushal Modi Jul 23 '16 at 20:06
  • Nice, thank you. I'm using the emacs mac port, so I'm stuck on 24.5.1 for now. Anyway, this forced me to take a look at the implementation of toggle-truncate-lines and pick out just the part I needed. That also gave me an appreciation for how accessible the various layers of emacs' source code are, which is a big change from vim. – ivan Jul 23 '16 at 20:48