Is there a way to do ignore whole file names the same way as 'completion-ignored-extensions' ignores filename extensions?

For example, I would like to blacklist __pycache__/, so that if the default directory contains __init__.py and __pycache__/ (and these two are the only files whose names start with underscore), I can do C-x C-f _ TAB and get right to __init__.py as the only choice, without Emacs offering me __pycache__/ after me hitting TAB once more to reveal the second underscore. Is this possible?

2 Answers 2


Not too sure what the context of your question is. If you're using function read-file-name then you can pass it a PREDICATE argument that will exclude any files or dirs you want to exclude as completion candidates. C-h f read-file-name tells us:

Sixth arg PREDICATE, if non-nil, should be a function of one argument; then a file name is considered an acceptable completion alternative only if PREDICATE returns non-nil with the file name as its argument.

So a PREDICATE value of foo would exclude file __pycache__:

(defun foo (filename)
  "Return non-nil if FILENAME is not `__pycache__'."
  (setq filename  (directory-file-name filename))
  (not (string= filename "__pycache__")))

(read-file-name "File: " nil nil t nil 'foo)

The reason for using directory-file-name is so that this works for a FILENAME value that is a directory name, as well as a value that is an ordinary file name.


From the documentation of completion-ignored-extensions

It ignores directory names if they match any string in this list which
ends in a slash.

so you can simply add __pycache__/ to the variable

(add-to-list 'completion-ignored-extensions

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.