DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Onorio Catenacci
Onorio Catenacci

Posted on

Using Ack To Search Elixir Code

I've been a fan of ack for quite some time.Β  Somehow (and I'm not quite sure how) once or twice it's found stuff in source code that VSCode simply misses (likely I was searching wrong with VSCode).Β  And there are a few things I've found that make it even more effective in searching Elixir code that I thought I'd share.

How Do I Configure Ack?

First, how to get ack?Β  If you're on a Mac then simply use Homebrew to install it: brew install ack. For those on Windows there's a Chocolatey NuGet package here. If neither of those are an option for you there are installation instructions on the homepage.

Secondly how should you configure ack for optimum use with Elixir?Β  I think the easiest way to set things up is to create an .ackrc file in your home directory.Β  So a ~/.ackrc file.Β  This is mine:

--type=elixir
--smart-case
--ignore-dir=deps
--pager=less -R -F

In order, this is what this .ackrc does:

  1. It tells ack to assume I'm searching elixir files (--type=elixir).Β  Ack is smart enough to ignore .git directories and other temporary files when it's searching for text.Β  This tells ack that I want to search only through Elixir files by default.
  2. Smart case tells ack to search case-insensitive unless I have an upper case letter in my search string. Obviously Elixir is case-sensitive so this is very helpful.
  3. Ignore directory (--ignore-dir) tells ack not to search in a given directory and below.Β  Since I don't usually want to see stuff that's brought in via Hex packages I ignore everything in any of the deps directory by default.
  4. Use less for the pager.Β  Less is a very neat tool that allows you to scroll forwards and backward through text output to the terminal one screenful at a time.Β  So by specifying less for my pager the ack output is sent out one page at a time and I can scroll forward and backward through it as I need to.

If you want to use ack without these settings (assuming you've added them to an .ackrc on your machine) you can simply pass the --noenv switch to ack on the command line and any settings in your .ackrc will be ignored.

How Do I Actually Search?

Open a terminal and then change to the root directory of the source code you want to search (e. g. ~/git/myelixirapp).Β  Then it's ack stringtosearchfor (e. g. ack defmodule).Β  If you want to search for a string containing spaces you need to enclose the string in double quotes (e. g. ack "defmodule MyGreatModule do").Β 

One very interesting option for ack is to tell it to search within a given range of code.Β  For instance if I'm looking for a function called "reverse_string_and_json_encode" then I can use a few parameters to ack on the command line to help me quickly narrow my search.Β  By using this command line: ack --range-start 'def reverse_string_and_json_encode do' --range-end 'end' I can find the exact definition of the function whereever it's defined.Β  Two important details about range-start and range-end:

  1. Range-start and range-end both support regular expressions.Β  For example, if I weren't quite sure of the name of the function I could also pass --range-start 'def reverse\w+' and --range-end 'end'
  2. You use single quotes (not double quotes) around the strings you use with range-start and range-end.

Are There Other Options?

If you don't care to use ack (or you have some sort of aversion to it), ack actually lists several good alternatives on their home page (under More Tools β†’ Other grep-like tools). I won't bother to repeat them here.Β 

What Else?

If you spot something I've got wrong, please let me know!Β  One reason I even bother to write this up is that I know of no method faster to find something I've gotten wrong than to share it with nerds. Β 

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.