How can I set the default of truncate-lines for dired buffers to truncate?

I.e. when I open a new dired buffer it should show the directory truncated and not wrapped into the next line.

1 Answer 1

(defun my-dired-mode-hook ()
  "Custom behaviours for `dired-mode'."
  ;; `truncate-lines' is automatically buffer-local.
  (setq truncate-lines t))

(add-hook 'dired-mode-hook #'my-dired-mode-hook)
  • 1
    You can add -local after setq and spare the comment
    – Stefan
    Commented Jan 29, 2020 at 13:13
  • 1
    Using setq-local in the context of setting truncate-lines potentially implies that the user has failed to take the time to investigate whether the variable was already buffer-local. Or worse, it could mean that the user erroneously thought it was a global variable. Users should be encouraged, in my opinion, to investigate and learn which variables are global and which ones have already been pre-defined as being buffer-local.
    – lawlist
    Commented Jan 29, 2020 at 19:58
  • I agree with both Stefan and lawlist :) I used plain setq with a comment for the benefit of the reader, so that they would also learn that some variables are automatically buffer-local, if they were not already aware.
    – phils
    Commented Jan 29, 2020 at 21:43
  • Thanks for this, @phils. Side note: Because truncate-lines is a (buffer-local) variable I can even put truncate-lines: t in the .dired or .dir-locals.el file. :-)
    – halloleo
    Commented Jan 30, 2020 at 2:54
  • @halloleo If you use .dir-locals.el (or indeed .dired, although that is deprecated), then the value is set buffer-locally (by way of make-local-variable) regardless of whether or not a plain setq would do the same thing.
    – phils
    Commented Jan 30, 2020 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.