PageRenderTime 97ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/sales/knowledge-base/how-to-search-source-code-using-ack.html

https://github.com/boyter/searchcode-server
HTML | 58 lines | 46 code | 12 blank | 0 comment | 0 complexity | 77175639e1841325e3a15070aa1c3dc5 MD5 | raw file
  1. <div class="row blue-container top50">
  2. <div class="container">
  3. <a href="/"><h1><img src="/images/searchcode_logo_white.png" height="40px" /></h1></a>
  4. </div>
  5. <div class="container">
  6. <h1 class="white">How to search source code in ack or ag</h1>
  7. </div>
  8. </div>
  9. <div class="row dark-blue-container">
  10. <div class="container">
  11. <p class="white em padtop10"><a class="btn btn-default btn-xs" href="/knowledge-base/"><span class="glyphicon glyphicon-link" aria-hidden="true"></span> Back to Knowledge Base</a></p>
  12. </div>
  13. </div>
  14. <div class="row">
  15. <div class="container em">
  16. <h2>ack <small>(command line)</small></h2>
  17. <a href="http://beyondgrep.com/">http://beyondgrep.com/</a>
  18. <p class="em">
  19. <button type="button" class="btn btn-success btn-xs">Windows</button>
  20. <button type="button" class="btn btn-success btn-xs">Linux</button>
  21. <button type="button" class="btn btn-success btn-xs">macOS</button>
  22. <button type="button" class="btn btn-success btn-xs">BSD</button>
  23. </p>
  24. <p class="em">Designed to be a better grep for source code. As such it includes defaults that make a great deal of sense in that context. Firstly you don't have to specify filenames as it assumes you want to start searching at the current directory and by default will recurse down the file tree. It also will ignore .svn and .git folders assuming (usually correctly) that you have no need to search inside them. Its output is also styled to make searching accross source code easier. Most of its arguments are similar to grep so it is instantly familiar. One really handy feature is being able to include or exclude file types using the --java --nophp syntax. It also ignores known backup files and other such redundant files.</p>
  25. <p class="em">For more performance you can also use the almost compatible version of ag "The Silver Searcher" <a href="http://geoff.greer.fm/ag/">http://geoff.greer.fm/ag/</a> which for most comparable searches is about 2-5x faster.</p>
  26. <p class="em">There is also another implementation which is similar to ack and ag called ripgrep <a href="https://github.com/BurntSushi/ripgrep">https://github.com/BurntSushi/ripgrep</a> which depending on your searching needs can yield even more performance over ag.</p>
  27. <p class="em">As with grep because ack looks at every file and depending on your query every byte in every file it can take a very long time to search across large or multiple code bases.</p>
  28. <p class="em">Some examples of searching over a codebase using ack are included below. All searches are run from within the directory you wish to search rather than having to specify the directory. Be sure to check out the <a href="https://beyondgrep.com/documentation/">documentation for ack</a>. Note that you can generally swap ack with ag if installed for faster results.</p>
  29. <ul class="list-unstyled text-left">
  30. <li><code>ack -i arraylist --java</code> Ignore case search for arraylist in java files</li>
  31. <li><code>ack if -A10 -B5 --shell</code> Show 10 lines after and 5 lines before shell scripts with the text if inside</li>
  32. </ul>
  33. <p class="em">Some timing tests for a directory using ack and ag on a directory with ~115 Gb of files.</p>
  34. <pre><code>du -hs
  35. 115G .
  36. time ack list -A10 -B5 --java
  37. real 0m12.560s
  38. user 0m7.270s
  39. sys 0m3.132s
  40. time ag list -A10 -B5 --java
  41. real 0m6.204s
  42. user 0m0.544s
  43. sys 0m0.930s
  44. </code></pre>
  45. </div>
  46. </div>