0

I would like to hide dotfiles only in the home directory in dired buffers, but I do not know how to achieve this. I have been trying with the following:

(use-package dired
  :ensure nil
  :commands (dired dired-jump)
  :bind (("C-x C-j" . dired-jump)
         (:map dired-mode-map ("C-c h" . dired-omit-mode)))
  :config
  (setq dired-omit-files (rx (seq bol "."))) ; hide dotfiles in dired-omit-mode

  (defun najjt/dired-after-readin ()
    "Turn on dired-omit-mode if current buffer is a dired buffer in the home directory."
    (if (string-equal (buffer-name) "najjt") ; check if current buffer name is equal to name of home dir
        (dired-omit-mode 1)
      (dired-omit-mode nil)))

  :hook ((dired-after-readin . (lambda () (najjt/dired-after-readin))))
  )

But it seems to hide dotfiles in all directories, not just my home directory.

1
  • 3
    .dir-locals.el may help
    – shynur
    Commented Dec 27, 2023 at 16:31

2 Answers 2

3

Instead of bothering with using dired-omit-mode to hide dot files, just use the ls switches that give you that behavior (pretty much anything other than switch -a).

And do that only in a particular directory (e.g., your home directory) by using a .dir-locals-file that specifies the specific value of dired-listing-switches that gives you the behavior you want.

(.dir-locals-file is the default value of variable dir-locals-file. See (elisp) Directory Local Variables.)

1
  • So, I have created a file called .dir-locals.el in my home directory, which contains this: ((dired-mode . ((dired-listing-switches . "-lh")))). I am not sure this is correct, however, because it does not hide anything.
    – najjt
    Commented Dec 28, 2023 at 13:29
1

Here's an approach that I like for its simplicity.

(defalias 'hide-dot-files
    (kmacro "% m ^ \\ . <return> k")
    "Hide dot files (files that start with a dot (.))

  For example, a list like this:

    .bashrc
    bookmarks.html

  is converted to this simpler list:

    bookmarks.html")

(keymap-global-set "C-c 1" #'hide-dot-files)

It uses dired's dired-mark-files-regexp command, bound in my system to the key combinations % m and * %, to identify lines that start with a dot and kill them. That doesn't delete the files. It just removes them from the dired listing. They can be restored with another dired command, revert-buffer, bound to the key g in my system.

I like this approach better than changing the flags to systems ls command, which I have set to -alhb, replacing the default value -al. It's not hard to change them, but for me it's a minor nuisance.

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.