4

I read the following article: http://qerub.se/hiding-hidden-files-in-emacs-dired about hiding certain files in dired mode. I decided to hide more files and added a shortcut Alt-q, that will toggle the visibility of:

  1. All the hidden files, starting with dot.
  2. All Emacs backup files, ending with '~'.

I wrote the following code:

(setq dired-omit-files "^\\.$|~$")
(add-hook 'dired-mode-hook
          (lambda ()
                (define-key dired-mode-map (kbd "M-q") 'dired-omit-mode)
      ))

But only ~ files are hidded, not those, starting with dot. What is wrong?

5
  • Try testing your regex using M-x regex-builder to troubleshoot. I used tool to generate this "^\\..+~?$" which should work for you.
    – Melioratus
    Jan 23, 2015 at 14:44
  • @Melioratus Thx, will do.
    – user4035
    Jan 23, 2015 at 14:45
  • Please post if the regexp works. Thanks!
    – Melioratus
    Jan 23, 2015 at 14:48
  • @Melioratus It works and is simpler, than made by abo-abo. But it's better to use this one: "^\\..*~?$". It will hide the . directory too.
    – user4035
    Jan 23, 2015 at 14:50
  • @Melioratus Done, but can't accept now.
    – user4035
    Jan 23, 2015 at 14:56

3 Answers 3

6

Testing with emacs -q to ensure no customizations:

(require 'dired-x) ;; provides dired-omit-mode

M-x dired ~ -> Backup files and dot files are both present (had to manually add backups since I store them in a dedicated folder by default.

  1. M-x dired-omit-mode -> Backup files are hidden
  2. M-x dired-omit-mode -> All files visible again
  3. (setq dired-omit-files "^\\..*$") -> Omit dotfiles
  4. M-x dired-omit-mode -> Backup and dotfiles are hidden.

Docstring for dired-omit-mode makes reference to dired-omit-extensions. Looking in there, the 2nd extension listed is ~ (emacs backup files). This comes from completion-ignored-extensions (following dock string).

This means by default dired-omit-mode will already remove backups from the view, reducing the complexity of the regexp needed.

2

I think this should work for you:

(setq dired-omit-files "^\\(?:\\..*\\|.*~\\)$")
4
  • What do you think about this regexp: "^\\..*~?$", invented by Melioratus and me?
    – user4035
    Jan 23, 2015 at 14:51
  • 2
    That regex will only match <dot><anything><optional~>. If you want starts-with-dot or ends-with-tilde then you need to look for both independently. Making both optional inline (^\\.?.*~?$) will end up matching everything, so you need to treat them as alternatives. Jan 23, 2015 at 14:56
  • @JonathanLeech-Pepin For some reason my regexp also hides the files, that don't start with dot. Despite the fact, that it shouldn't do it. Can you test on your system and confirm?
    – user4035
    Jan 23, 2015 at 14:58
  • 1
    Figured out why, separate answer incoming to clarify Jan 23, 2015 at 15:03
1

This regexp works successfully: (setq dired-omit-files "^\\..*~?$")

Your Answer

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

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