8 ms·
Ack is a grep-like tool, optimised for programmers
- jaredmcateer 12y agoI'm assuming the title got rewritten? It is probably important to note: "ack versions 2.00 to 2.11_02 are susceptible to a code execution exploit. Please upgrade to 2.12 or higher ASAP."
- tyilo 12y agoThe 2.12 version was posted December 3 2013, so no.
- antisocial 12y agoMy ex-colleague introduced this to me and I thank him every time I use ack. It is really so much better than grep. I have set up a bunch of aliases to search by file type and it makes me so productive.
- cake 12y agoI couldn't live without this tool today, very useful to quickly find where that method is used for example.
- Kiro 12y agoIs it better than just searching the project in your IDE?
- vidarh 12y agoDon't know about "cake", but my "IDE" is a command line.
- base698 12y agoI seem to find things faster than my coworkers. The ability to quickly filter out non relevant files and do nested searches of the searches is the strong point. Unix as an IDE and all.
- sillysaurus3 12y agoExamples, please? If you have the time.
- base698 12y agoThese are just a few examples I do pretty frequently: Nested Search: ag functionName | ag moreSpecificContextLikeArgs Find variable changed yesterday: git log -p --since yesterday | ag varName Find controllers changed yesterday: git log --oneline --showfile | ag controllers What files did I work on last week: git log --name-only --oneline --author me --since 1.weeks How many JS commits did I do last month? git log --since 1.months --author me --name-only | ag -i '\.js$' | wc -l How many JS commits did I do on each file last month? git log --since 1.months --author me --name-only | ag -i '\.js$' | awk '{arr[$1]++} END {for(i in arr) print arr[i]," - ",i}' | sort -r -n Change a "classname" from MyClass to BetterName: ag MyClass # verify it only finds what you think it will ag MyClass | awk -F':' '{print $1}' | sort | uniq | while read line do sed -i' ' 's/MyClass/BetterName/g' $line done
- a_e_k 12y agoNested searches are handy. They can also be used to find cases of one thing within a few lines of proximity to another: ack -C5 firstThing | less /secondThing One bonus ack trick that I like also like is bulk loading into Emacs for further manipulation (e.g., multi-occur and then occur-edit-mode): emacsclient -n `ack -l functionName`
- joel_perl_prog 12y agoI've often wondered about that. For me, the development environment is extremely minimalistic, by some types of standards. Linux itself, including tools like ack + vim. I use various vim tricks, not up to the point of it being my de-facto OS (as is possible to do!). From what I can observe, I am generally faster than my co-workers. But it could be possible that with a great IDE, I could be faster yet. I don't feel any tug to leave, but could just mean I'm ignorant of that truly better way.
- deleted 12y ago[deleted]
- coffeeaddicted 12y agoIt's better if you don't have the IDE for that project open already. Or if you search for a project which doesn't come with project files for your IDE of choice. Or if you want to pipe the results. I work a lot with IDE's but still use ack-grep regularly.
- efuquen 12y agoIt's a liberating feeling to not be a slave to your IDE. It will allow you to explore more things outside some IDE walled garden, live a little ;-)
- rbonvall 12y agoIn my case, my IDE is the command line. ack is one of its plugins. The builtin plugins are also ok (find, ls, mv, etc.). I can create my own plugins for my IDE, and there are even package managers to install new plugins (yum, apt-get).
- pie 12y agoIt's also worth checking out The Silver Searcher: https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- pmelendez 12y ago1000 times this... it is exactly like ack-grep but faster :)
- 5h 12y agoI normally tell people to use ack because it's like grep but faster (owing to it's sensible defaults) ... if I use this I'm worried I might go too fast and travel backwards in time or something.
- csgavino1 12y agoGive ag a shot, you'll be able to relive the emotions you felt when you switched to ack from grep, but this time you're switching to ag from ack.
- djeikyb 12y agoOne thing I miss a little is that ack has the super convenient: ack --java "foo" while with ag you write: ag -G"\.java$" "foo" But yes, ack and ag feel pretty identical except for the speed. Most of the time the speed improvement is irrelevant to me, except sometimes now I'll use ag in my home folder, and it's still fairly snappy.
- tsenkov 12y agoJudging by this: https://news.ycombinator.com/item?id=7710269 https://news.ycombinator.com/item?id=7710269 I guess ag now supports that, too.
- coffeeaddicted 12y agoThat was too much typing anyway. When you mostly work with one language something like this is nice (in my case c/c++): alias ack-cpp='ack-grep --type=cpp --type=cc'
- 616c 12y agoFor some reason, it took me one or two minutes of rereading to realize it was ack, not awk. I think this website was going to be some ironic trash-talking about grep. Then I saw "written in Perl" and I got so confused my head almost exploded. Anyway, neat tool. Will check it out soon.
- prakashk 12y ago> I think this website was going to be some ironic trash-talking about grep. Andy Lester, the primary author of ack, is one of the nicest guys I know of. You wouldn't see any trash-talking on that site. He even changed the name of the site from "better than grep" to "beyond grep" [1]. In fact, he gives props to similar tools like ag and others [2]. [1] https://news.ycombinator.com/item?id=5578304 https://news.ycombinator.com/item?id=5578304 [2] http://beyondgrep.com/more-tools/ http://beyondgrep.com/more-tools/
- deleted 12y ago[deleted]
- raverbashing 12y agoYeah, I think grep sucks as well, that's why I created my own little ack thing (since it's in a sorry state I won't post it here, and it will attract the rage of people for not being compatible with grep/in python/no docs/etc)
- djeikyb 12y agoI use ag[2], which is pretty much the same as ack, but even faster. The other day I was using it to find all instances in all projects of a list of problematic method names[1], in case anyone wants to see a real world use case. [1]: http://unix.stackexchange.com/questions/108471/no-output-using-parallel-in-tandem-with-ag-or-ack/108472#108472 http://unix.stackexchange.com/questions/108471/no-output-usi... [2]: https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- masklinn 12y agoThe only annoyance with ag is it does not have ack's quick filters e.g. ack --py versus ag -G '\.py$' (and ack's type flags can include multiple file extensions).
- wahnfrieden 12y agoIt does have them now, update to the latest release :)
- TorKlingberg 12y agoAck is great, but watch out if you have any source files with unusual file name extensions. Ack will only search file types it knows about. Also if you have your whole source tree in your editor or IDE, then you may as well search there instead.
- theOnliest 12y agoThat's true by default, but it's trivial to add your own filetypes with a .ackrc file.
- npongratz 12y agoAddressed in ack's FAQ [0], and in its own section of the manual [1]. The manual explains: "This is done with command line options that are best put into an .ackrc file - then you do not have to define your types over and over again." Then comprehensively describes options for both command line and .ackrc. [0] http://beyondgrep.com/documentation/ack-2.12-man.html#faq http://beyondgrep.com/documentation/ack-2.12-man.html#faq [1] http://beyondgrep.com/documentation/ack-2.12-man.html#defining_your_own_types http://beyondgrep.com/documentation/ack-2.12-man.html#defini...
- TorKlingberg 12y agoYes, I should have added "by default".
- ritchiea 12y agoYou can also include the -a flag and search all files
- olalonde 12y agoIs there a code repository somewhere? Can't seem to find one...
- theOnliest 12y agohttps://github.com/petdance/ack2 https://github.com/petdance/ack2 I'll send Andy a PR about putting the Github link on the site somewhere.
- petdance 12y agohttps://github.com/petdance/beyondgrep https://github.com/petdance/beyondgrep is the repo for the site.
- deleted 12y ago[deleted]
- VeejayRampay 12y agoI recently found out about git-grep. It's good and quite fast.
- gegtik 12y agoIt should be noted that "faster" is due to the fact that it limits itself to searching the subset of files that have code extensions. I've tested grep against ack and ag for large text files and grep won handily, especially the latest version of grep. Also note you can use GNU Parallel to run multiple greps
- djeikyb 12y agoYeah, I think it's important not to throw grep away. Ack is really for when you don't know or can't be bothered to explicitly mention the (several) specific files to search.
- petdance 12y ago> I think it's important not to throw grep away Exactly. There's no reason that you can't have grep AND ack AND ag in your toolbox to choose from.
- fra 12y agoRight, and grep lets you filter files by extensions so there's really no reason to use ack or ag... alias ack="grep --include=*.c"
- 0x0 12y agoI used this for a while, but got bitten by the fact that, by default, it does not search all file types. :-/
- davexunit 12y agoI'll stick with grep, thanks. M-x rgrep in Emacs works great.
- michaelsbradley 12y ago'M-x ag', is just a package install away: https://github.com/Wilfred/ag.el https://github.com/Wilfred/ag.el
- senthilnayagam 12y agohttps://github.com/monochromegane/the_platinum_searcher https://github.com/monochromegane/the_platinum_searcher I use pt for stuff which is not in git, written in go and it is fast
- Flenser 12y agoPt also has first party builds for Windows and Mac
- herokusaki 12y agoIs there a tool that is to find what ack is to grep?
- hoelzro 12y agoI usually use ack -g, which lists files that match the regex, rather than lines of files that match it.
- e12e 12y agoIt depends what you mean... as others have mentioned[1], neither ack or ag are particularly fast compared to grep, they just give you a lot of specialized context (search the right files). As such, what would be to find as ack is to grep? A find that automatically filters out files that are not source code files? [1] Things might have changed since the last time I personally tried this, at the time grep was significantly faster, especially for fixed string searches -- but then again, I never tried to coerce up a command line that gave the same kind of output that ack/ag does (which could probably be hammerd out with help of awk). So don't take my comment to suggest that these tools aren't valuable, just maybe not for the reason some people (notably not the authors of said tools) claim.
- herokusaki 12y ago> find that automatically filters out files that are not source code Not just that but an extensible set of file type filters that are simple to invoke is what I had in mind. E.g., the tool would let you perform searches like find++ --Python projects/archive/200? or find++ --video trailer where in the latter case the hypothetical find++ would refer to my config to get a list of video file extensions and then print a list of all files in the current directory and its subdirectories with the word "trailer" in their name. For better effect it would ship with useful filters like "--video" by default.
- e12e 12y agoRight. It's not entirely straight forward to link up the mime database (via eg: file) and generating filters for use by find. Basing filters off of filenames isn't a very good idea -- and actually a little regressive in my opinion -- after all project/bin/foo (executable) might be a python or perl or whatever script -- not just a binary file. But first getting all files via find, then testing with file, and finally matching against mime-type doesn't sound like something that's going to be as fast as possible... I tried to see if maybe gvfs (gio - gnome io) could help, but couldn't really find anything directly applicable (although there is a set of gvfs command line tools, like gvfs-ls, gvfs-info, gvfs-mime).
- NicoJuicy 12y agoThis has found it's way to HN a long long time ago: https://news.ycombinator.com/item?id=975511 https://news.ycombinator.com/item?id=975511
- pretz 12y agoIt's worth noting that if you're always searching in a git repo, git-grep is pretty comparable to ack and you already have it installed.
- badman_ting 12y agoTotally, I use it all the time now. Here's a pretty great writeup on git grep that was on HN recently: http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-ninja/ http://travisjeffery.com/b/2012/02/search-a-git-repo-like-a-...
- revscat 12y agoFor Java programmers who use Silver Searcher or ack, this lets you search all jars in a directory tree for a given string. Requires GNU Parallel: function ffjar() { jars=(./**/*.jar) print "Searching ${#jars[*]} jars for '${*}'..." parallel --no-notice --tag unzip -l ::: ${jars} | ag ${*} | awk '{print $1, ":", $5}' } Because it uses parallel it spreads the workload across CPUs. I use this frequently when I have to update/rewrite/create build scripts, and I know a class exists but not which jar file it lives in.
- garblegarble 12y agoThat's pretty neat! I use the following (slower, but short enough that it sticks in my memory so I can do it at a colleague's machine): find . -name "*.jar" | xargs -tn1 unzip -l | grep SomeClass
- burntsushi 12y ago`xargs` also has a `-P` flag which will instruct it to spread work over multiple processes. Given that you already have `-n1`, adding a `-P 0` will have it automatically spread out over all your CPUs.
- garblegarble 12y agoAwesome, that's good to know!
- ole_tange 12y agoBe aware that xargs does not deal nicely with race conditions: (Parallel grep) http://www.gnu.org/software/parallel/man.html#differences_between_xargs_and_gnu_parallel http://www.gnu.org/software/parallel/man.html#differences_be...
- ole_tange 12y agoConsider running 'parallel --bibtex' once.
- omegote 12y agoBeen using ack-grep for years. In systems where it's not installed I just use grep -Hnir
- Antwan 12y agoSeen http://beyondgrep.com/security/ http://beyondgrep.com/security/ Didn't read further.
- fdsary 12y agoI thought we were done with this already? https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- gejjaxxita 12y agoThe problem with such tools is often their lack of ubiquity. I don't want to start using ack, forget a lot of my grep knowledge, only to ssh into a server and need grep. The benefit of grep's ubiquity outweighs any small advantage ack has in usability.
- davorb 12y agoack is a tiny perl script that you can simply wget and add to your path. I hear what you are saying, and I think that it applies to a lot of utilities, but not ack. Imho ack is so much better than grep that it is worth the hassle of having to install it every now and then.
- mdavidn 12y agoTry git-grep. It ignores the .git directory, and it's as ubiquitous as Git.
- bch 12y agoHow does this compare to cscope[0] ? edit: or ctags[1] ? --- [0] http://en.wikipedia.org/wiki/Cscope http://en.wikipedia.org/wiki/Cscope [1] http://en.wikipedia.org/wiki/Ctags http://en.wikipedia.org/wiki/Ctags
- astine 12y agocscope and ctags are language syntax dependent searching tools for c-like programming languages. They let you search specifically for all instances of a function names 'foo' for example. Ack is instead just a normal pattern matcher like grep except that it has some cleverness by which it knows not to search certain file types and directories. It will return all lines which match a string rather than just variable names or functions.
- bch 12y agocscope lets you search for arbitrary text strings and egrep patterns as well. "The fuzzy parser supports C, but is flexible enough to be useful for C++ and Java, and for use as a generalized 'grep database' (use it to browse large text documents!)"[0] Exuberant Ctags supports 41 languages[1], incl. javascript, Tcl, Ruby, TeX, awk, etc. I was wondering if there are obvious ack killer features or areas where it's remarkably superior. [0] http://cscope.sourceforge.net/ http://cscope.sourceforge.net/ [1] http://ctags.sourceforge.net/languages.html http://ctags.sourceforge.net/languages.html
- petdance 12y agoThey solve different problems. Ctags and cscope index a corpus of source code, usually tied into another tool, like Ctrl-] in vim. ack searches the files every time.
- eliben 12y agoSimilar tool in pure Python: https://github.com/eliben/pss https://github.com/eliben/pss
- abind 12y agoThank you for this! `pip install pss` is one of the first things I do on a new computer :)
- username42 12y agoWhy did they choose the same name as minix compiler ack (http://tack.sourceforge.net/ http://tack.sourceforge.net/) ?
- foz 12y agoI used Ack a lot when I was coding Perl (it's been a while). After I switched to Ruby, I used rak [1], which seemed easier to use most of the time, and nearly identical. However, when you just want to find stuff fast, it's annoying to have to deal with Perl/CPAN or RVM/Rubygems, especially when the dependencies are not installed on your server/workstation. That's why I've switched to silver searcher (ag) [2], as it can be installed with any OS package manager (brew, apt, yum). [1] http://rak.rubyforge.org http://rak.rubyforge.org [2] https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- ediblenergy 12y agoag is not available as a package on debian stable. Ack is though; as ack-grep. So if you don't want to mess with CPAN that's fine. The non-CPAN instructions are right on the website.
- petdance 12y agoThere's a list of other tools for search source code besides ack at http://beyondgrep.com/more-tools/ http://beyondgrep.com/more-tools/, including other grepalikes and indexing tools like ctags and cscope. I suggest that you need not limit yourself to only one tool for your code searching. Toolboxes FTW.
- andrelaszlo 12y agoOld thread here: https://news.ycombinator.com/item?id=6075083 https://news.ycombinator.com/item?id=6075083
- anotherevan 12y agoAnd just to warp things even further, use ack to find the files you're after, then run through grep: ack -f --css | parallel grep search_term
- sitaramc 12y agoGrep is just as good, and with the recent order of magnitude speed improvement on non-C locales that they made -- see https://lwn.net/Articles/586899/ https://lwn.net/Articles/586899/ -- which may not have made its way into distros yet, it's easily the best option. I have a simple wrapper over egrep (see https://github.com/sitaramc/ew https://github.com/sitaramc/ew ) that adds those little extras (ignoring binary files, ignoring VCS directories...). I'm sure it's improved since the days I tried it, but I tend to be permanently prejudiced against tools where the author can't/won't document the file selection logic and says "there's really no English that explains how it works" when someone asks.