<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Payton Swick</title>
    <description>The latest articles on DEV Community by Payton Swick (@sirbrillig).</description>
    <link>https://dev.to/sirbrillig</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F321620%2F0b18ccf2-7f8f-4559-a97d-6edbcf1f3c24.jpeg</url>
      <title>DEV Community: Payton Swick</title>
      <link>https://dev.to/sirbrillig</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sirbrillig"/>
    <language>en</language>
    <item>
      <title>grepdef: a quick way to search for definitions in code</title>
      <dc:creator>Payton Swick</dc:creator>
      <pubDate>Sat, 15 Aug 2020 21:18:50 +0000</pubDate>
      <link>https://dev.to/sirbrillig/grepdef-a-quick-way-to-search-for-definitions-in-code-27a9</link>
      <guid>https://dev.to/sirbrillig/grepdef-a-quick-way-to-search-for-definitions-in-code-27a9</guid>
      <description>&lt;p&gt;I live inside my code editor all day and something that I need to do pretty often is to search for the definition of a symbol (a variable, constant, or function). For example, I might be debugging a problem in a JavaScript file when I come across this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//...
let newData = translateDataFromRaw(rawData);
//...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I need to see &lt;code&gt;translateDataFromRaw&lt;/code&gt; to understand how it works. How can I do this?&lt;/p&gt;

&lt;p&gt;If I’m using an IDE or a project covered by a tags file, I can use the magical “jump to definition” feature. This is usually the best option as it is fast and nearly always accurate. However, if I’m using a simple code editor that does not have a “jump to definition” feature, or if the feature is still indexing my project (this can take quite a while sometimes), or if I want to find that function on the command-line for some other purpose without having to open up an IDE, then this will not work. What are my other options?&lt;/p&gt;

&lt;p&gt;I could use &lt;code&gt;grep&lt;/code&gt; to search my files for the string &lt;code&gt;translateDataFromRaw&lt;/code&gt; and then find the definition from among the results. This works well and is pretty simple, unless that function is used in a lot of places. If there are hundreds of results, it may take me forever to find the one I want.&lt;/p&gt;

&lt;p&gt;To help with this situation, I wrote a little command-line tool called &lt;a href="https://github.com/sirbrillig/grepdef"&gt;grepdef&lt;/a&gt;. With it, I could just run &lt;code&gt;grepdef translateDataFromRaw&lt;/code&gt; and I’ll instantly get the grep result for the definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ grepdef translateDataFromRaw
./src/translators.js:function translateDataFromRaw(data) {
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It’s way faster than “jump to definition” on some of the projects I work with. I even have it integrated into my editors with the plugins &lt;a href="https://github.com/sirbrillig/vim-grepdef"&gt;vim-grepdef&lt;/a&gt; and &lt;a href="https://github.com/sirbrillig/vscode-grepdef"&gt;vscode-grepdef&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work?
&lt;/h2&gt;

&lt;p&gt;It’s basically the same as the &lt;code&gt;grep&lt;/code&gt; technique I described above, except that it has two advantages:&lt;/p&gt;

&lt;p&gt;First, it uses a regular expression to find symbol definitions unique to a specific code language. This can definitely be inaccurate, but in my experience it’s close enough.&lt;/p&gt;

&lt;p&gt;Second, it uses &lt;a href="https://github.com/BurntSushi/ripgrep"&gt;ripgrep&lt;/a&gt; which is blazingly fast.&lt;/p&gt;

&lt;p&gt;All together, &lt;code&gt;grepdef&lt;/code&gt; saves me hours of development time. Maybe it will help you too?&lt;/p&gt;

&lt;p&gt;Right now it only supports two language types: PHP and JavaScript/TypeScript, but it’s built to be easily extensible to other languages. Please &lt;a href="https://github.com/sirbrillig/grepdef/issues/new"&gt;suggest them as issues&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>vim</category>
      <category>grep</category>
      <category>javascript</category>
      <category>php</category>
    </item>
  </channel>
</rss>
