An Alfred workflow for:
- searching for directory trees with a regular expression against the file name like the
find
command. - searching for lines matching a regular expression like the
grep
command. - opening the matching files or lines directly in your text editor such as MacVim, Emacs, Sublime Text, and TextMate
- Alfred Powerpack
- OS X Mavericks or higher (Ruby 1.9.3 or higher)
- Text editor which allows you to open a file and go to a specific line
- Download and open the workflow
- Review the workflow to add custom hotkeys. An example is shown below:
ctrl+command+/
Run the workflow with last queryctrl+command+j
Find Nextctrl+command+k
Find Previous
In Alfred's File Actions, select grep
to specify where the workflow searches for files in.
The contents of the directory are listed when the query is empty.
You can search for names of files as well as lines of files with a pattern.
Whitespace-separated multiple patterns are treated as AND search.
To open the results with your text editor, just select it.
By default, Sublime Text will be launched. To change the default application, edit settings.json
in the workflow directory.
The --d
flag means selecting only directories.
A string starting with --
will be treated as the search options. You can write it everywhere in the query.
The --f
flag means selecting only files.
The --r
flag means searching subdirectories recursively.
To specify multiple options, just add the character of the option without spaces. Or you can specify the options separately like --r --f
.
The pattern is an unquoted string like h
, or is a regular expression literal like /.../
. It is optionally followed by the flag which determines the search target like -x
.
The -x
flag means "search for file extensions with that pattern".
The -p
flag means "search for file paths with that pattern".
This example selects the files where their extensions contain "h" and their paths contain "gl/".
To do grep
, type the pattern optionally followed by the -g
flag.
You can specify multiple search targets to one pattern. When those are omitted, the flags -pg
meaning "search for file paths or lines with that pattern" will be used implicitly.
Note that the multiple search targets are handled as "short circuit OR". The contents of a file will not be searched if the path matches the pattern.
To search for only lines with the pattern, add an explicit -g
flag.
Regular expression pattern is also available by using the /.../
literal.
The -c
flag means "search for file contents with that pattern".
The -!
flag means "exclude pattern".
Note that /foo/ -pg!
is expanded as /foo/ -p! /foo/ -g!
.
-i
Ignore case-I
Case sensitive
-p
Search for file path likebar/foo.txt
-n
Search for file name likefoo.txt
-b
Search for file basename likefoo
-x
Search for file extension liketxt
-t
Search for file tags using OpenMeta
-c
Search for file contents-g
Search for lines of files
-!
Exclude pattern
--r
Search subdirectories recursively--l
Non-recursive search
--d
List directories--f
List files--e
List any type of files
--v
List visible files--h
List hidden files--a
List all files
--n
Sort by file name--x
Sort by file extension--s
Sort by file size--m
Sort by modification time--g
Show direct child files of the search paths to the head of the results--u
Turn off the above feature
--A
Show all lines of matching files