<?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: Snehit Sah</title>
    <description>The latest articles on DEV Community by Snehit Sah (@flyingcakes).</description>
    <link>https://dev.to/flyingcakes</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%2F636314%2Faf25a7be-91dd-46b7-aaab-f325de9856ae.jpeg</url>
      <title>DEV Community: Snehit Sah</title>
      <link>https://dev.to/flyingcakes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/flyingcakes"/>
    <language>en</language>
    <item>
      <title>Generating Combinations using C++</title>
      <dc:creator>Snehit Sah</dc:creator>
      <pubDate>Fri, 23 Jul 2021 12:01:09 +0000</pubDate>
      <link>https://dev.to/flyingcakes/generating-combinations-using-c-5bhl</link>
      <guid>https://dev.to/flyingcakes/generating-combinations-using-c-5bhl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The task for this post is to help you understand how to generate combinations of size &lt;code&gt;r&lt;/code&gt; using numbers from 1 to &lt;code&gt;n&lt;/code&gt; and then extend that code to generate combinations for any set of objects.&lt;/p&gt;

&lt;p&gt;When I say &lt;code&gt;n = 5&lt;/code&gt; and &lt;code&gt;r = 3&lt;/code&gt;, it means we are dealing with the first five natural numbers to generate combinations of size 3.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Combinations
&lt;/h2&gt;

&lt;p&gt;We shall be generating combinations in lexicographic order. Also, since combination does not concern itself with the relative order the chosen elements are arranged in, we shall always be having our chosen elements in lexicographical order too.&lt;/p&gt;

&lt;p&gt;The first combination we choose has to be the first &lt;code&gt;r&lt;/code&gt; elements when the &lt;code&gt;n&lt;/code&gt; objects are sorted. This is because it gives us the lexicographically smallest word with the individual elements themselves in lexicographical order too. (basically, the if all combinations were arranged in a dictionary, then we want to start with the first one) So, in case of &lt;code&gt;n = 5&lt;/code&gt; and &lt;code&gt;r = 3&lt;/code&gt;, the first combination is &lt;code&gt;123&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now, think what should be the last combination. I propose that it will be the last &lt;code&gt;r&lt;/code&gt; elements when the &lt;code&gt;n&lt;/code&gt; objects are sorted. Why should it be? When we are generating combinations in lexicographical order, each combination should have a greater position in a dictionary. So the last combination should have the greatest elements from given objects. The chosen elements themselves should also be in lexicographical order.&lt;/p&gt;

&lt;p&gt;The knowledge of last combination also gives us an interesting observation. The maximum value at a given index &lt;code&gt;idx&lt;/code&gt; can be &lt;code&gt;n - r + idx + 1&lt;/code&gt;, where index starts from zero. This is necessary to maintain the dictionary order of individual elements in a combination.&lt;/p&gt;

&lt;p&gt;Take for example the case of &lt;code&gt;n = 5&lt;/code&gt; and &lt;code&gt;r = 3&lt;/code&gt;. If we position the number 5 at index 1, then the combination will look like &lt;code&gt;X5_&lt;/code&gt;, where &lt;code&gt;X&lt;/code&gt; may be any number lower than 5. The blank at third position should have a number greater than 5 if we are to maintain dictionary order. But placing 6 there is not possible since we only have 5 elements. According to the formula &lt;code&gt;n - r + idx + 1&lt;/code&gt;, the maximum value at third position (or second index) can be 4.&lt;/p&gt;

&lt;p&gt;Another thing to note is that every time we increment a position, we need to set the elements to its right as one more than its left neighbor. This will be clear when I discuss the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formal Algorithm
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun next_combination :
    parameters: integer n - max objects available
                integer r - size of chosen set
                array curr_comb - denoting
                  current combination of r elements
                integer idx - index to increment

    return:     true if next combination exists
                  curr_comb is updated in place
                  with the next combination
                false if last combination is provided
    ____

    if idx &amp;lt; 0 :
        return false

    if curr_comb[idx] &amp;lt; n - r + idx + 1 :
        ++curr_comb[idx]
        for i in (idx + 1, n - 1):
            curr_comb[i] = curr_comb[i - 1]
        return true

    else :
        return next_combinaition(n, r, curr_comb, idx - 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this function to work, it should be called with &lt;code&gt;idx&lt;/code&gt; as &lt;code&gt;r - 1&lt;/code&gt;, since we want to start incrementing from last index. This code first check if the index to increment is less than 0. If yes, it means that the next combination does not exist, or in other words, we are already at last combination.&lt;/p&gt;

&lt;p&gt;Then it checks if the value at &lt;code&gt;curr_comb[idx]&lt;/code&gt; is less than its maximum permissible value. If yes, then it increments the value at &lt;code&gt;idx&lt;/code&gt; and sets its following elements as one plus their left neighbor.&lt;/p&gt;

&lt;p&gt;If the value it &lt;code&gt;idx&lt;/code&gt; is already the maximum allowed, then the function recursively calls itself with same parameters, but &lt;code&gt;idx&lt;/code&gt; decremented by one. This continues till either the next combination is found, or the function is called with &lt;code&gt;idx&lt;/code&gt; less than zero, in that case next combination does not exist.&lt;/p&gt;

&lt;p&gt;The function is recursive, so it must pass the sanity test. Does it stop recursing at some point? Each recursive call decrements &lt;code&gt;idx&lt;/code&gt; by 1. Eventually, &lt;code&gt;idx&lt;/code&gt; will be less than zero, in which case the function will no longer recurse. So, it is guaranteed that the function does not recurse indefinitely.&lt;/p&gt;

&lt;h2&gt;
  
  
  C++ Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;increment_neighbor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;_next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                       &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;increment_neighbor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;_next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curr_comb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following are the required includes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;algorithm&amp;gt;
#include &amp;lt;utility&amp;gt;
#include &amp;lt;vector&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is a direct implementation of the algorithm I shared above. The part that increments the neighbors by 1 has been extracted to its own function called &lt;code&gt;increment_neighbor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You might ask, why are there two functions named &lt;code&gt;next_combination&lt;/code&gt;, one with a preceding underscore. The goal of a good interface is to be as simple as possible. In the recursive function, we initially call it with &lt;code&gt;idx = r - 1&lt;/code&gt;. For a programmer reusing our function, it may be tedious to pass the fourth parameter, when it can be simply deduced from the third second one.&lt;/p&gt;

&lt;p&gt;So, we create a helper function that takes the three required parameters and calls the original recursive function with fourth parameter set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Our Function
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// take input for n and r&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter r individual elements (integers) of"&lt;/span&gt;
                 &lt;span class="s"&gt;" current combination separated by spaces&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Next combination is&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"The input is already the final combination&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code will simply check if &lt;code&gt;next_combination&lt;/code&gt; return true or false. If true, it means that the next combination was found and it is printed. Otherwise, inform user that we are already at last combination.&lt;/p&gt;

&lt;p&gt;To generate all combination, use the following snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running on Custom Elements
&lt;/h2&gt;

&lt;p&gt;Lets say the user does not want to generate combination of numbers, but instead wants to generate combinations of a custom set of objects. The object can be anything - character, string, struct etc.&lt;/p&gt;

&lt;p&gt;The basic idea is to store the &lt;code&gt;n&lt;/code&gt; individual elements in an array. Generate the combination on numbers from 1 to &lt;code&gt;n&lt;/code&gt; as we did earlier. But while printing the combination, instead of number, print the element at that position.&lt;/p&gt;

&lt;p&gt;As an example, here is the snippet for generating combinations of words.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Enter n individual elements (words)"&lt;/span&gt;
                &lt;span class="s"&gt;" separated by spaces&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cin&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;words&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next_combination&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Memoization
&lt;/h2&gt;

&lt;p&gt;Recursive algorithms can be made much faster when using memoization. Do we need memoization?&lt;/p&gt;

&lt;p&gt;Memoization helps when the function is called with the same parameters multiple times. By storing intermediate results, it does not need to do computations if the parameters provided are not new.&lt;/p&gt;

&lt;p&gt;In our function, &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt; are constant throughout. &lt;code&gt;idx&lt;/code&gt; changes its value, but it does so in a very small range. So there seems to be lot af scope for repetition of parameters. However, the parameter &lt;code&gt;curr_comb&lt;/code&gt; does not repeat. Each combination is different. When recursing with the same combination, then the value of &lt;code&gt;idx&lt;/code&gt; is guaranteed to change.&lt;/p&gt;

&lt;p&gt;So we need not use memoization, as the intermediate values are never reused.&lt;/p&gt;

&lt;p&gt;Thanks for reading! Share your feedback in the comments &amp;lt;3&lt;/p&gt;

&lt;p&gt;You can follow my personal blog at &lt;a href="https://flyingcakes85.github.io/blog/"&gt;https://flyingcakes85.github.io/blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on GitHub : &lt;a href="https://github.com/flyingcakes85"&gt;flyingcakes85&lt;/a&gt;&lt;/p&gt;

</description>
      <category>cpp</category>
      <category>coding</category>
      <category>math</category>
    </item>
    <item>
      <title>Fixing Jekyll/Webrick Issue on Arch Linux</title>
      <dc:creator>Snehit Sah</dc:creator>
      <pubDate>Mon, 24 May 2021 11:42:46 +0000</pubDate>
      <link>https://dev.to/flyingcakes/fixing-jekyll-webrick-issue-on-arch-linux-4mah</link>
      <guid>https://dev.to/flyingcakes/fixing-jekyll-webrick-issue-on-arch-linux-4mah</guid>
      <description>&lt;p&gt;Jekyll may not run on Arch Linux because of an error with &lt;code&gt;webrick&lt;/code&gt;. This is caused because webrick is not yet compatible with latest version of Ruby (3.0.1). &lt;/p&gt;

&lt;p&gt;This is the relevant line from error output.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;/blog/vendor/bundle/ruby/3.0.0/gems/jekyll-4.2.0/lib/jekyll/commands/serve/servlet.rb:3:in &lt;code&gt;require&lt;/code&gt;: cannot load such file -- webrick (LoadError)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While setting up my GitHub pages blog, I faced this issue. I'll quickly document how to fix this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Bundle
&lt;/h3&gt;

&lt;p&gt;You will need to have &lt;code&gt;rubygems&lt;/code&gt; installed for this.&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;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; rubygems
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then install &lt;code&gt;bundle&lt;/code&gt; via gem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gem &lt;span class="nb"&gt;install &lt;/span&gt;bundle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gem will print a warning about not having the local binary folder in your path. Add the following line to your &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshrc&lt;/code&gt; depending on the shell you use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export PATH=/home/$USER/.local/share/gem/ruby/3.0.0/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you replace "3.0.0" with the version number gem reports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uninstall Ruby
&lt;/h3&gt;

&lt;p&gt;We need to remove the latest Ruby version.&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;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-Rns&lt;/span&gt; ruby rubygems
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Install Ruby 2.7
&lt;/h3&gt;

&lt;p&gt;Webrick is compatible with Ruby 2.7. This is the version we will install.&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;sudo &lt;/span&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; ruby2.7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Symlink &lt;code&gt;ruby&lt;/code&gt; Binary
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;bundle&lt;/code&gt; will call the binary at &lt;code&gt;/usr/bin/ruby&lt;/code&gt;. However, the old Ruby version is installed at &lt;code&gt;/usr/bin/ruby-2.7&lt;/code&gt;. So we will create a symlink. You will need to uninstall &lt;code&gt;ruby&lt;/code&gt; as I said earlier.&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;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /usr/bin/ruby-2.7 /usr/bin/ruby 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, &lt;code&gt;/usr/bin/ruby&lt;/code&gt; will just be a symlink (or shortcut in layman terms) to &lt;code&gt;/usr/bin/ruby-2.7&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Run &lt;code&gt;bundle&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Go to your Jekyll project directory. Install the dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then launch your Jekyll site&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle &lt;span class="nb"&gt;exec &lt;/span&gt;jekyll serve
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your website should be now live!  🚀&lt;/p&gt;

&lt;p&gt;You can follow my personal blog at &lt;a href="https://flyingcakes85.github.io/blog/"&gt;https://flyingcakes85.github.io/blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on GitHub : &lt;a href="https://github.com/flyingcakes85"&gt;flyingcakes85&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>jekyll</category>
      <category>webrick</category>
      <category>archlinux</category>
    </item>
    <item>
      <title>Beginner's Guide To Shell Aliases With Examples</title>
      <dc:creator>Snehit Sah</dc:creator>
      <pubDate>Mon, 24 May 2021 07:09:42 +0000</pubDate>
      <link>https://dev.to/flyingcakes/beginner-s-guide-to-shell-aliases-with-examples-5hbj</link>
      <guid>https://dev.to/flyingcakes/beginner-s-guide-to-shell-aliases-with-examples-5hbj</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;*nix shell is a powerful tool that can help you go about your work easily. At the lowest level, it can be It can seamlessly run binaries and this power combined with shell grammar, you can write scripts to automate processes on your system.&lt;/p&gt;

&lt;p&gt;Shell has this feature where you can define aliases to existing commands. So, if I alias &lt;code&gt;w&lt;/code&gt; to &lt;code&gt;wget&lt;/code&gt;, then every time I run &lt;code&gt;w&lt;/code&gt;, it will be interpreted as &lt;code&gt;wget&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Aliases
&lt;/h2&gt;

&lt;p&gt;Shell aliases can be easily defined by the following syntax:&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;alias&lt;/span&gt; &amp;lt;keyword&amp;gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"some long command"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, for the above example, I can write&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;alias &lt;/span&gt;&lt;span class="nv"&gt;w&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"wget"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This line goes in your &lt;code&gt;~/.bashrc&lt;/code&gt; or &lt;code&gt;~/.zshrc&lt;/code&gt; depending on the shell you use. After adding an alias, you need to reload your shell config. This can be done simply by restarting your teminal, or if you don't want to close the current running terminal, you can 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;&lt;span class="nb"&gt;source&lt;/span&gt; ~/.zshrc   &lt;span class="c"&gt;# bash users replace with ~/.bashrc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Some Useful Aliases I Keep
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Text Editor
&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;alias &lt;/span&gt;&lt;span class="nv"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"nvim"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am quite often in the terminal messing with configuration files and all, meaning I regularly open the text editor. So, I created a single character alias to launch nvim.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Commands
&lt;/h3&gt;

&lt;p&gt;I alias the common Git commands. Some people like to go overboard and add lots of Git related aliases. Do whatever you are comfortable with.&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;alias &lt;/span&gt;&lt;span class="nv"&gt;gcm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"git checkout main"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;gp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"git pull"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;gpu&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"git push"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Aliases for Development Tools
&lt;/h3&gt;

&lt;p&gt;I first did this when I was learning Rust.&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;alias &lt;/span&gt;&lt;span class="nv"&gt;ccl&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cargo clean"&lt;/span&gt;  &lt;span class="c"&gt;# cc conflicts with gcc/cc&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;cch&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cargo check"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;cb&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cargo build"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;cr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cargo run"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;ca&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"cargo add"&lt;/span&gt;
&lt;span class="nb"&gt;alias &lt;/span&gt;&lt;span class="nv"&gt;rn&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"rustup override set nightly"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can set aliases for whichever language or build tools you generally use.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;ls&lt;/code&gt; commands
&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;alias &lt;/span&gt;&lt;span class="nv"&gt;l&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"ls -alh --color=tty"&lt;/span&gt;
&lt;span class="nb"&gt;alias ls&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"ls --color=tty"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I simply use &lt;code&gt;l&lt;/code&gt; to list files in long format.&lt;/p&gt;

&lt;h3&gt;
  
  
  cat -&amp;gt; bat
&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;alias cat&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"bat"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/sharkdp/bat"&gt;bat&lt;/a&gt; is a cat clone with host of new features. The major reason why I use bat is the syntax highlight and line numbering. Using Linux distros for a few years now, I am used to running &lt;code&gt;cat&lt;/code&gt; in the terminal, so I aliased it to &lt;code&gt;bat&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clear Terminal
&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;alias &lt;/span&gt;&lt;span class="nv"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"clear"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes I am too lazy to type clear so I aliased it to &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Copy to Clipboard
&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;alias &lt;/span&gt;&lt;span class="nv"&gt;cpy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"xclip -sel clip"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Often you need to copy the output of a command on to the clipboard. If the output is short, you can use your mouse to select text and press Ctrl+Shift+C. However, if the output is long, good luck copying it.&lt;/p&gt;

&lt;p&gt;With the above alias, I pipe the output of a command to &lt;code&gt;cpy&lt;/code&gt; and it will be copied on to the clipboard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;inxi &lt;span class="nt"&gt;-F&lt;/span&gt; | cpy   &lt;span class="c"&gt;# output of inxi -F is copied to clipboard&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Youtube-dl Command
&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;alias &lt;/span&gt;&lt;span class="nv"&gt;ytmp3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"youtube-dl --extract-audio --audio-format mp3 --audio-quality 256K --add-metadata --metadata-from-title &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;%(artist)s - %(title)s&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; -o &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;%(title)s.%(ext)s&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This downloads the audio stream from the supplied link and add metadata to file in &lt;em&gt;Artist - Title&lt;/em&gt; format.&lt;/p&gt;

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

&lt;p&gt;These were just some examples of the aliases I use. You can set more such aliases to make your workflow faster.&lt;/p&gt;

&lt;p&gt;You can follow my personal blog at &lt;a href="https://flyingcakes85.github.io/blog/"&gt;https://flyingcakes85.github.io/blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on GitHub : &lt;a href="https://github.com/flyingcakes85"&gt;flyingcakes85&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://www.pexels.com/@anete-lusina?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;Anete Lusina&lt;/a&gt; from &lt;a href="https://www.pexels.com/photo/crop-hacker-silhouette-typing-on-computer-keyboard-while-hacking-system-5240547/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;Pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>unix</category>
      <category>linux</category>
    </item>
    <item>
      <title>Switching to Programmer Dvorak - 3 Months Later</title>
      <dc:creator>Snehit Sah</dc:creator>
      <pubDate>Mon, 24 May 2021 02:25:00 +0000</pubDate>
      <link>https://dev.to/flyingcakes/switching-to-programmer-dvorak-3-months-later-4a62</link>
      <guid>https://dev.to/flyingcakes/switching-to-programmer-dvorak-3-months-later-4a62</guid>
      <description>&lt;p&gt;I've been trying to learn touch typing for long. Like most people, I practiced on qwerty, but could never actually touch type with confidence.&lt;/p&gt;

&lt;p&gt;First time I touch typed, I was in third grade. As a part of the computer club, I had to type out a children's book into MS Word and then copy paste the text into Windows Movie Maker to make a short video with the text and pictures. That ended up being my last touch with touch typing for ten years.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start Fresh
&lt;/h2&gt;

&lt;p&gt;In 2020, I decided to learn touch typing, no matter what! Easy thing to say, but after typing for ten years by looking at the keyboard, I couldn't get rid of that habit. Looking back, I felt this was an investment I was too lazy to&lt;br&gt;
do. During my school days, I learnt basic C++, did some competitive coding, did lot of HTML and CSS, typed many many documents and even got Microsoft Office Specialist Certification - all via hunting and pecking on the keyboard.&lt;/p&gt;

&lt;p&gt;Old habits are tough to change. I practiced on qwerty, and reached a tiny 40wpm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switching to new layout
&lt;/h2&gt;

&lt;p&gt;A random user on a discord server suggested me to try out an alternative layout so that looking at the keyboard becomes futile. The idea resonated with me. Up until then, I never considered using an alternate layout.&lt;/p&gt;

&lt;p&gt;I looked up on the net, and found that Dvorak kinda fit my needs. Claimed to be comfortable. Common alphabets placed on the home row. And right hand has more movement than the left, which is good for right handed people like me.&lt;/p&gt;

&lt;p&gt;I started practicing on this website called &lt;a href="https://learn.dvorak.nl/"&gt;learn.dvorak.nl&lt;/a&gt;. The website does not require you to change your layout from system settings. Instead, the website remaps keys based on input from your qwerty input. Of course, you can disable that feature if you have changed to Dvorak from system settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter Programmer Edition
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--56m0KpOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgwn3x04y30m0bvw1hoe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--56m0KpOQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vgwn3x04y30m0bvw1hoe.png" alt="Programmer Dvorak Layout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;About two weeks later, I was comfortable typing on Dvorak keyboard. As they claimed, it was easy to learn. I wasn't a fast typer by any measure, but at least the keys were memorized and I could type without looking at the keyboard - even if it was at 15wpm. I decided I was ready to switch to Dvorak full time.&lt;/p&gt;

&lt;p&gt;I went to the Arch Wiki to know how to change my layout. There I found another Dvorak based layout - Programmer Dvorak. The idea behind this layout was to place the common symbols used in programming on the top number row, and have them inserted without pressing Shift. The numbers need to be inserted with Shift key combination. Alphabets were in their right locations as they are on Simplified Dvorak.&lt;/p&gt;

&lt;p&gt;This layout, for obvious reasons, immediately appealed to me. I started using it right away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Experience
&lt;/h2&gt;

&lt;p&gt;I get the typing benefits of Dvorak, which are great by the way. I feel less hand movement as compared to my Qwerty touch typing sessions. Layout makes sense, although I am not a fan of positioning the letter &lt;code&gt;l&lt;/code&gt; where it is.&lt;/p&gt;

&lt;p&gt;The layout is comfortable too. My wrists don't curse me anymore when I am typing for long hours.&lt;/p&gt;

&lt;p&gt;Having symbols on the top row makes them easily accessible. I am surprised by the fact that I am now happily typing out symbols while coding without requiring to press Shift.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bottom line
&lt;/h2&gt;

&lt;p&gt;Simplified Dvorak is good for all practical purposes. Programmer Dvorak comes with a small problem that the numbers on top row are not in increasing order. Typing numbers has been a severe pain for me, and those who watched me live stream my code often asked me why I was typing wrong whenever I had to type a number.&lt;/p&gt;

&lt;p&gt;I faced another crisis when I gave my programming test at university. They had Windows computer, and I wasn't allowed to use my laptop to give the test. Dvorak is a universal layout which you will find on all modern operating systems. However, Programmer Dvorak is not universal, meaning I did not have that for my exam. Alphabets were easy to type, but typing symbols - which you do after almost every word - was a serious pain. I suffered the torture for the first task and switched to Qwerty for my second task. Hunt and peck is more efficient than pressing random keys and hoping they are correct.&lt;/p&gt;

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

&lt;p&gt;If you are happy with your current layout, continue with it by all means. However, even if there's a tiny bit of curiosity in you to try out alternate layouts, then make sure you experiment.&lt;/p&gt;

&lt;p&gt;If you want to be able to type on any computer - in case you type on other's systems often - then Simplified Dvorak is the best option. If you want Dvorak but are mostly typing on your own system (like me) do consider Programmer Dvorak. &lt;/p&gt;

&lt;p&gt;If you don't mind using a non-universal layout, then there is Colemak layout, which looks quite impressive. Its easy to transition from Qwerty to Colemak. And reading up on their website, it certainly is worth trying.&lt;/p&gt;

&lt;p&gt;For me, I am happy with Programmer Dvorak and am going to continue with it.&lt;/p&gt;

&lt;p&gt;You can follow my personal blog at &lt;a href="https://flyingcakes85.github.io/blog/"&gt;https://flyingcakes85.github.io/blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on GitHub : &lt;a href="https://github.com/flyingcakes85"&gt;flyingcakes85&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://www.pexels.com/@john-petalcurin-750345?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;John Petalcurin&lt;/a&gt; from &lt;a href="https://www.pexels.com/photo/close-up-photo-of-gaming-keyboard-2115257/?utm_content=attributionCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=pexels"&gt;Pexels&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typing</category>
      <category>dvorak</category>
    </item>
    <item>
      <title>Arch Linux Package Management Beginner's Cheat Sheet</title>
      <dc:creator>Snehit Sah</dc:creator>
      <pubDate>Sun, 23 May 2021 14:34:27 +0000</pubDate>
      <link>https://dev.to/flyingcakes/arch-linux-package-management-beginner-s-cheat-sheet-3kb8</link>
      <guid>https://dev.to/flyingcakes/arch-linux-package-management-beginner-s-cheat-sheet-3kb8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Arch Linux comes with a powerful package manager called pacman. Often new users take time to figure out its commands because it does not have the usual 'obvious' commands like apt. So here is a short post about pacman commands.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note : All pacman commands need to be run with superuser privileges. You need to prepend the commands with &lt;code&gt;sudo&lt;/code&gt; for them to work. For the sake of brevity, I have omitted &lt;code&gt;sudo&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Packages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Download and Install
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;-S&lt;/code&gt; flag asks pacman to synchronize packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; package1 package2 package3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will download install specified packages along with their necessary dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Selecting Repo in Case of Multiple Providers
&lt;/h3&gt;

&lt;p&gt;By default, package is searched in repos in the order they are listed in &lt;code&gt;/etc/pacman.conf&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In case a package is available from more than one source, then you can specify the repo name before package name to select the repo you want to download the package from. For example, I am using Endeavour OS, with Chaotic AUR added to my repo list. So, I can get the &lt;code&gt;polybar&lt;/code&gt; package from two sources - EndeavourOS reop and Chaotic AUR repo. Since Endeavour OS repo has a preference in my &lt;code&gt;pacman.conf&lt;/code&gt;, pacman will download polybar from that repo. However, if I want to download it from Chaotic AUR, I can 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;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; chaotic-aur/polybar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating Packages
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Syu&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will download new package database and then install the packages that have new updates available. This will include your kernel and other core packages too so make sure you have backups ready in case something goes wrong. Timeshift is a popular backup tool.&lt;/p&gt;

&lt;h3&gt;
  
  
  Searching For a Package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Ss&lt;/span&gt; search terms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will search for the supplied terms and provide you with a list of matching entries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Download But Don't Install
&lt;/h3&gt;

&lt;p&gt;You may want to download packages, but not install them right away. This can be done by using the &lt;code&gt;-w&lt;/code&gt; flag with one of the sync commands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Sw&lt;/span&gt; reflector   &lt;span class="c"&gt;# downloads reflector but doesn't install it&lt;/span&gt;
pacman &lt;span class="nt"&gt;-Syuw&lt;/span&gt;   &lt;span class="c"&gt;# downloads available updates but doesn't install them&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  About &lt;code&gt;-Syy&lt;/code&gt;, &lt;code&gt;-Sy&lt;/code&gt; and &lt;code&gt;-Syuu&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;I edited this post and added this section because this deserves some attention. New users often use &lt;code&gt;-Syyu&lt;/code&gt; to update packages. This is not a good practice, since if you frequently update, it will put extra load on the mirrors.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-Sy&lt;/code&gt; will update only those repos that are outdated. &lt;code&gt;-Syy&lt;/code&gt; will force update all repos even if they are up to date. So avoid using &lt;code&gt;-Syy&lt;/code&gt;. It is meant to be used in case your packaged database is corrupted or has some other issue.&lt;/p&gt;

&lt;p&gt;Similarly, &lt;code&gt;-Syuu&lt;/code&gt; will force package version to be in sync with the repo. In case your mirror has an outdated version of a package, it will be forcefully downgraded. Use &lt;code&gt;-Syu&lt;/code&gt; to update packages.&lt;/p&gt;

&lt;p&gt;And finally, never use &lt;code&gt;-Sy&lt;/code&gt; alone. On Arch Linux, &lt;strong&gt;partial upgrades are not supported&lt;/strong&gt;. If you do &lt;code&gt;-Sy&lt;/code&gt; and then install a package, you will run into a case of partial upgrade and that may lead to problems. &lt;/p&gt;

&lt;p&gt;In short, use &lt;code&gt;-S&lt;/code&gt; to install and &lt;code&gt;-Syu&lt;/code&gt; to update.&lt;/p&gt;

&lt;h3&gt;
  
  
  Details for a package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Si&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will show details for the supplied package name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing packages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Remove a Package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-R&lt;/span&gt; package1 package2 package3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will remove the specified packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Remove along with unneeded dependencies
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Rs&lt;/span&gt; package1 package2 package3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;-s&lt;/code&gt; flag instructs pacman to remove any dependencies of the specified packages, that are no longer required by any other package on the system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Removing groups of packages
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Rsu&lt;/span&gt; gnome
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will remove the gnome group, but preserve any packages that are required by one or more packages installed on the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Query commands
&lt;/h2&gt;

&lt;h3&gt;
  
  
  List of installed packages
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Q&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will output the list of installed packages along with their versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  List packages no longer needed
&lt;/h3&gt;

&lt;p&gt;If you remove packages without using the &lt;code&gt;-s&lt;/code&gt; command, then unneeded dependencies can pile up on your system. You can list them with 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;pacman &lt;span class="nt"&gt;-Qdtq&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To uninstall them, simply pipe them to &lt;code&gt;pacman -Rs&lt;/code&gt; in with 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;pacman &lt;span class="nt"&gt;-Qdtq&lt;/span&gt; | pacman &lt;span class="nt"&gt;-Rs&lt;/span&gt; -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Packages no longer part of any repository
&lt;/h3&gt;

&lt;p&gt;Sometimes, when a package is no longer maintained, it is removed from repositories. You may want to remove them since they are most certainly no longer required&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Qm&lt;/span&gt;   &lt;span class="c"&gt;# list foreign packages&lt;/span&gt;
pacman &lt;span class="nt"&gt;-Qmq&lt;/span&gt; | pacman &lt;span class="nt"&gt;-Rs&lt;/span&gt; -    &lt;span class="c"&gt;# remove foreign packages&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Details for a package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Qi&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will display information for a the supplied package name. Note that this works only for installed packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Know owner of a file
&lt;/h3&gt;

&lt;p&gt;In case a system file is damaged, reinstalling the relevant package may help you fix it. In such case, you need to know which package owns the file in question.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Qo&lt;/span&gt; /usr/bin/zsh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command tells me package that owns the file I specified.&lt;/p&gt;

&lt;h3&gt;
  
  
  List explicitly installed packages
&lt;/h3&gt;

&lt;p&gt;To get the list of package installed explicitly, meaning they are not installed as dependencies, 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;pacman &lt;span class="nt"&gt;-Qe&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The "explicitly installed" tag can be modified as described here.&lt;/p&gt;

&lt;h3&gt;
  
  
  List packages installed as dependency
&lt;/h3&gt;

&lt;p&gt;To get the packages that were installed as a dependency, 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;pacman &lt;span class="nt"&gt;-Qd&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, this tag can be modified as described here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify packages
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Qk&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will check if all the files for the installed packages are available on the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Database commands
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Mark a package as explicitly installed
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-D&lt;/span&gt; &lt;span class="nt"&gt;--asexplicit&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will mark the package as explicitly installed and this package won't be considered when you run &lt;code&gt;pacman -Qdtq&lt;/code&gt; or remove commands unless you explicitly specify the package name as a candidate for removal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mark a package as dependency
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;--asdeps&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works as totally reverse of the previous command. Specified package will be considered for removal and also as unneeded dependency if no package depends on it.&lt;/p&gt;

&lt;h2&gt;
  
  
  File commands
&lt;/h2&gt;

&lt;h3&gt;
  
  
  List files installed by a package
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Fl&lt;/span&gt; package-name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will show the list of files that a package will install onto your system.&lt;/p&gt;

&lt;h3&gt;
  
  
  Search for file or filepaths
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-F&lt;/span&gt; filename
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will search for filename and list the packages that provide a file by that name. This is specially useful when I need to know which package provides a specific header file I need to use.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-F&lt;/code&gt; only accepts complete filenames, without path. To do a regex search, with filepaths and partial names, use the &lt;code&gt;-x&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pacman &lt;span class="nt"&gt;-Fx&lt;/span&gt; path/to/file
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Thats all for this post.&lt;/p&gt;

&lt;p&gt;You can follow my personal blog at &lt;a href="https://flyingcakes85.github.io/blog/"&gt;https://flyingcakes85.github.io/blog/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on GitHub : &lt;a href="https://github.com/flyingcakes85"&gt;flyingcakes85&lt;/a&gt;&lt;/p&gt;

</description>
      <category>archlinux</category>
      <category>pacman</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
