0

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.

halloleo
  • 1,215
  • 9
  • 23

1 Answers1

3
(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)
phils
  • 48,657
  • 3
  • 76
  • 115
  • 1
    You can add `-local` after `setq` and spare the comment – Stefan Jan 29 '20 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 Jan 29 '20 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 Jan 29 '20 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 Jan 30 '20 at 02: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 Jan 30 '20 at 05:17
  • i.e. All variables *can* be given a buffer-local value, but some of them (such as `truncate-lines`) also *default* to behaving that way when they are set, rather than only doing it on explicit request. – phils Jan 30 '20 at 05:19