<?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>Forem: Zaerald</title>
    <description>The latest articles on Forem by Zaerald (@zaerald).</description>
    <link>https://forem.com/zaerald</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%2F53410%2Fe1bda67a-eb17-473b-9e44-f4ef5c1fe6ae.jpg</url>
      <title>Forem: Zaerald</title>
      <link>https://forem.com/zaerald</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zaerald"/>
    <language>en</language>
    <item>
      <title>Using Vim As Your Shell Command-Line Scratch</title>
      <dc:creator>Zaerald</dc:creator>
      <pubDate>Wed, 20 Apr 2022 15:02:38 +0000</pubDate>
      <link>https://forem.com/zaerald/using-vim-as-your-shell-command-line-scratch-1lcl</link>
      <guid>https://forem.com/zaerald/using-vim-as-your-shell-command-line-scratch-1lcl</guid>
      <description>&lt;p&gt;Using (n)vim as your command-line scratch helps you to efficiently fix, create, and run ad-hoc commands. &lt;/p&gt;

&lt;p&gt;If you are not familiar with vim, I suggest you start playing with the &lt;code&gt;vimtutor&lt;/code&gt; first. I’ll try my best to explain each keys that I have pressed. In this blog, I’m using &lt;a href="https://github.com/neovim/neovim"&gt;Neovim&lt;/a&gt;, but you can still follow along even if you’re using &lt;a href="https://github.com/vim/vim"&gt;Vim&lt;/a&gt;. I have aliased my &lt;code&gt;nvim&lt;/code&gt; to &lt;code&gt;vim&lt;/code&gt; with &lt;code&gt;alias vim=nvim&lt;/code&gt;. Please follow along with the demonstrations, so you can get used to this workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixing Ad-Hoc Commands
&lt;/h2&gt;

&lt;p&gt;Have you experienced running a command and realizing that you made a mistake after executing it? You missed some syntax or forgot adding a &lt;code&gt;sudo&lt;/code&gt;, and much more?&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands that requires privilege
&lt;/h3&gt;

&lt;p&gt;You are copying a file to another directory, or installing a package, but you forgot that it requires a &lt;code&gt;sudo&lt;/code&gt; privilege.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/q6ZV4yQMye4"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;I - insert mode before the first character
Escape or Ctrl + C - to go to the normal mode
ZZ or :wq- to save and exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that in vim, keys are case-sensitive, this means that the lowercase &lt;code&gt;i&lt;/code&gt; is different to the capital &lt;code&gt;I&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As you have noticed, we were able to edit our previously executed command with fix command (&lt;code&gt;fc&lt;/code&gt;), it opened the vim editor for us with the command as our content. If we exit from the editor, it’ll run the command that we have edited.&lt;/p&gt;

&lt;p&gt;For this case, it is better to use sudo-bang-bang (&lt;code&gt;sudo !!&lt;/code&gt;) if you need to add &lt;code&gt;sudo&lt;/code&gt; before the previous command.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/eq6CV0_d9jg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;!!&lt;/code&gt; - will use your previously executed command&lt;/p&gt;

&lt;p&gt;If we view the man page of &lt;code&gt;fc&lt;/code&gt;. It checks for the &lt;code&gt;FCEDIT&lt;/code&gt; and &lt;code&gt;EDITOR&lt;/code&gt; environment variables. If both of them are unassigned, it’ll use &lt;code&gt;vi&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-e ename
...
If ename is not given, the value of the FCEDIT variable is used,
and the value of EDITOR if FCEDIT is not set.
If neither variable is set, vi is used.
When editing is complete, the edited commands are echoed and executed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have exported my &lt;code&gt;EDITOR&lt;/code&gt; variable in my config file to use neovim.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;EDITOR&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;nvim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can assign &lt;code&gt;nano&lt;/code&gt;, or &lt;code&gt;code&lt;/code&gt; here to use VS Code as your &lt;code&gt;fc&lt;/code&gt; editor, but I do not recommend it. You’ll see why it’s better to use vim later on.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/K7wjiU9dd8o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands with typo
&lt;/h3&gt;

&lt;p&gt;Another use case is if you made a typo in the middle of the command, or missed any quotation marks. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrow Keys Spam&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/lIAmBhPlZSg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;When we try to fix our command, we will spam the thing out of the left arrow key to navigate to the middle of the command to fix it. This workflow is so slow, and even annoying to deal with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;fc&lt;/code&gt; to the rescue!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XAw9RP_sQo0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Invoke the &lt;code&gt;fc&lt;/code&gt; command to fix the previous command that you have executed. This will open a vim editor which we can easily perform search and replace commands. This would be more useful if we have really long commands and arguments.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:s/ooo/o&lt;/code&gt; - perform a substitute &lt;code&gt;ooo&lt;/code&gt; to &lt;code&gt;o&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:s/hir/here&lt;/code&gt; - perform a substitute &lt;code&gt;hir&lt;/code&gt; to &lt;code&gt;here&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For more information about the vim substitute command, please check the help file with &lt;code&gt;:h :s&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands with shell expansion
&lt;/h3&gt;

&lt;p&gt;Let’s say you want to say hello to the current &lt;code&gt;USER&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/JHGCmF9nNmo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Hmmmmm 🤔, our &lt;code&gt;USER&lt;/code&gt; is not printing, but it has value as we have checked it. We have found out that we were using single quotes (&lt;code&gt;'&lt;/code&gt;), and this means that &lt;a href="https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html"&gt;shell expansion&lt;/a&gt; never happened, what we need is to surround them with double quotes (&lt;code&gt;"&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;We can still use &lt;code&gt;:s/'/"&lt;/code&gt; here, but we can also leverage the power of vim with plugins!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qk2lxHbRIh0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;I have navigated to the text to replace the surrounding quotes, then used &lt;a href="https://github.com/tpope/vim-surround"&gt;tpope/vim-surround&lt;/a&gt; plugin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cs'" - it means change surrounding single quote (') to double quote (").
                c      s                         '                   "
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to delete a quote, you can use &lt;code&gt;ds'&lt;/code&gt;, or &lt;code&gt;ds"&lt;/code&gt; for double quotes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands that you want to edit without executing
&lt;/h3&gt;

&lt;p&gt;We have learned that we can use &lt;code&gt;fc&lt;/code&gt; to edit or fix our previous command. What if you noticed a typo, and you do not want to execute the command? In that case, what we can do is press &lt;code&gt;ctrl + a&lt;/code&gt; to move the cursor before the first character in your command line and type pound (&lt;code&gt;#&lt;/code&gt;), this results into making the command as a comment. &lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/yFt5w1qS-gg"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;If the command with pound (&lt;code&gt;#&lt;/code&gt;) in front is executed, it won’t execute the command itself, but instead it’ll result into being a shell comment. The good thing with this is you can use &lt;code&gt;fc&lt;/code&gt; to open it in your editor and edit it. You can do this because the commented-out command was already added to your command history, and &lt;code&gt;fc&lt;/code&gt; will edit the previously executed command. Just remove the &lt;code&gt;#&lt;/code&gt; on your edit.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xGgQLjYbTmc"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands with long parameters
&lt;/h3&gt;

&lt;p&gt;If we want to run a postgres container with a command from their documentation, we will run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; some-postgres &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;mysecretpassword &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;PGDATA&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/lib/postgresql/data/pgdata &lt;span class="nt"&gt;-v&lt;/span&gt; /custom/mount:/var/lib/postgresql/data postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we have pasted this in our terminal, it’s hard for us to update the parameter variables. We will use so many arrow key presses just to navigate to the environment variable values and edit it. We can comment this for now and run the &lt;code&gt;fc&lt;/code&gt; command. By doing this, we can leverage the vim editor to manipulate the text.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XbZ10YsqU4I"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x&lt;/code&gt; - delete the &lt;code&gt;#&lt;/code&gt; symbol&lt;/p&gt;

&lt;p&gt;&lt;code&gt;:s/some-/my-&lt;/code&gt; - substitute the container name&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/secretciwmyverysecret
  - '/secret' search and navigate to the first 'secret' word match
  - 'ciw' delete the inner word, and go into insert mode
  - 'myverysecret' literal word to replace the password value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add new lines before the &lt;code&gt;-e&lt;/code&gt; and &lt;code&gt;-v&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:s/ -[ev]/\r\0/g
  - ':s/' substitute command
  - ' -[ev]' search for text that has ' -e' or ' -v'
  - '/\r' add a carriage return
  - '\0' substitute with the first captured group
  - '/g' substitute globally, not just the first match
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this substitute, we were able to format our command, but there is a problem, in shell with multiple lines, we need to suffix every line with a backslash (&lt;code&gt;\&lt;/code&gt;). We can achieve that with performing a &lt;code&gt;normal&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:%norm A \
  - ':%norm' perform a normal command for the whole buffer
  - 'A' go to insert mode to the end of the line
  - ' \' add literal space and backslash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;normal&lt;/code&gt; command helps you to perform it on multiple lines as if you’re actually typing it directly on the buffer, to learn more about it, check the help file for &lt;code&gt;normal&lt;/code&gt; using &lt;code&gt;:h norm&lt;/code&gt; (&lt;code&gt;norm&lt;/code&gt; command is just a shorter version of &lt;code&gt;normal&lt;/code&gt; command).&lt;/p&gt;

&lt;p&gt;We were now able to update the values of a command with multiple long parameters, and also properly formatted it. If you noticed that my line numbers are not linearly increasing, I’m using a relative numbers, you can learn more about it with &lt;code&gt;:h relativenumber&lt;/code&gt;. I highly recommend setting &lt;code&gt;set rnu&lt;/code&gt; config together with &lt;code&gt;set nu&lt;/code&gt; so you can easily jump to arbitrary lines relative to your current line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands that you are currently fixing and want to abort it
&lt;/h3&gt;

&lt;p&gt;If you have edited your previous command and you do not want to execute it. If you use &lt;code&gt;ZQ&lt;/code&gt; or &lt;code&gt;:wq!&lt;/code&gt; to exit vim without saving, it’ll still execute your command.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/v2XK8w-ke7k"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;What happened? It turns out that when the editor exits with &lt;code&gt;0&lt;/code&gt;, it’ll execute your command, and using &lt;code&gt;:wq!&lt;/code&gt; gracefully exits vim.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;
0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to solve this? You need to use &lt;code&gt;:cq&lt;/code&gt; to exit vim with error code. You can add a key bind for this, or you can just delete the content of your vim buffer with &lt;code&gt;dd&lt;/code&gt; and exit.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/gr0FeLG3gTQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Vim Buffers and Shell
&lt;/h2&gt;

&lt;p&gt;Fixing your previous command is cool, but what if you want to perform a command operation to the output of your previous command, and update its content? You can use Vim to achieve that.&lt;/p&gt;

&lt;p&gt;If we perform an operation and pipe it to our Vim buffer, it’ll use the stdin as your buffer content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;hello | nvim -
&lt;span class="c"&gt;# look ma, no dash&lt;/span&gt;
&lt;span class="nb"&gt;echo &lt;/span&gt;hello | nvim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are times that you need to use dash (&lt;code&gt;-&lt;/code&gt;) to pipe the standard output to standard input of the next command. In nvim, the dash is not required.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sort and remove duplicate names
&lt;/h3&gt;

&lt;p&gt;We have a file with a list of names, but there are duplicates. We want to remove the duplicates, and sort them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;names.txt
John Doe
John Doe
David William
Joseph Thomas
David William
Joseph Thomas
Joseph Thomas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can pipe the output of the &lt;code&gt;cat names.txt&lt;/code&gt;, or just directly open it with vim.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim names.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can now perform a command operation to update the content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:%!sort &lt;span class="nt"&gt;-u&lt;/span&gt;
David William
John Doe
Joseph Thomas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anything after the &lt;code&gt;!&lt;/code&gt; character is the shell command.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/EMRnCyAWByI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Of course, we do not need to open it in vim to remove the duplicates, it is to simply demonstrate that you can perform commands on the current buffer and replace it with the output of the shell command.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sort and remove duplicate names of some part of the buffer
&lt;/h3&gt;

&lt;p&gt;We can also specify some area that we want to perform the operation using the visual mode of vim.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;names2.txt
John Doe
John Doe
David William
Joseph Thomas
David William
Joseph Thomas
Joseph Thomas
&lt;span class="c"&gt;# please do not modify the following lines&lt;/span&gt;
Christopher Daniel
Mark Anthony
Christopher Daniel
Paul Steven
Mark Anthony
Paul Steven
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/9xSjN_y_jVM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;First, we need to select the range with the visual line (&lt;code&gt;V&lt;/code&gt;), and run the &lt;code&gt;:!sort -u&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;Note that the &lt;code&gt;'&amp;lt;,'&amp;gt;&lt;/code&gt; characters are automatically added by vim, this represents the selected line range.&lt;/p&gt;

&lt;h3&gt;
  
  
  Formatting and filtering JSON content
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;characters.json
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
&lt;span class="s2"&gt;"id"&lt;/span&gt;: 1,
          &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Tomoko Kuroki"&lt;/span&gt;,
    &lt;span class="s2"&gt;"age"&lt;/span&gt;: 15,
&lt;span class="s2"&gt;"height"&lt;/span&gt;: &lt;span class="s2"&gt;"147 cm"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;, &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 2,
          &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Marin Kitagawa"&lt;/span&gt;,
    &lt;span class="s2"&gt;"age"&lt;/span&gt;: 15,
&lt;span class="s2"&gt;"height"&lt;/span&gt;: &lt;span class="s2"&gt;"164 cm"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="o"&gt;{&lt;/span&gt;
          &lt;span class="s2"&gt;"id"&lt;/span&gt;: 3,
&lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Kaguya Shinomiya"&lt;/span&gt;, &lt;span class="s2"&gt;"age"&lt;/span&gt;: 17,
                &lt;span class="s2"&gt;"height"&lt;/span&gt;: null
  &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a JSON file with a content that is not properly formatted. We also want to remove some noise, we only want the &lt;code&gt;id&lt;/code&gt;, and the &lt;code&gt;name&lt;/code&gt; properties of the characters. We can do this by using the &lt;a href="https://stedolan.github.io/jq/"&gt;jq&lt;/a&gt; command. We can also use any vim plugins like prettier for different file types.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/g21jGOxmV10"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;We have formatted it with &lt;code&gt;:%!jq .&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:%!jq &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Tomoko Kuroki"&lt;/span&gt;,
    &lt;span class="s2"&gt;"age"&lt;/span&gt;: 15,
    &lt;span class="s2"&gt;"height"&lt;/span&gt;: &lt;span class="s2"&gt;"147 cm"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 2,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Marin Kitagawa"&lt;/span&gt;,
    &lt;span class="s2"&gt;"age"&lt;/span&gt;: 15,
    &lt;span class="s2"&gt;"height"&lt;/span&gt;: &lt;span class="s2"&gt;"164 cm"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 3,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Kaguya Shinomiya"&lt;/span&gt;,
    &lt;span class="s2"&gt;"age"&lt;/span&gt;: 17,
    &lt;span class="s2"&gt;"height"&lt;/span&gt;: null
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Filtering the properties that we only want with &lt;code&gt;:%!jq '[.[] | {id, name}]'&lt;/code&gt;. This is really helpful to analyze some JSON data, and want to play with the properties.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/mOY9fB_unYY"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:%!jq &lt;span class="s1"&gt;'[.[] | {id, name}]'&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Tomoko Kuroki"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 2,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Marin Kitagawa"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"id"&lt;/span&gt;: 3,
    &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"Kaguya Shinomiya"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pressing &lt;code&gt;u&lt;/code&gt; to undo the changes to the buffer. You can learn more about &lt;code&gt;jq&lt;/code&gt; from their &lt;a href="https://stedolan.github.io/jq/manual/"&gt;manual&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We were able to easily parse the JSON without leaving our editor!&lt;/p&gt;

&lt;h2&gt;
  
  
  Searching file contents
&lt;/h2&gt;

&lt;p&gt;You can also pipe the output of your grep to vim, so you can easily navigate to the matched files. If the cursor is under a file path, and pressing &lt;code&gt;gf&lt;/code&gt; will open another buffer with the file and the cursor is on the line number that was created in our &lt;code&gt;grep&lt;/code&gt; result.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/8LzGbPxIzgU"&gt;
&lt;/iframe&gt;
&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;grep -Hnri 'search keyword here' . | nvim
H - print file name
n - add line number
r - recursive search for each directory
i - ignore case
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have the matched searches in our buffer with their associated file names and line numbers, we can use &lt;code&gt;gf&lt;/code&gt; here to navigate to the file under the cursor. This can help you preview and analyze the files, you can get back to the previous buffer with &lt;code&gt;ctrl + 6&lt;/code&gt;. See &lt;code&gt;:h buffers&lt;/code&gt; for more information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing to Shell
&lt;/h2&gt;

&lt;p&gt;Can we do it the other way around? Using the content of your buffer to execute them in your shell?&lt;/p&gt;

&lt;p&gt;One common misconception when you run &lt;code&gt;vim &amp;lt;file&amp;gt;&lt;/code&gt;, is that your’e editing the actual file, but in fact, you are actually opening a buffer of that file, and not editing the file itself.&lt;/p&gt;

&lt;p&gt;This is different to the other editors where they will immediately create the file for you.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/LNefH_a6JbQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;When you save it with &lt;code&gt;:w&lt;/code&gt;, you are just overwriting the file with your current buffer, and if the file does not exist, it’ll create it. This is equivalent to &lt;code&gt;:w %&lt;/code&gt; or &lt;code&gt;:w yourfile.txt&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/3LnOlBfes0Q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;With this in mind, what if you write the buffer to your shell? Well...you guessed it right (pun not intended 😉), if you write the buffer to your shell, you can execute commands from your Vim buffer!&lt;/p&gt;

&lt;p&gt;I am using &lt;code&gt;zsh&lt;/code&gt; here, but any shell should work.&lt;/p&gt;

&lt;p&gt;If we write the buffer to a shell, it’ll invoke the command to a sub-shell.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:w &lt;span class="o"&gt;!&lt;/span&gt;zsh
:w &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nv"&gt;$SHELL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;file-that-contains-shell-script.txt

&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'i have echo'&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;touch &lt;/span&gt;newfile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xaPSGL1wrPU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;We can also pick which line to execute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:.w &lt;span class="o"&gt;!&lt;/span&gt;zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the dot(&lt;code&gt;.&lt;/code&gt;) before &lt;code&gt;w&lt;/code&gt;, this means “write the current line of the cursor to the shell”.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;sample-line.txt
&lt;span class="nb"&gt;echo &lt;/span&gt;hello1
&lt;span class="nb"&gt;echo &lt;/span&gt;hello2
&lt;span class="nb"&gt;echo &lt;/span&gt;hello3
&lt;span class="nb"&gt;echo &lt;/span&gt;hello4
&lt;span class="nb"&gt;echo &lt;/span&gt;hello5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/5-04YkJgjB8"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Shell Level
&lt;/h3&gt;

&lt;p&gt;If we print out the &lt;code&gt;SHLVL&lt;/code&gt; environment variable, we are currently at level &lt;code&gt;1&lt;/code&gt;. Invoking this command inside vim will result into the level &lt;code&gt;2&lt;/code&gt;. This means that it is executing it to the sub-shell.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/131E9H4shvQ"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Bindings Configuration
&lt;/h3&gt;

&lt;p&gt;Typing &lt;code&gt;:w !zsh&lt;/code&gt; or &lt;code&gt;:w !$SHELL&lt;/code&gt; again and again is too much, I recommend adding your own bindings similar to what I have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="s2"&gt;" write to shell
let mapleader="&lt;/span&gt; &lt;span class="s2"&gt;"
nnoremap &amp;lt;silent&amp;gt; &amp;lt;leader&amp;gt;xs :.w !&lt;/span&gt;&lt;span class="nv"&gt;$SHELL&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;CR&amp;gt;
xnoremap &amp;lt;silent&amp;gt; &amp;lt;leader&amp;gt;xs :.w !&lt;/span&gt;&lt;span class="nv"&gt;$SHELL&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;leader&amp;gt;xS :w !&lt;/span&gt;&lt;span class="nv"&gt;$SHELL&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;CR&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can put this into your &lt;code&gt;.vimrc&lt;/code&gt; file or the &lt;code&gt;init.vim&lt;/code&gt; for neovim. Then you can just press the leader key which is set to space and &lt;code&gt;xs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XF2hPDsck40"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  File Analysis
&lt;/h3&gt;

&lt;p&gt;From our previously found files in the “Searching file contents” section, we decided that we want to delete those files. We need to filter out the file paths first, and there are multiple ways to achieve this. We can use &lt;code&gt;cut&lt;/code&gt; here, but for now, let’s use &lt;code&gt;awk&lt;/code&gt; with a colon separator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:%!awk &lt;span class="nt"&gt;-F&lt;/span&gt;: &lt;span class="s1"&gt;'{print $1}'&lt;/span&gt;
./subfolder0/somefile.txt
./subfolder0/anotherfile.txt
./subfolder1/alpha.txt
./subfolder3/beta.txt
./subfolder4/magic.txt
./sample-line.txt
./sample-line.txt
./sample-line.txt
./sample-line.txt
./sample-line.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/e59b94-250A"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;There are duplicate files, let’s perform a unique sort and prepare to delete with &lt;code&gt;rm&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:%!sort &lt;span class="nt"&gt;-u&lt;/span&gt;
./sample-line.txt
./subfolder0/anotherfile.txt
./subfolder0/somefile.txt
./subfolder1/alpha.txt
./subfolder3/beta.txt
./subfolder4/magic.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/olP7tBWj8PY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now that we have added the &lt;code&gt;rm&lt;/code&gt; command on each line, and writing them to your shell will remove the files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shell Intellisense
&lt;/h2&gt;

&lt;p&gt;We can improve our workflow by adding intellisense using the &lt;a href="https://microsoft.github.io/language-server-protocol/"&gt;Language Server Protocol (LSP)&lt;/a&gt;. LSP is a protocol that can help provide some auto completion, quick documentation, go to definition, and many other features. You can use any LSP provider, at the time of this writing, I’m using &lt;a href="https://github.com/neoclide/coc.nvim"&gt;coc.nvim&lt;/a&gt; for my LSP.&lt;/p&gt;

&lt;p&gt;Setting up your own LSP and their configuration is out of scope for this blog, you can check their documentation for it.&lt;/p&gt;

&lt;p&gt;In order for our LSP to work, there are multiple ways to enable it, but the easiest one I can think of is to set the file type of the buffer that we are currently working on. You can check the current file type with &lt;code&gt;set filetype?&lt;/code&gt;, and assign a file type using &lt;code&gt;set filetype=sh&lt;/code&gt;. Now that we have an &lt;code&gt;sh&lt;/code&gt; filetype, our LSP now knows that we are editing a shell buffer, which will provide us the language features of a shell.&lt;/p&gt;

&lt;h3&gt;
  
  
  Command Auto-Completion and Documentation
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/JPvUm4VYbDk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Path Auto-Completion
&lt;/h3&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0WVa-ERmRe4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;This is just the tip of LSP, and you can check the language server of what features it can offer for you to help in your shell scratch workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Use Cases
&lt;/h2&gt;

&lt;p&gt;In this section, I will share some of my workflow that I use day to day.&lt;/p&gt;

&lt;h3&gt;
  
  
  Workflows in Git
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Long Branch Names
&lt;/h3&gt;

&lt;p&gt;Whenever we create new features, or fixing a bug. It’s better to create new branch from the &lt;code&gt;main&lt;/code&gt; or any base branch. We usually create branches using the ticket name &lt;code&gt;feat/ZERO-1234&lt;/code&gt;, but browsing these branches does not provide any context as to what is the purpose of that branch, unless you memorized the ticket number and their description.&lt;/p&gt;

&lt;p&gt;We can add more details to the branch name, but the problem with this branch names is that the checkouts, rebase, and renames is a bit of pain to type, unless you have auto complete. You can pipe the branch output to vim, and perform the checkouts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;feat/ZERO-123-create-some-cool-feature-here-that-no-one-usees
fix/ZERO-456-the-bug-that-will-consume-your-days
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/I9tLevfBEJU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The aliases that you have configured in your Git will work here like &lt;code&gt;git config --global alias.co checkout&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deleting multiple branches
&lt;/h3&gt;

&lt;p&gt;Deleting multiple branches with &lt;code&gt;grep&lt;/code&gt; is a bit of a hassle, unless you run the &lt;code&gt;grep&lt;/code&gt; first to see the list of the branches. What we can do is pipe the output to vim, and perform any searches.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/-ki6so7KKCY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;By doing this, you have a faster feedback of what branches would be deleted, you can even easily remove any branch that was part of the initial &lt;code&gt;grep&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading Command Outputs
&lt;/h3&gt;

&lt;p&gt;APIs mostly use JSON as their payload. We can easily create them using &lt;a href="https://github.com/jpmens/jo"&gt;jo&lt;/a&gt;. We can read the command output and put it to your current buffer. For example, we want to create a JSON object with a lower case uuid value for its &lt;code&gt;id&lt;/code&gt; property, and a simple name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;:read &lt;span class="o"&gt;!&lt;/span&gt;jo &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;uuidgen | &lt;span class="nb"&gt;tr &lt;/span&gt;A-Z a-z&lt;span class="si"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'zaerald'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Anything after the &lt;code&gt;!&lt;/code&gt; will run a shell command.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/MQSmpn-hp1M"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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

&lt;p&gt;You can now efficiently edit previously executed commands using &lt;code&gt;fc&lt;/code&gt;. Using vim with plugins and LSP can help improve your workflow. Sky is the limit! Using vim as your command-line scratch is much fun to use. Just play with it, and don’t forget to have fun!&lt;/p&gt;

&lt;p&gt;You can check my dotfiles in &lt;a href="https://github.com/zaerald/dotfiles"&gt;github.com/zaerald/dotfiles&lt;/a&gt;. Please note that my dotfiles continues to evolve, and there’s a higher chance that it is different now compared at the time of this writing.&lt;/p&gt;

&lt;p&gt;This blog post is originally posted &lt;a href="https://blog.zaerald.com/using-vim-as-your-shell-command-line-scratch"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>shell</category>
      <category>vim</category>
    </item>
    <item>
      <title>How to improve Java Unit Testing Skills to a more advanced level?</title>
      <dc:creator>Zaerald</dc:creator>
      <pubDate>Mon, 31 Aug 2020 17:05:45 +0000</pubDate>
      <link>https://forem.com/zaerald/how-to-improve-java-unit-testing-skills-to-a-more-advanced-level-5d9o</link>
      <guid>https://forem.com/zaerald/how-to-improve-java-unit-testing-skills-to-a-more-advanced-level-5d9o</guid>
      <description>&lt;p&gt;What are the efficient ways to improve and learn more about Java unit testing? Is there a repository where I can practice Unit Testing with ways how to solve it, so I can compare it with mine? How did you practice doing it? As sometimes when I'm doing stuff I get stuck and not able to test the other parts. &lt;/p&gt;

</description>
      <category>java</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How to Hack Learning Curves for Slow Learners Like Me</title>
      <dc:creator>Zaerald</dc:creator>
      <pubDate>Mon, 26 Aug 2019 17:38:27 +0000</pubDate>
      <link>https://forem.com/zaerald/how-to-hack-learning-curves-for-slow-learners-like-me-3528</link>
      <guid>https://forem.com/zaerald/how-to-hack-learning-curves-for-slow-learners-like-me-3528</guid>
      <description>&lt;p&gt;Before I would discuss the techniques I use, I'll start giving a backstory of when I was before I discovered these techniques, when I was studying something I couldn't get it and I always get stuck! It is so frustrating that would make me feel so dumb that I'll start to believe that it is not for me, I started to think that maybe programming is not for me. It always reminded me that I'm no genius because whenever I browse the internet and look at how others do things and look easy for them, that's the time when I wonder that is there a way to learn how to learn?&lt;/p&gt;

&lt;p&gt;I tried to search for things on how to learn and what I had discovered changed my life and I will share it with you. You should keep in mind that learning is emotional, so you have to believe that you have the ability to learn, and you do, as I had said earlier it is bad to think that the one thing you're learning is not for you when we are a kid, and we couldn't walk or run properly, we didn't say that "running is not for me", we always believed, failed at some times and we did. Brains are like muscles that when stretched they'll break and repair themselves and be stronger, the brain also keeps changing and creates stronger neuron connections based on what we do with it, we just need to utilize it and exercise it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Techniques
&lt;/h1&gt;

&lt;p&gt;Here is the list of techniques I used to learn efficiently. Please be mindful that not all of these techniques could apply to you, some may work and some may not, you could try and combine these techniques when you're trying to learn. &lt;/p&gt;

&lt;h3&gt;
  
  
  First-principles
&lt;/h3&gt;

&lt;p&gt;Whenever you encounter a new topic, break it down into its first principles--deconstruct it as each topic is just a composition of smaller topics or concepts that you can boil down to something that you do know. After breaking things down look for the essentials, see if some of it could be postponed for you to focus your energy towards those mostly or commonly used. I create mind maps so the things I break down are linked to their parents, so I can look up to their relations and associations which helps me to understand the overall idea of the topic.&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://coggle.it"&gt;coggle&lt;/a&gt; for my digital mind maps, but I mostly use pen and paper to create my mind maps. You can learn more about mind mapping by watching the &lt;a href="https://www.youtube.com/watch?v=5nTuScU70As"&gt;talk by Hazel Wagner, Ph.D.&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Transfer Learning
&lt;/h3&gt;

&lt;p&gt;Now that you managed to break down the topic into smaller ones, you can then find patterns that smaller sets that can be used to other skills or can be used to a different topic. For example, you're trying to learn a programming language like Java and as you have broken its concept and as you see the language has control structures that have &lt;code&gt;if-else&lt;/code&gt; statements, and you'll soon realize that when you learned it you can use it and transfer the learning to many programming languages, with that in mind, it is very helpful for you to study and master it.&lt;/p&gt;

&lt;p&gt;With this, you'll learn faster because you know that it is a part of it and helps you not to be overwhelmed by it. Be comfortable being uncomfortable, when you're trying to learn your brain receives a pain signal to your neurons, and you would try to avoid learning, this is sometimes the reason why we procrastinate as it makes it hard to learn. Just let it sink in and sooner or later the pain will ease.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brain Modes
&lt;/h3&gt;

&lt;p&gt;Have you ever experienced it when you're taking a shower or just walking after you worked on some topic and left it for a while and knowing the answer? Or when you're working on a bug, and you decided to sleep and when you're sleeping you'll know the fix or even dream about the fix!?&lt;/p&gt;

&lt;p&gt;A study conducted by Prof. Barbara Oakley mentions that our brains have two modes: They are &lt;strong&gt;focused mode&lt;/strong&gt; and &lt;strong&gt;diffused mode&lt;/strong&gt;. This is different research on the left brain or right brain. The focus mode is the mode wherein you're working on learning the topic or when you're in the &lt;em&gt;zone&lt;/em&gt;, the other mode is very different because this mode is making your brain at rest and let your mind wander.&lt;/p&gt;

&lt;p&gt;While taking that hot shower, you would find out that the topics now make more sense, or you'll know the resolution to the problem. That's because the diffuse mode is at work, these modes cannot work in parallel and diffuse mode can only work effectively after you have focused on the topic first, so the brain can manage those things you try to put into your brain.&lt;/p&gt;

&lt;p&gt;Pair programming also works effectively because of this, the driver is in the focused mode while the observer is in diffused mode, this is sometimes the observer can see the mistakes of the driver because the driver is in the focused mode and not seeing the bigger picture, he is so focused that sometimes he couldn't see the other solutions. This means that when you're trying to solve an unsolvable problem, try to solve it away from your workstation and just let your mind wander.&lt;/p&gt;

&lt;p&gt;Learn more about &lt;a href="https://www.youtube.com/watch?v=O96fE1E-rf8"&gt;focused and diffused mode&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Feynman technique
&lt;/h3&gt;

&lt;p&gt;This technique is developed by &lt;a href="https://en.wikipedia.org/wiki/Richard_Feynman"&gt;Richard Feynman&lt;/a&gt; which is named to himself, the technique is just to study the topic and try to teach it and when you can't teach some part of it, you'll have to go back to study the topic again. I use this when I just learned a new topic I try to share the learnings with others which means that I have to teach it to them too.&lt;/p&gt;

&lt;p&gt;This may also refer to Einstein's quotation &lt;em&gt;"If you can't explain it simply, you don't understand it well enough"&lt;/em&gt;. The statement could also be reversed and when you don't understand it enough you couldn't explain it simply. Try to explain it to someone or your &lt;a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging"&gt;rubber duck&lt;/a&gt; when you're studying, with this you're helping yourself to learn, and also you're helping others to learn too, I call this process a peer to peer (&lt;a href="https://en.wikipedia.org/wiki/Peer-to-peer"&gt;p2p&lt;/a&gt;) learning as there's free-flowing information through your network.&lt;/p&gt;

&lt;p&gt;The reason why this works is that whenever you're thinking, your brain short-circuits in order for you to think fast, this was also referred to as &lt;em&gt;System 1&lt;/em&gt; on &lt;a href="https://www.goodreads.com/book/show/11468377-thinking-fast-and-slow"&gt;Thinking, Fast and Slow by Daniel Kahneman&lt;/a&gt; that is why when you try to use a language to convey it, your brain needs to create a structure of the way how you think and your brain can only create the structure when you get a good deeper understanding of the topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Habits
&lt;/h3&gt;

&lt;p&gt;Learning is a skill and when learning it won't happen overnight you need to develop it, you have to make it a habit to learn. To create a habit, you need to know its steps. A habit consists of 3 steps, a &lt;em&gt;cue&lt;/em&gt;, &lt;em&gt;routine&lt;/em&gt;, and &lt;em&gt;reward&lt;/em&gt;. Cue is a trigger that tells your brain to go to the habit. Routine is the action itself and the Reward makes the particular routine worth remembering because your brain releases dopamine which makes you feel motivated and do it again. Try it when you have finished learning something to find your reward, as for me my rewards are playing games or just mindlessly surfing the internet.&lt;/p&gt;

&lt;p&gt;You could also create your &lt;code&gt;if-then&lt;/code&gt; statements to create your &lt;em&gt;cue&lt;/em&gt;, add the &lt;code&gt;if&lt;/code&gt; statement to your existing routine. For example, &lt;code&gt;if&lt;/code&gt; I opened my laptop &lt;code&gt;then&lt;/code&gt; I'll open my digital notes and &lt;code&gt;if&lt;/code&gt; I opened my notes &lt;code&gt;then&lt;/code&gt; I'll read what I had studied yesterday to review it. My other &lt;em&gt;cue&lt;/em&gt; is &lt;code&gt;if&lt;/code&gt; I finished drinking my coffee &lt;code&gt;then&lt;/code&gt; I'll start learning (this was maybe the reason why I like coffee, it helps me to learn 😁). Try to develop a habit to learn 25 minutes a day, it is better to learn 25 minutes a day for a week compared to 175 minutes in just a day. Because your brain has to create the building blocks, imagine it as if you're creating the foundation of the house, it needs time to strengthen its foundation for you to build on top of it and when you try to cram it in a day, you wouldn't have a stronger foundation.&lt;/p&gt;

&lt;h3&gt;
  
  
  ELI5
&lt;/h3&gt;

&lt;p&gt;Another thing I use is to create analogies, I mostly use Explain Like I'm 5 to try to explain it simply and create analogies that a 5-year-old can understand, that's also the same as whenever I try to learn something and I get stuck, I'll try to look up the term I wanted to understand and add the "ELI5" to it and see how others explain it to a 5-year-old. Recently I'm trying to learn about &lt;a href="https://kubernetes.io"&gt;Kubernetes (K8s)&lt;/a&gt; and looked up &lt;em&gt;"Kubernetes eli5"&lt;/em&gt; on Google and has a result of &lt;a href="https://www.reddit.com/r/explainlikeimfive/comments/8ur2z7/eli5_what_is_kubernetes_and_how_different_better/"&gt;ELI5: What is Kubernetes...&lt;/a&gt;, this helped me to understand its concepts more. But there's a danger when creating analogies, as there are some topics that are not a complete analogy so just be aware of that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spaced Repetition
&lt;/h3&gt;

&lt;p&gt;After you have studied the topics, you have to repeat it, as the neurons in your brain only remember the things that are repeated over and over to them, the reason &lt;a href="https://www.psychologytoday.com/us/blog/defining-memories/201706/why-we-forget"&gt;why your brain forgets things&lt;/a&gt; because your brain tries to remove the things that you do not need, so you have to tell your brain that you need to learn the topic by repeating it over time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Spaced_repetition"&gt;Spaced repetition&lt;/a&gt; is a learning technique that helps you review the topics at gradually increasing intervals, what does it mean? It means that when you try to learn a topic try to recall it again after 3 days and when you still know it tries it again after 6 days and if you noticed that you forget some of it you have to review it again and review it in 3 days and so on. I use &lt;a href="https://apps.ankiweb.net/"&gt;Anki&lt;/a&gt; for this to help me manage the spaced repetitions of the topic. Also, one thing to improve retention is to &lt;a href="https://www.lifehack.org/articles/featured/writing-and-remembering-why-we-remember-what-we-write.html"&gt;write it&lt;/a&gt;, use pen and paper! Even if we are in the digital age. This will also help you to have an overview of the written notes or mind maps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recognition vs Recall
&lt;/h3&gt;

&lt;p&gt;There are also times that you think that you already know about the topic and mastered it. This is the feeling when you're watching some tutorial and feels like you're learning it but when you try it, you're lost, this is the recognition pitfall, as you're just recognizing it based on what you see you're not actually recalling it, recalling is when you have the capacity to self-correct and by recalling your brain tries to create a connection and improves it, the more you recall it the more the connection strengthens and improves, which makes it easier for you to pick it up when you needed that fragment of information, make sure to try it and do it on your own for you to recall it when you needed it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exercise!!!
&lt;/h3&gt;

&lt;p&gt;I know this seems weird right? I use this technique to improve my capability to learn, as the exercise increases levels of the important neurotransmitters (&lt;a href="https://www.webmd.com/depression/features/serotonin#1"&gt;serotonin&lt;/a&gt;, &lt;a href="https://www.cardiosmart.org/Healthwise/d003/23/d00323"&gt;norepinephrine&lt;/a&gt;, and &lt;a href="https://en.wikipedia.org/wiki/Dopamine"&gt;dopamine&lt;/a&gt;) that is responsible in how you process your thoughts and emotions that affect how you think, you'll notice that when you're stressed or in anger it is harder to focus and think properly. With exercise, it'll help you to feel energized and motivated this will help you to learn faster, and when you noticed that you can't focus, try to walk around your block, do some jumping jacks or even sprint just to move your muscles that could help produce energy levels that travel to your bloodstream and into your brain. &lt;/p&gt;

&lt;p&gt;On &lt;a href="https://www.youtube.com/watch?v=pO_iQBz-qW0"&gt;Dr. Terrence Sejnowski interview on how he learns&lt;/a&gt;, he uses exercise to learn more which also helps him take advantage of the &lt;em&gt;diffuse mode&lt;/em&gt; thinking.&lt;/p&gt;

&lt;h1&gt;
  
  
  Closing notes
&lt;/h1&gt;

&lt;p&gt;Lastly, to learn how to learn you have to apply some techniques if not all, try to use one or a couple of the tips/techniques first and integrate another one, try each of everything and see if it works for you, as we do have different solutions. Or you could discover or even develop your own! Enjoy the process, because if you don't enjoy the process, you'll get disappointed, sometimes you have to just take a break if you get stuck. As stated I am no genius and by no means learning fast, and I still have many knowledge gaps, I'm still learning, keep learning, and don't stop learning. GLHF!&lt;/p&gt;

&lt;p&gt;I recommend you to take the course of &lt;a href="https://www.coursera.org/learn/learning-how-to-learn"&gt;Learn how to learn by Dr. Barbara Oakley&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>motivation</category>
      <category>challenge</category>
    </item>
  </channel>
</rss>
