Using Silver Searcher, how can I search for:
Other preferences: would like to have case insensitive search and search through dotfiles.
Tried to alias using this without much luck:
alias search="ag -g $1 --smart-case --hidden && ag --smart-case --hidden $1"
To search for files in File Explorer, open File Explorer and use the search box to the right of the address bar. Tap or click to open File Explorer. Search looks in all folders and subfolders within the library or folder you're viewing. When you tap or click inside the search box, the Search Tools tab appears.
Click the Start button to go to the Start screen, then start typing to search for a file. The search results will appear on the right side of the screen. Simply click a file or folder to open it.
According to the man page of ag
-G --file-search-regex PATTERN Only search files whose names match PATTERN.
You can use the -G
option to perform searches on files matching a pattern.
So, to answer your question:
root@apache107:~/rpm-4.12.0.1# ag -G cpio.c size rpm2cpio.c 21: off_t payload_size; 73: /* Retrieve payload size and compression type. */ 76: payload_size = headerGetNumber(h, RPMTAG_LONGARCHIVESIZE);
the above command searches for the word size
in all files that matches the pattern cpio.c
Reference:
man page of ag
version 0.28.0
If you are looking for a string in certain file types, say all C sources code, there is an undocumented feature in ag
to help you quickly restrict searches to certain file types.
The commands below both look for foo
in all php files:
find . -name \*.php -exec grep foo {} ag --php foo
While find + grep
looks for all .php
files, the --php
switch in the ag
command actually looks for the following file extensions:
.php .phpt .php3 .php4 .php5 .phtml
You can use --cpp
for C++ source files, --hh
for .h
files, --js
for JavaScript etc etc. A full list can be found here
Try this:
find . | ag "/.*SEARCHTERM[^/]*$"
The command find .
will list all files.
We pipe the output of that to the command ag "/.*SEARCHTERM[^/]*$"
, which matches SEARCHTERM if it's in the filename, and not just the full path.
Try adding this to your aliases file. Tested with zsh but should work with bash. The problem you encountered in your example is that bash aliases can't take parameters, so you have to first define a function to use the parameter(s) and then assign your alias to that function.
searchfunction() {
echo $(ag -g $1 --hidden)
echo $(ag --hidden -l $1)
}
alias search=searchfunction
You could modify this example to suit your purpose in a few ways, eg
-l
flag depending on whether or not you want text results to show the text match or just the filename[Edit: removed unnecessary --smart-case flag per Pablo Bianchi's comment]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With