Last major edit (later minor edits)
> (You can also work in the other direction, reusing an Isearch
> regexp as a content-matching regexp. See [[Icicles - History Enhancements#InsertPreviousInputs|Using Completion to Insert Previous Inputs: `M-o']].)
|Previous: Icicles - Buffer-Name Input||Icicles||IciclesIndex||Next: Icicles - Persistent Completions|
Emacs offers two main functions for reading minibuffer input with completion:
‘read-file-name’. Icicles uses both of these, and it enhances each of them in various ways for use in your own EmacsLisp code. These two functions can each be used to read file-name input, but they do so in very different ways.
This section provides a reminder description of these two functions, and then it describes the main Icicles commands that read file names.
You do not need to use these Icicles commands to get the benefits of Icicles enhancements to
‘read-file-name’. What these commands offer are additional benefits, which you might or might not appreciate.
By default, that is, if you do not change the value of user option `icicle-top-level-key-bindings’, some commonly used Emacs commands for accessing files are remapped to Icicles commands. For example, command
‘find-file’, is by default remapped in Icicle mode to multi-command
‘icicle-file’. Remapping means that any keys, such as
‘C-x C-f’, that would normally be bound to
‘find-file’ are instead bound to
‘icicle-file’ when you are in Icicle mode.
‘icicle-top-level-key-bindings’ to see how this works. In particular, note that if you normally bind a different command to one of the keys mentioned here then the corresponding remapping in
‘icicle-top-level-key-bindings’ will not have the advertised effect.
For example, if you normally use the key bindings provided by library
ffap.el (FindFileAtPoint), meaning that you invoke command
‘ffap-bindings’, then for you
‘C-x C-f’ is bound to
‘find-file’. By default, option
‘icicle-top-level-key-bindings’ does not remap
‘icicle-file’, so even in Icicle mode
‘C-x C-f’ remains bound to
‘find-file-at-point’. If you want
‘C-x C-f’ to be bound to
‘icicle-file’, then change the
‘find-file’ entry in
‘icicle-top-level-key-bindings’ (or add another entry for
What has just been pointed out for Icicles file commands applies more generally to all Icicle-mode command remapping provided by option
In addition to the usual Icicles key bindings, during file-name completion the Icicles commands that read file names bind the following keys dynamically in the minibuffer keymaps, so you can use them while to perform various operations on the fly when you input a file name. These minibuffer bindings are also available for autofile bookmark commands.
‘icicle-up-directory’) to navigate up the directory hierarchy. It removes the last directory component (and any partial file name) from your minibuffer input. (For Emacs versions before Emacs 22, this feature is available only for completion of absolute file names.)
C-c +’ (
‘icicle-make-directory’) to create a directory on the fly.
‘S-delete’to delete the file named by the current completion candidate.
‘icicle-all-candidates-list-alt-action’) to open Dired on the currently matching file names. That is, it opens a special Dired buffer that contains only the matching files. You are prompted for the Dired buffer name. See Alternative Actions.
‘icicle-bookmark-file-other-window’) to visit a bookmarked file or directory. This is available only if you use library Bookmark+ (bookmark+.el). It is a multi-command, so you can actually visit any number of file bookmarks. When finished, you can continue with non-bookmark file-name completion.
C-x a +’ or
‘C-x a -’to add or remove tags for a file. These are delicious-style tags – any labels you like. If there is not already an autofile bookmark for the file, then one is created. (The autofile bookmark is where the tags are persisted.) You need library Bookmark+ for this feature. See also tagging files.
‘C-x a a’to bookmark a file, that is, to create an autofile bookmark. This is the same as `
C-x a +’ and
‘C-x a -’, except that it does not prompt you for tags to add or remove. You need library Bookmark+ for this feature.
‘C-x C-t *’to narrow the current file-name completion candidates to those that are tagged with all of the tags you specify. You are prompted for the tags.
C-x C-t +’ – same as
‘C-x C-t *’, except it narrows to the files tagged with any of the specified tags.
C-x C-t % *’ to narrow the candidates to those whose tags all match a specified regexp. You are prompted for the regexp.
C-x C-t % +’ – same as `
C-x C-t % *’, except it narrows to the files that have any matching tag.
In many cases for Icicles multi-commands that read a file name, you can use
‘M-n’ to pick up the file name at point, or if none then the name of the file you are currently visiting.
Note: Whether a command supports file-name globbing wildcards such as
‘*’ is independent of whether it uses
‘completing-read’. It is the command itself that offers such support or not. Globbing takes place only after the file name (with wildcards) is read. All Icicles commands that visit files (all of the commands mentioned in this section) let you use globbing wildcards. Remember also that file-name globbing, with its special wildcards, has nothing to do with regexp completion matching. See Icicles - Special Characters in Input Patterns for more information about file-name globbing.
‘read-file-name’ uses relative file-name completion.
It is specialized for file-name input with completion. It knows about files and file names for your current platform. It knows about Emacs remote file name syntax (Tramp, Ange FTP). And starting with Emacs 23,
‘TAB’ also completes environment variables during
‘read-file-name’ is the most flexible way to read a file name in Emacs, and it is the traditional way. Unless stated otherwise, “file-name completion”, even in the Icicles doc, refers to
‘read-file-name’ reads input, only the file name itself, not the directory portion, is used for matching. The directory is understood to be the directory that is present in the minibuffer, or the value of variable `default-directory’ (which you can change using command
‘cd’, for instance) if there is no directory there.
Whether the value of
‘default-directory’ is automatically inserted in the minibuffer is controlled by user option
‘insert-default-directory’. But if you do not change the directory seen in the minibuffer then the behavior is the same whether or not the
‘default-directory’ name is present in the minibuffer: it is the assumed directory. If you prefer, you can delete the directory name first, using
‘read-file-name’, you can thus use apropos completion to match a file-name substring, without needing to prefix the substring with `.*’ in the minibuffer. For example, to match the file named `
favorite-foo-file.bar’ in directory
‘/some/path/to/my/’, you need not use
foo’; it is sufficient to use either
‘completing-read’ is a general function for reading input with completion. It is not specially designed for reading file names. It knows nothing about files and file names. It knows nothing about remote file-name syntax. When
‘completing-read’ reads input, it makes no use of
‘default-directory’. The completion candidates are treated as simple strings; they are not really treated as file names.
Icicles commands that use
‘completing-read’ to read a file name typically read an absolute name, that is, a name that includes the directory portion. This means that you can match against any part of the full name, including any directory components. The directory portions of the candidate file names need not be the same – you can thus complete against a set of files in multiple directories.
Icicles commands that use
‘read-file-name’ include all multi-commands, such as
‘icicle-find-file’, that are defined using
‘icicle-define-file-command’. Vanilla Emacs command
‘find-file’ is another example of a command that uses
Icicles commands that use
‘completing-read’ to read file names include the multi-commands
‘icicle-locate-file-no-symlinks’. These are defined using
There are also
‘-other-window’ versions of all of the Icicles commands that read file names.
The Icicles commands that use
‘completing-read’ to read file names have an additional feature: you can use a prefix argument to tell them to combine the last modification date with the file name, as a multi-completion. This means that you can easily look up files whose modification time or date matches some (regexp) criterion, such as being sometime in July 2008.
When using a command that reads an absolute file name, remember that, to save space, you can use `
C-x .’ to toggle hiding of the common match portions of the candidates in
‘*Completions*’. This portion is often a long directory substring.
‘icicle-file’ is bound, by default, to
‘C-x C-f’ in Icicle mode, thus taking the place of
‘find-file’. It combines multi-commands
‘icicle-find-file-absolute’. With no prefix argument, it matches relative file names; with a prefix argument, it matches absolute names (as ordinary strings). With a negative prefix argument, you can match also the modification date.
An additional feature of
‘C-x C-f’ with a prefix arg) is that candidates that are directory names are highlighted in buffer
‘*Completions*’ using face
An additional feature of command
‘C-x C-f’ with no prefix arg) is that if you use a prefix arg when acting on an individual file-name candidate then the file is visited in read-only mode. The same invocation of
‘C-x C-f’ can thus open multiple files, some in read-only mode, some not.
(There is also multi-command
‘icicle-find-file-read-only’, unbound by default, which is the same as
‘icicle-find-file’ but with the prefix arg behavior flipped: with no prefix arg when you act on a candidate file it is visited read-only.)
Starting with Emacs 23, command
‘icicle-find-file’ (that is,
‘icicle-file’ with no prefix argument) is an alias for command
‘icicle-find-file-of-content’, which lets you optionally provide a regexp pattern to match against file content. In this it is similar to the buffer-switching multi-command
If you provide a pattern to match file content then all files whose names match the file-name part of your input are searched for the content pattern. Icicles looks only for a single content-match within each file it tests. Choose a candidate file just visits it normally — it does not move to that tested match or to any other content-match position. Matching is used only to filter candidate files.
However, if your input includes a content-matching part that matches, that part is automatically added to the Isearch regexp history,
‘regexp-search-ring’, whenever you hit
‘S-TAB’ to complete your input. This means that when you visit the file you can immediately search for matches using
(You can also work in the other direction, reusing an Isearch regexp as a content-matching regexp. See Using Completion to Insert Previous Inputs: `M-o'.)
Content-searching is obviously more costly than file-name matching, so clearly if you can provide some information about the file name, that improves performance. IOW, the more you can limit the number of files to search, the better.
To search file content, the candidate files are visited, that is, buffers are created for them and searched. By default, after the command is finished these buffers are killed, except for those you actually chose as completion candidate(s) and any that existed prior to invoking the command.
This automatic extra-buffers cleanup is controlled by option `icicle-kill-visited-buffers-flag’. But providing a prefix argument to the command flips the behavior specified by that option for the command duration.
You can use option `icicle-find-file-of-content-skip-hook’ to specify patterns for file names to exclude from content-searching when you provide a content-matching pattern to
In Dired there is a related content-matching multi-command,
‘icicle-visit-marked-file-of-content’ (bound to
‘C-O’ for other-window), which you can use to visit marked files and subdirectories whose content matches a regexp.
(Prior to Emacs 23,
‘icicle-find-file’ is an alias for
‘icicle-find-file-no-search’, which does not search file content.)
See also Icicles - Searching Named Containers.
‘icicle-find-file-in-tags-table-other-window’ let you visit files that are listed in the current Emacs tags table. You can think of these potential completion candidates as all of the files in a project defined by the tags table.
You can use
‘icicle-recent-file’ to open any file that you have visited recently, perhaps in a previous Emacs session.
Because Icicles lets you easily sort and filter candidates, consider customizing option
‘recentf-max-saved-items’ to a large number, such as 1000. The default number (20) is very small because it assumes that you will access the candidates using a menu.
You can use
‘icicle-locate-file’ to find a file when you do not know precisely what directory it is in. The former requires GNU/Linux or UNIX command
‘locate’, to work. The latter does not require any external program.
Because it takes advantage of
‘locate’ having indexed files on your file system,
‘icicle-locate’ can be much faster than
‘icicle-locate-file’. Otherwise, these two Icicles commands work similarly.
Since it does not use an index,
‘icicle-locate-file’ looks throughout a given directory, including throughout all of its subdirectories. Command
‘icicle-locate-file-no-symlinks’ is the same, except that it does not follow symbolic links. Both of these commands respect option `icicle-ignored-directories’, which is a list of directories to ignore – by default, version-control directories.
By default, the target directory for
‘icicle-locate-file’ is the current directory, but if you supply a non-negative numeric prefix argument (non-positive means include the date), then you are prompted for the directory to search.
If you use a plain prefix arg (
‘C-u’) or a double plain prefix arg (
‘C-u C-u’) then you are prompted to choose a set of directories to use. This choosing uses multi-command
‘icicle-directory-list’. Icicles searches for file-name matches under each of the directories you choose. For
‘C-u C-u’, the candidate multi-completions include the last file-modification date.
In addition, if you turn on the use of proxy candidates (`
‘icicle-directory-list’ also offers proxy candidates that are variables, such as
‘load-path’, whose values are lists that include directories. Choosing a proxy candidate adds all of its directories to the set to be searched.
If you use the root of your file system as the search directory, then the Icicles file-locating commands will match completion candidates anywhere in your file system.
This can be quite useful. It gives you much of the power of the Unix
‘find’ command just for completing input. And with incremental completion (see Icicles - Icompletion), you can see what matches your input as you type.
Obviously, if you use your entire file system as the set of completion candidates and you use
‘icicle-locate-file’ (because you do not have available the external program
‘locate’), then gathering and matching such a large set of file names can take some time.
On my hard drive, for instance, there are 36 GB full of files, and it takes about 40 seconds to gather all of the file names. In spite of this inconvenience, this functionality can be useful. And of course searching a shallower directory tree presents less of a performance penalty – you pay for what you get.
However, even if you do not have command
‘locate’, there of having your cake and eating it too. You can gather all of the file names in your file system once, and save that list of completion candidates to a cache file on disk, as a snapshot. See Icicles - Persistent Completions for how to do this.
‘completing-read’ has no understanding of file-name syntax, including remote file-name syntax,
‘C-u C-x C-f’) and similar commands are similarly ignorant. (You can nevertheless use `
C-. with these Icicles commands, to toggle respect of
‘completion-ignored-extensions’.) In particular, these commands will not let you complete to a remote file name if the current directory is local. They also will not let you complete to a file name in a different local directory.
Because all Icicles commands that read file names use lax completion, you can nevertheless visit a file in a different directory (remote or local) from the current one, even though you cannot complete your input to such a name. That is, you can always use
‘RET’ with any file name as minibuffer input.
So how can you complete your input to an absolute file-name in a different directory? By retrieving a saved candidate set that has such absolute names and then completing against that set. For example, you can retrieve a set that represents files on a remote machine and complete to their names even from a local directory. All that counts for
‘completing-read’ is that your input can match candidates, where that matching is ordinary (apropos or prefix) string matching.
To create such a saved set of names, you can visit the directory (perhaps remote) that contains the files and then use
‘C-u C-x C-f’ and `
C-}’ to save the candidates. You can later retrieve this saved set for completion, no matter what the current directory is. As another example, you can use
‘icicle-locate-file’ in a remote directory to create a saved set that includes remote files that are all somewhere under that remote directory.
In addition, you can add more file names to an existing saved set using `
‘M-S-mouse-2’ – see Add More Saved Candidates. The added names can come from a different directory than files already in the saved set. In this way, you can build up a saved set that includes files from any directories, some of which can be local and some remote, some remote from one host, some remote from another, and so on. You can create a saved set with any mix of absolute file names from any locations.
‘C-u C-x C-f’(absolute), not
Finally, although the commands that read absolute file names are essentially ignorant of directory hierarchies and of file names as such, so that they treat their candidates only as simple strings, a few of these commands nevertheless define their domain of possible file-name candidates relative to some starting directory.
This is the case for
‘icicle-locate-file’ (and their variants). For these commands, you can use
‘C-c C-d’ (think UNIX command
‘cd’) during completion to change the current working directory (
‘default-directory’) on the fly. You are prompted for the directory. The domain of possible candidates is recomputed relative to the new
‘C-c C-d’ this way as many times as you like. You can use this feature to add file names from different directories to a saved set of candidates. When the command is finished, the original
‘default-directory’ is restored.
|Previous: Icicles - Buffer-Name Input||Icicles||IciclesIndex||Next: Icicles - Persistent Completions|