1

I use the following code for including dired omit mode:

(require 'dired-x)
(setq dired-omit-files "^\\...+$")
(add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
;(dired-omit-mode 1)

When I turn on dired, by (C-x d), text files ending with ~ are visible. I can hide them by hitting M-x dired-omit-mode. However I would like that dired opens directly into omit mode. I try to achieve this by uncommenting the fourth line, (dired-omit-mode 1), but I get the error message:

Wrong type argument: stringp, nil

What is wrong with my forth code line?

How to start dired in omit mode?

  • C-h f dired-omit-mode tells you: "If called from Lisp, enable the mode if ARG is omitted or nil." – Drew Jul 17 at 0:32
  • @Drew: yes, I read the same from C-h f, but I have no clue as to which consequences this have on how to alter my code. – myotis Jul 17 at 7:01
1

Dired-Omit mode is a buffer-local minor mode, it doesn't make sense if the current buffer is not a Dired buffer, thus the following doesn't make sense because when Emacs starts the current buffer is definitely not a Dired buffer (actually it's in Fundamental mode):

;;; init.el
(dired-omit-mode 1)

The correct way to enable dired-omit-mode whenever Dired starts is using dired-mode-hook, e.g.,

(add-hook 'dired-mode-hook 'dired-omit-mode)

The following does the same as the above but is verbose

(add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))

And since dired-omit-mode is provided by dired-x.el and isn't autoloaded (though I think it is worth to be autoloaded), you won't be able to invoke dired-omit-mode unless it's defined or autoloaded. You can autoload it via

(autoload 'dired-omit-mode "dired-x")

or you can ensure it's defined via

(with-eval-after-load 'dired
  (require 'dired-x))

the following does the similar as the above, though it will slow down your Emacs startup

(require 'dired-x)

I'm not sure what's the problem you're encountering, but if you understand the code you're using, it's likely you can fix it by yourself.

  • Thanks. I tried to insert your (autoload 'dired-omit-mode "dired-x") between my first and second code line, between my second and third line, and below my third line. In none of these positions did dired start in omit mode. How would you alter my code in order to make it open dired in omit mode? – myotis Jul 21 at 21:16
  • @myotis (add-hook 'dired-mode-hook 'dired-omit-mode) does the work, if it doesn't for you, you have to try to figure out what it does and why by yourself, since I can't reproduce the issue. – xuchunyang Jul 21 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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