<?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: Richard Glen Domingo</title>
    <description>The latest articles on DEV Community by Richard Glen Domingo (@chardskarth).</description>
    <link>https://dev.to/chardskarth</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%2F803371%2F57ebcdb3-cb44-42be-bbbc-48b8edf07bfa.jpeg</url>
      <title>DEV Community: Richard Glen Domingo</title>
      <link>https://dev.to/chardskarth</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chardskarth"/>
    <language>en</language>
    <item>
      <title>How to Fuzzy Search: Finding File Names and Contents using Bash Scripting and Commandline Tools</title>
      <dc:creator>Richard Glen Domingo</dc:creator>
      <pubDate>Sun, 30 Jun 2024 16:53:52 +0000</pubDate>
      <link>https://dev.to/chardskarth/how-to-fuzzy-search-finding-file-names-and-contents-using-bash-scripting-and-commandline-tools-52kd</link>
      <guid>https://dev.to/chardskarth/how-to-fuzzy-search-finding-file-names-and-contents-using-bash-scripting-and-commandline-tools-52kd</guid>
      <description>&lt;p&gt;This blog explains a bash script that does a fuzzy search against file names and file contents of all the files in the directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;When searching for specific text (or code), you often rely on your IDE or file manager.&lt;br&gt;
But if you're like me who wants to:&lt;br&gt;
&lt;br&gt;&lt;br&gt;
        1. Search files fast against different directories&lt;br&gt;
        1. Filter filenames, then search for text within the filtered files&lt;br&gt;
        1. Control which files be ignored&lt;/p&gt;

&lt;p&gt;You might find searching using IDE or the file manager to be slow, inefficient and too limiting. That's why I wrote this script.&lt;/p&gt;
&lt;h2&gt;
  
  
  TLDR (just give me the bash script)
&lt;/h2&gt;



&lt;p&gt;```bash title=vicontrolp wrap showLineNumbers=false&lt;br&gt;
fd \&lt;br&gt;
  -E '&lt;em&gt;.key'\&lt;br&gt;
  -E '&lt;/em&gt;.crt'\&lt;br&gt;
  -E '&lt;em&gt;lock.yaml'\&lt;br&gt;
  -E '&lt;/em&gt;.jar'\&lt;br&gt;
  -E '*.db'\&lt;br&gt;
| xargs \&lt;br&gt;
  -I{} awk \&lt;br&gt;
  -e '/^([[]-}{#]|[[:space:]])+$/{next;}{ print "{}:" NR ":" $0}'\&lt;br&gt;
  {} 2&amp;gt; /dev/null \&lt;br&gt;
| fzf \&lt;br&gt;
  --delimiter : \&lt;br&gt;
  --preview="bat --color=always --style=plain --highlight-line={2} {1}" \&lt;br&gt;
  --preview-window +{2}-5 \&lt;br&gt;
  --bind="enter:execute(nvim {1} +{2})"&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


I have this hooked up with [`zellij`](https://zellij.dev/) and did a keybind with `⌥ + p`.



```nginx title="~/.config/zellij/config.kdl" showLineNumbers=false
keybinds {
    normal {
        ...
        bind "π" {
          Run "vicontrolp" {
            close_on_exit true;
            in_place true;
          }
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With this, I can trigger this script and search for files whenever I'm&lt;br&gt;
in the comfort of my terminal.&lt;/p&gt;


 

&lt;h2&gt;
  
  
  🤔 Understanding the script
&lt;/h2&gt;

&lt;p&gt;Right! Of course you're not some mediocre developer who just copy pastes stuff.&lt;br&gt;
You want to understand how this works so you can expand your knowledge so then you can write your own&lt;br&gt;
developer tools that will enhance your developer experience!&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;In order for the script to work, you need the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;:orange[fd], this commandline tool is required to be installed on your system. See &lt;a href="https://github.com/sharkdp/fd?tab=readme-ov-file#installation"&gt;this link&lt;/a&gt; to install.&lt;/li&gt;
&lt;li&gt;:orange[xargs], this commandline tool is builtin so you don't need to install this. Just listing this here because we'll explain what this does later.&lt;/li&gt;
&lt;li&gt;:orange[fzf], this commandline tool is required to be installed on your system. See &lt;a href="https://github.com/junegunn/fzf?tab=readme-ov-file#installation"&gt;this link&lt;/a&gt; to install.&lt;/li&gt;
&lt;li&gt;:orange[bat], this commandline tool is required to be installed on your system. See &lt;a href="https://github.com/sharkdp/bat?tab=readme-ov-file#installation"&gt;this link&lt;/a&gt; to install.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;:orange[creating an executable script], this consists of (1) creating the file, (2) making it executable and (3) including in your &lt;code&gt;$PATH&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; ~/.localscripts/vicontrolp
&lt;span class="c"&gt;# open ~/.localscripts/vicontrolp, and pasting the commands in this file&lt;/span&gt;
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x ~/.localscripts/vicontrolp
&lt;span class="nb"&gt;echo export &lt;/span&gt;&lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:~/.localscripts &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.bashrc
&lt;span class="c"&gt;# if you use zsh, change bashrc to zshrc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;:orange&lt;a href="https://dev.tooptional"&gt;zellij&lt;/a&gt; if you want to add the same binding mentioned above.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Explaining the script
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. &lt;a href="https://github.com/sharkdp/fd"&gt;&lt;code&gt;fd&lt;/code&gt;&lt;/a&gt; command
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;fd&lt;/code&gt; is an alternative to the builtin &lt;code&gt;find&lt;/code&gt; command. It recursively lists all files within the directory.&lt;/p&gt;

&lt;p&gt;While listing the files, it ignores the files in &lt;code&gt;.gitignore&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  1.1. &lt;code&gt;-E&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;Besides the files in my &lt;code&gt;.gitignore&lt;/code&gt; there are other file formats I want to ignore. The currently excluded files are not exhaustive, you'll also want to ignore&lt;br&gt;
non-text searchable files like images, videos, etc.&lt;/p&gt;
&lt;h4&gt;
  
  
  2. &lt;code&gt;xargs&lt;/code&gt; command
&lt;/h4&gt;

&lt;p&gt;This is a command that allows me to execute a new command using the inputs from stdin as argument.&lt;/p&gt;
&lt;h4&gt;
  
  
  2.1 What is &lt;code&gt;stdin&lt;/code&gt;?
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;stdin&lt;/code&gt; is short for &lt;code&gt;standard input&lt;/code&gt;. It is a file stream from which a program may read it's input from.&lt;br&gt;
In command line scripting you'll often want to to pipe output from one command as input to another command.&lt;/p&gt;
&lt;h4&gt;
  
  
  2.2 What is &lt;code&gt;pipe&lt;/code&gt;?
&lt;/h4&gt;

&lt;p&gt;Pipe, indicated by the pipe operator: :orange[&lt;code&gt;|&lt;/code&gt;], means to take the result of one command and pass it to the next command so it can read it as input and output a new set of data.&lt;/p&gt;
&lt;h4&gt;
  
  
  2.3 So how what did &lt;code&gt;fd -E ... | xargs -I{} awk ... {}&lt;/code&gt; do?
&lt;/h4&gt;

&lt;p&gt;The result of &lt;code&gt;fd&lt;/code&gt; was piped to &lt;code&gt;xargs&lt;/code&gt;. Then &lt;code&gt;xargs&lt;/code&gt; executes &lt;code&gt;awk&lt;/code&gt; for each files outputed by &lt;code&gt;fd&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  2.4. &lt;code&gt;-I{}&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;This option tells &lt;code&gt;xargs&lt;/code&gt; which character should be used to replace them with inputs from the stdin.&lt;/p&gt;

&lt;p&gt;If for example your current directory consists the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```sh showLineNumbers=false&lt;br&gt;
.&lt;br&gt;
├── cskth-kt.mdx&lt;br&gt;
├── images&lt;br&gt;
│   └── restore_2.jpg&lt;br&gt;
├── lorem-ipsum.md&lt;br&gt;
└── tips-for-aspiring-professionals.mdx&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


doing `fd | xargs -I{} cp {} {}.bak` will be like executing the following commands (copies each file with the new file appended with `.bak`)


```sh  showLineNumbers=false frame=none
cp cskth-kt.mdx cskth-kt.mdx.bak
cp restore_2.jpg restore_2.jpg.bak
cp lorem-ipsum.md lorem-ipsum.md.bak
cp tips-for-aspiring-professionals.mdx tips-for-aspiring-professionals.mdx.bak
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In our command we run the &lt;code&gt;awk&lt;/code&gt; command for each file instead.&lt;/p&gt;
&lt;h4&gt;
  
  
  3. &lt;code&gt;awk&lt;/code&gt; command
&lt;/h4&gt;

&lt;p&gt;This program scans each line of an input file and allows you to do an action for each that matches a pattern.&lt;/p&gt;
&lt;h4&gt;
  
  
  3.1 &lt;code&gt;-e '...'&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;This option consists of three parts:&lt;/p&gt;
&lt;h4&gt;
  
  
  3.1.1. The pattern: &lt;code&gt;/^([\[\]-}{#]|[[:space:]])+$&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This &lt;code&gt;regex&lt;/code&gt; matches when a line consists only of &lt;code&gt;{&lt;/code&gt;,&lt;code&gt;[&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;#&lt;/code&gt;, &lt;code&gt;]&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt; or &lt;strong&gt;whitespaces&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  3.1.2. The action of the pattern: &lt;code&gt;{next;}&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;If previous pattern matches, it tells awk to proceed to the next line and don't do any further actions.&lt;br&gt;
This ultimately skips the next block which prints the important line to be piped to &lt;code&gt;fzf&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  3.1.3. The action block: &lt;code&gt;{ print "{}:" NR ":" $0 }&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Remember that this option is still under &lt;code&gt;xargs&lt;/code&gt; which means &lt;code&gt;{}&lt;/code&gt; will be replaced by the filename.&lt;br&gt;
Then &lt;code&gt;NR&lt;/code&gt; in awk will print the current line of the file that's being scanned. Then &lt;code&gt;$0&lt;/code&gt; points to the current line.&lt;/p&gt;

&lt;p&gt;So in our previous example, we may see the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cskth-kt.mdx:1:# Heading 1
cskth-kt.mdx:2:Heading 1 content
cskth-kt.mdx:3:Heading 1 content, second line
lorem-ipsum.md:1:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
lorem-ipsum.md:2:Vivamus non dapibus est, a rutrum nisi.
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
Depending of course on the contents of your files in your current directory 





&lt;p&gt;❗️Take note of this formatted output because we'll be explaining this later when this is piped into &lt;code&gt;fzf&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  3.2. &lt;code&gt;{} 2&amp;gt; /dev/null&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This is the parameter passed to &lt;code&gt;awk&lt;/code&gt; command which is replaced again by &lt;code&gt;xargs&lt;/code&gt; with the file name from &lt;code&gt;fd&lt;/code&gt;'s input.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2&amp;gt; /dev/null&lt;/code&gt; is called a stdout redirection. It simply means to redirect errors into &lt;code&gt;/dev/null&lt;/code&gt; stream. It means to ignore any error messages&lt;br&gt;
by outputting it to a blackhole or a non existent file stream: (&lt;code&gt;/dev/null&lt;/code&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  4. &lt;code&gt;fzf&lt;/code&gt; command
&lt;/h4&gt;

&lt;p&gt;This awesome commandline program is the heart of this script. From &lt;code&gt;awk&lt;/code&gt; command, all contents of each file, prepended by their filename, is now piped into this program.&lt;/p&gt;

&lt;h4&gt;
  
  
  4.1. &lt;code&gt;--delimeter :&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;This tells fzf to use &lt;code&gt;:&lt;/code&gt; character to separate words. This is used to separate file name, line number, and file contents again because we printed them as one line&lt;br&gt;
from &lt;code&gt;awk&lt;/code&gt; earlier.&lt;/p&gt;

&lt;h4&gt;
  
  
  4.2 &lt;code&gt;--preview ...&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;This tells &lt;code&gt;fzf&lt;/code&gt; to use &lt;code&gt;bat&lt;/code&gt; program to preview the whole file. Along with using &lt;code&gt;bat&lt;/code&gt;, we also added parameter to highlight the current line of the current fuzzy search match.&lt;/p&gt;

&lt;h4&gt;
  
  
  4.3 &lt;code&gt;--preview-window ...&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;This tells &lt;code&gt;fzf&lt;/code&gt; to scroll the preview window to include the current line that's being searched in fzf.&lt;/p&gt;

&lt;h4&gt;
  
  
  4.4 &lt;code&gt;--bind ...&lt;/code&gt; option
&lt;/h4&gt;

&lt;p&gt;Lastly, this tells &lt;code&gt;fzf&lt;/code&gt; to do a keybinding that when &lt;code&gt;enter key&lt;/code&gt; is pressed, open neovim and directly jump into the line number of the search match.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;... there's really not much to conclude this with. Hopefully you'll find this script useful. ✌🏻&lt;/p&gt;

</description>
      <category>devtools</category>
      <category>bash</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tips for aspiring professionals: 4 mindset you can apply in your career and everyday life</title>
      <dc:creator>Richard Glen Domingo</dc:creator>
      <pubDate>Thu, 20 Jun 2024 15:38:29 +0000</pubDate>
      <link>https://dev.to/chardskarth/tips-for-aspiring-professionals-4-mindset-you-can-apply-in-your-career-and-everyday-life-12bp</link>
      <guid>https://dev.to/chardskarth/tips-for-aspiring-professionals-4-mindset-you-can-apply-in-your-career-and-everyday-life-12bp</guid>
      <description>&lt;p&gt;See my &lt;a href="https://blog.chardskarth.me/blog/tips-for-aspiring-professionals/"&gt;original post here in my blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;4 things I realized as I reflected going through a tough day. This tips will hopefully help you prosper in your chosen field.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. As you PRACTICE, be ALWAYS on the lookout for small things that can be improved
&lt;/h2&gt;

&lt;h2&gt;
  
  
  2. Do your best to adhere to best practices
&lt;/h2&gt;

&lt;h2&gt;
  
  
  3. Keep in mind that you want to deliver value, but think in long term.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  4. At the end of the day, It's all just work.
&lt;/h2&gt;

</description>
      <category>career</category>
      <category>tips</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
