4

I am using ido-mode to select files from Emacs. This works well for most cases, but for some cases I would like to modify the files that are shown in the completion buffer with ido-find-file.

In some folders I have a lot of *.bak files, and I would like to have the possiblity to remove them from the completion buffer by pressing the F1 key, for example. This is how far I got on this task:

(add-hook 'ido-minibuffer-setup-hook 'ido-my-keys)

(defun ido-my-keys ()
  (define-key ido-file-completion-map (kbd "<f1>") 'my-modify-files))

(defun my-modify-files ()
  (interactive)
  (message-box "Here I would like to delete the .bak files from the minibuffer"))
7

ido-mode has built-in support for hiding files matching specific regexps. All you need to do is add \.bak to ido-ignore-files:

(add-to-list 'ido-ignore-files "\.bak")

After invoking ido-find-file, you can then hit C-a (ido-toggle-ignore) to toggle hiding of .bak files.

2
  • Perfect! It works like a charm :) Nov 13 '14 at 10:05
  • 1
    @HåkonHægland Great! :) If you're interested, have a look at the documentation of ido-find-file (C-h f ido-find-file RET) for a full list of commands and options you can toggle while the Ido prompt is active.
    – itsjeyd
    Nov 13 '14 at 10:12
1

The correct way to ignore files by extension for file name completion purposes is using the variable completion-ignored-extensions. The variable holds a list of strict suffix strings that are ignored for:

  • directory completion, if the list element ends in a /, or
  • file completion otherwise.

The ido-ignore-files variable, on the other hand, is a list of regular expressions, and matches anywhere in the file name, unless the regex is anchored. In particular, the recommendation in another answer,

(add-to-list 'ido-ignore-files "\.bak")

tells Ido to match the substring .bak anywhere in file name, which may or may not be what you really want. On the other hand,

(add-to-list 'completion-ignored-extensions ".bak")

makes all Emacs completion machinery, not only Ido, but also out-of-the-box file completion and Dired, and likely other places, ignore files ending in .bak (including full name match). Note that this variable is not a list of regexes, but rather a list of literal suffixes; the dot should not be \-escaped.

The default completion-ignored-extensions list contains commonly ignored suffixes such as .elc, .o, .a, .so, ~, .git/, and many more. This is why the default ido-ignore-files list is suspiciously short.

The completion behavior is actually more complex than just always ignoring the files; see the docstring of the variable. One important point is that if all completions are to be ignored according to completion-ignored-extensions, then none are. This allows, for example, descending into the .git/ subdirectory: as soon as it becomes the only completion (after typing .g/, if no other directories matching .g*/ exist), it is shown by Ido as a possible completion. Similarly, in the original question case, typing .bak makes Emacs "un-ignore" and immediately show files ending in .bak, unless other non-ignored files match by Ido rules, so that the C-a is not always necessary to find an ignored file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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