views:

297

answers:

2

How do I prevent Emacs from showing me all the files I'm not interested in (such as ~ backup files, .pyc files, or .orig files) when I: C-x C-f TAB ?

It is working in one respect: if I know the file I want to open begins with foo and I type foo TAB then the mini-buffer correctly autocompletes all the way to foo.py. It correctly ignored foo~ and foo.pyc, because both ~ and .pyc are in completion-ignored-extensions. It also correctly lets me open either ignored file if I really want to by typing in all the letters my self.

However, if I just hit TAB to to bring up the completion list buffer then that list includes files with extensions in completion-ignored-extensions, which makes it very difficult to find what I'm looking for.

Clearly the code to ignore uninteresting files is there and working. How do I get the completion list buffer to respect completion-ignored-extensions?

(by-the-by, can I make dired behave similarly?)

A: 

I don't know of an answer for completion, I'm afraid. I think this is by design - when you know the name you're looking for, you probably don't want e.g. the backup file. But when you don't know, it's probably better to have a list of all of the files.

However, for dired, you can load the 'dired-x' package on startup (in your .emacs), and this provides dired-omit-mode:

(load "dired-x")

You can use 'M-x customize-variable<RET>dired-omit-files' to set the actual patterns to ignore. Then when you are in dired mode you can use M-O (the letter, not the number) to toggle 'omission' on and off.

Peter Hart
Nice. I like the way dired says "Omitted 26 lines." (or whatever) hen you open a directory. I think the same could happen for the file completion list, which would mitigate your concern about hiding files which might sometimes be needed. Pressing TAB again could show all files (like M-o).
emacsulike
+8  A: 

This piece of advice filters out files with extensions listed in 'completion-ignored-extensions:

(defadvice completion--file-name-table (after 
                                        ignoring-backups-f-n-completion 
                                        activate)
  "filter out results when the have completion-ignored-extensions"
  (let ((res ad-return-value))
(if (and (listp res)
  (stringp (car res))
  (cdr res))                 ; length > 1, don't ignore sole match
    (setq ad-return-value
              (completion-pcm--filename-try-filter res)))))

Note: This doesn't affect dired.

For the dired issue, add this to your .emacs

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

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

Read the documentation for dired-x to get an idea of what's available there.

Trey Jackson
Almost perfect! Any chance the advice can be modified to respect the variable completion-ignored-extensions?
emacsulike
Yup, now uses `completion-ignored-extensions`.
Trey Jackson
Awesome. btw: I don't think you need the (require 'cl) as it currently stands.
emacsulike
Touche, removed.
Trey Jackson