How to get Dired to ignore files with specific extensions

五迷三道 提交于 2019-12-04 03:03:50

问题


I put the following in my .emacs file:

(require 'dired-x)
(add-hook 'dired-load-hook '(lambda () (require 'dired-x)))
(setq dired-omit-files-p t)
(setq dired-omit-files
(concat dired-omit-files "\\|^\\..+$\\|-t\\.tex$\\|-t\\.pdf$"))

But C-x d still shows me .pdf and .tex files. Did I get the syntax wrong in that last line?

Bonus question: Is there a way to get Dired to hide hidden directories, like .git folders?


回答1:


Your regexp will match *-t.tex files, not *.tex ones.

With recent version of Emacs, it should be sufficient to add the following section to ~/.emacs to filter what you want:

(require 'dired-x)
(setq-default dired-omit-files-p t) ; this is buffer-local variable
(setq dired-omit-files
    (concat dired-omit-files "\\|^\\..+$\\|\\.pdf$\\|\\.tex$"))

Update: by default, dired-omit-files regexp filters out special directories . and ... If you don't want this behavior, you can just override defaults (instead of inheriting them with concat):

(setq dired-omit-files "^\\.[^.]\\|\\.pdf$\\|\\.tex$")

The regexp ^\\.[^.] will match any string of length 2+ starting with a dot where second character is any character except the dot itself. It's not perfect (will not match filenames like "..foo"), but should be ok most of the time.




回答2:


A simple and very general solution which doesn't rely on any extras is to do C-u s to change the ls flags and immediately refresh (that is, C-u s takes care of refreshing also, so there is very little typing involved). Usually you will want to remove -a to hide dotfiles. But you can do everything you're already able to do in the shell console, which is far more than what a simple toggle mode could offer (at the cost of some extra keypressings). And there is a history of previous flags available, so "toggling" is pretty fast too.



来源:https://stackoverflow.com/questions/14850103/how-to-get-dired-to-ignore-files-with-specific-extensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!