When using stock find-file or counsel-find-file, is there a way to ignore certain files that I almost never want to open by hand? Examples include .elc files and backups.
Asked
Active
Viewed 2,377 times
7
Swarnendu Biswas
- 1,378
- 1
- 11
- 24
Nova
- 1,059
- 9
- 21
-
Option **`completion-ignored-extensions`** doesn't work in that context? That's what ordinary `file-name-completion` respects. See (elisp)[File Name Completion](https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Name-Completion.html) and (emacs)[Completion Options](https://www.gnu.org/software/emacs/manual/html_node/emacs/Completion-Options.html). Of course, that's just extensions... – Drew Jan 10 '17 at 05:58
-
2If you customize `counsel-find-file-ignore-regexp`, one of the options is to use `completion-ignored-extensions`. – John Wiegley Dec 13 '17 at 00:12
2 Answers
9
Yes, use counsel-find-file-ignore-regexp.
For the simple example you provide use
(setq counsel-find-file-ignore-regexp "\\.elc\\'")
The are more examples in the docstring for that variable.
justbur
- 1,500
- 8
- 8
-
-
To ignore paths, check `counsel-file-jump-args` - this gives a lot of control over what is found in the first place. – ideasman42 May 18 '23 at 04:07
1
While counsel-find-file-ignore-regexp can be used to exclude filenames, you can configure the find command to exclude paths with greater control as it has all the options available to find.
For example, this command excludes __pycache__ and directories beginning with a . including .git & .mypy_cache.
This example finds all *.el, *.org & Makefiles, excluding .* (.git for e.g.).
(setq counsel-file-jump-args
(list "."
"-not" "-path" "*/\\.*"
"-not" "-path" "./*__pycache__/*"
"(" "-name" "*.el" "-o" "-path" "*.org" "-o" "-path" "**/Makefile" ")"
"-type" "f"
"-print"))
You can experiment in the command line to check which files will be found:
find . -not -path './*__pycache__/*' \
-not -path '*/\\.*' \
"(" -name "*.el" -o -path "*.org" -o -path "**/Makefile" ")" \
-type "f" -print
ideasman42
- 8,375
- 1
- 28
- 105