3

Goal:

  • When dired is opened by default hidden files should be hidden.
  • When the user clicks "M-q" hidden files must be shown except for "." and ".."

I tried the following:

;don't show hidden files
(setq dired-omit-files "^\\..*$")
(defun swap-omit-files ()
      "Show/hide hidden files except . and .."
      (interactive)
      (if (string= dired-omit-files "^\\..*$")
            (setq dired-omit-files "^\\.\\.?$")
            (setq dired-omit-files "^\\..*$")
            )
      )

(add-hook 'dired-mode-hook
      (lambda ()
            (setq dired-omit-mode t)
            (define-key dired-mode-map (kbd "M-q") 'swap-omit-files)
            ))

Hidden file are indeed hidden by default, but something is wrong with swap-omit-files --- the hidden files are not shown.

How to fix?

1

This worked. We needed to revert-buffer after changing dired-omit-files:

(defun swap-omit-files ()
      "Show/hide hidden files except . and .."
      (interactive)
      (if (string= dired-omit-files "^\\..*$")
            (setq dired-omit-files "^\\.\\.?$")
            (setq dired-omit-files "^\\..*$")
            )
      (revert-buffer)
      )
0

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.