<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suvrat Apte on Medium]]></title>
        <description><![CDATA[Stories by Suvrat Apte on Medium]]></description>
        <link>https://medium.com/@suvratapte?source=rss-d0d7f7e45e30------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*brcJon_SPLWG_NcLZbBdAQ.jpeg</url>
            <title>Stories by Suvrat Apte on Medium</title>
            <link>https://medium.com/@suvratapte?source=rss-d0d7f7e45e30------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 09:40:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suvratapte/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Consistent Hashing with Clojure]]></title>
            <link>https://medium.com/helpshift-engineering/consistent-hashing-with-clojure-5804f95960f8?source=rss-d0d7f7e45e30------2</link>
            <guid isPermaLink="false">https://medium.com/p/5804f95960f8</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[clojure]]></category>
            <category><![CDATA[consistent-hashing]]></category>
            <category><![CDATA[sharding]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[Suvrat Apte]]></dc:creator>
            <pubDate>Thu, 31 Dec 2020 09:59:40 GMT</pubDate>
            <atom:updated>2021-02-16T07:11:22.144Z</atom:updated>
            <content:encoded><![CDATA[<p>In this post, let’s try and understand what <em>Consistent Hashing</em> is, when it is needed and how to implement it in Clojure. Consistent hashing has many use cases. I have chosen the use case of distributed caching for this post. One other common use case is for sharding in databases.</p><h3>Caching</h3><p>Almost all applications today use some kind of caching. Caches reduce the number of requests directly served by your database and improve latency. You start with one cache node sitting over your database. On read paths, you check if the data is available on the cache node, if not, you go to the database, serve the request and store the data on your cache node. On write paths, you update the database and then update (or invalidate) your cache. In such a setup, your cache provides fast access to items in your DB, and reduces load on the DB.</p><p>But as your application usage grows, your cache node is also going to get overwhelmed and soon enough, you will need multiple cache nodes. When you have multiple nodes, you will need to decide how you are going to divide data between those nodes.</p><h3>Distributed Caching</h3><p>One very simple strategy to divide data between cache nodes is to take an integer hash of the cache key and then take the mod by the number of cache nodes.</p><p>For example, if the hash of the cache key came out to be 86696499 and if we have 4 servers, then (86696499 mod 4) = 3; so the data will go to the node with index 3 (0 based indexes).</p><p>This is very simple to implement. For simplicity, we will use email addresses of users as cache keys.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b470f3e7775b81064e11d3f77c355bc4/href">https://medium.com/media/b470f3e7775b81064e11d3f77c355bc4/href</a></iframe><p>Let’s go through the code above.</p><p>emails (line #3) is just a collection of emails.</p><p>get-node (line #7) tells us which object should go on which node. This was the logic that we discussed previously. We are first taking a hash and then taking mod n of that hash.</p><p>get-distribution (line #13) just runs get-node on emails and returns a map which has node names as keys and corresponding values as list of keys which would reside on those nodes.</p><p>The last line prints how emails will be divided on cache nodes if we had 4 cache nodes. It prints the following map:</p><pre>{&quot;node-0&quot; [&quot;Bob@example.com&quot; &quot;Steve@example.com&quot;<br>           &quot;Zack@example.com&quot;], <br> &quot;node-1&quot; [&quot;Carry@example.com&quot; &quot;Daisy@example.com&quot;<br>           &quot;George@example.com&quot;],  <br> &quot;node-2&quot; [&quot;Mark@example.com&quot;], <br> &quot;node-3&quot; [&quot;Heidi@example.com&quot;]}</pre><p>Okay so this seems to be working well! Now every time we have to fetch some data, we will just check on which node that data is expected and then we will try to fetch it from that node. So far so good!</p><p>In today’s distributed infrastructure, the total number of cache nodes can easily change. There could be multiple reasons such as, needing a few extra nodes when more traffic is expected, or a node going down due to some error.</p><p>We can simulate the above two situations.</p><p>Let’s say we add another cache node in our cluster. We can simulate this by running:</p><pre>(-&gt;&gt; emails (get-distribution 5) (into (sorted-map)) pp/pprint)</pre><p>We will get the following:</p><pre>{&quot;node-0&quot; [&quot;Bob@example.com&quot;],  <br> &quot;node-1&quot; [&quot;Daisy@example.com&quot;],<br> &quot;node-2&quot; [&quot;Steve@example.com&quot;],<br> &quot;node-3&quot; [&quot;Carry@example.com&quot; &quot;George@example.com&quot;<br>           &quot;Mark@example.com&quot;],<br> &quot;node-4&quot; [&quot;Heidi@example.com&quot; &quot;Zack@example.com&quot;]}</pre><p>If we compare the distribution of keys for 4 nodes vs 5 nodes, we can see that 6 out of 8 keys have different nodes now.</p><p>The same will happen if a node goes down. Let’s simulate this by running:</p><pre>(-&gt;&gt; emails (get-distribution 3) (into (sorted-map)) pp/pprint)</pre><p>This produces:</p><pre>{&quot;node-0&quot; [&quot;Zack@example.com&quot;],<br> &quot;node-1&quot; [&quot;Bob@example.com&quot; &quot;Daisy@example.com&quot;<br>           &quot;George@example.com&quot;],<br> &quot;node-2&quot; [&quot;Carry@example.com&quot; &quot;Heidi@example.com&quot;<br>           &quot;Mark@example.com&quot; &quot;Steve@example.com&quot;]}</pre><p>In this case as well, 5 out of 8 keys now have a different node.</p><p>Both of these cases create a really bad situation for our databases. Initially, our data was distributed across 4 nodes. On adding or removing nodes, almost all of the keys get relocated to other nodes. This results in a flurry of cache misses and all the missed requests go to our databases. Further, our cache nodes now have stale data with no way for us to clean it up. We have to wait for the stale data to expire.</p><p>This is clearly undesirable and in extreme situations, this could even take our entire system down.</p><p>Let’s understand why this is happening. We need to remember that our logic to select nodes ( get-node) for data includes <em>number of nodes</em> as a parameter. So when our <em>number of nodes</em> changes, clearly the output of get-node is most likely to change.</p><p>We need to find a strategy which will not directly depend on the <em>number of nodes</em> that we have.</p><h3>Consistent Hashing</h3><p>Consistent hashing is a simple method of hashing which does not depend on the number of nodes we have. In consistent hashing, we imagine our nodes to be placed on a ring. The ring is made up of the range of our hash function. For example, if our hash function produced hashes over the entire range of integers, then the ring would go from the minimum integer to the maximum integer.</p><p>We will generate hashes for nodes using some unique property of nodes, say IP addresses. These hashes will be the locations of our nodes on the ring.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*MOg3T0D1yoM_3K5ulYUNPA.png" /><figcaption>Consitent hashing ring</figcaption></figure><p>To insert or retrieve data, we will hash the caching key and use the node which is closest to the caching key hash in the clockwise direction. (Clockwise is just a convention we are using for this post. Anti-clockwise will also work.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R5qF4iths1NxVtlI.png" /><figcaption>Key distribution</figcaption></figure><p>What benefit has this given us?</p><p>Well, so far it does not look useful. In fact, we are doing more work to find out which data goes to which node. We will now consider the two cases that we discussed for mod n hashing.</p><p>Let’s say we add a 5th node to our ring.</p><p>If the 5th node gets placed between the 1st and the 2nd node, think about which keys will get relocated. Only the keys between 1st and 5th node will be relocated to the 5th node. All the keys on the rest of the ring will remain where they were.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/0*dVpfYvHMOiqQfiLU.png" /><figcaption>Rebalancing on addition of a node</figcaption></figure><p>Similarly, if one of our nodes, say the 4th node, goes down; then only the keys between the 3rd and 4th will get relocated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0-CvejHl9NYrmCsU.png" /><figcaption>Rebalancing on removal of a node</figcaption></figure><p>When nodes are added or removed, only count(keys) / count(nodes) number of keys will be relocated. This will reduce the number of cache misses by a huge amount and save our databases from having to serve a large number of requests!<br><em>(There is a small caveat here which is discussed later.)</em></p><h3>Implementation in Clojure</h3><p>Let’s write a simple API for consistent hashing. Here are the functions we need:</p><ol><li>create-ring, add-node, remove-node: These functions will build and manage the ring.</li><li>get-node: This function plays the same role as it did in the mod-n hashing.</li></ol><p>Let’s look at create-ring first.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2514777302dc005d080f3b3b6a4616e4/href">https://medium.com/media/2514777302dc005d080f3b3b6a4616e4/href</a></iframe><p>We are taking the set of nodes to make sure there are no duplicate entries. Then we get the hash values for all the nodes and sort them in ascending order. This sorted list will be used to find the closest node in the ring. We also create a lookup table hash-&gt;node which gives us the node corresponding to a given hash. Finally, we return both of these so that they can be passed to other functions in our API.</p><p>Let’s see how add-node and remove-node work.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afc9f7594a0b1e2602613e9335237756/href">https://medium.com/media/afc9f7594a0b1e2602613e9335237756/href</a></iframe><p>As you can see, these are very simple functions which just get current-nodes from the ring and then add or remove a node and call create-ring again. This works because our hash function is repeatable. So creating the ring again generates the same hash values for existing nodes.</p><p>Let’s look at the last function in our API, get-node.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15394caa05d4c6535ae8e7440d74850a/href">https://medium.com/media/15394caa05d4c6535ae8e7440d74850a/href</a></iframe><p>The main logic in this function is to find the closest node to the cache-key, in clockwise direction.</p><pre>(or (-&gt;&gt; node-hashes         <br>         (drop-while #(&lt; % key-hash))<br>         first)<br>    (first node-hashes))</pre><p>node-hashes is the sorted list of hashes of our nodes. We are dropping the nodes which have hashes lesser than the hash of the cache-key. We will stop dropping once we find a value greater than key-hash. This value will be the hash of the closest node in clockwise direction. If key-hash is greater then all the values in node-hashes, we wrap around and select (first node-hashes). For simplicity, I have used sequential search for finding the closest hash. Binary search could be used for making this more efficient.</p><p>Now our API for consistent hashing is complete and ready for use!</p><p>Let’s use it for the same example scenarios that we used for mod n hashing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fce965cfea7f6619f0b896b45b2ce11b/href">https://medium.com/media/fce965cfea7f6619f0b896b45b2ce11b/href</a></iframe><p>Result with 4 nodes:</p><pre>{&quot;node-0&quot; [&quot;Daisy@example.com&quot; &quot;George@example.com&quot;],<br> &quot;node-1&quot; [&quot;Mark@example.com&quot; &quot;Zack@example.com&quot;],<br> &quot;node-2&quot; [&quot;Bob@example.com&quot; &quot;Heidi@example.com&quot;],<br> &quot;node-3&quot; [&quot;Carry@example.com&quot; &quot;Steve@example.com&quot;]}</pre><p>With a node added:</p><pre>(-&gt;&gt; emails <br>     (get-distribution (conj nodes &quot;node-4&quot;))<br>     (into (sorted-map))<br>     pp/pprint)  </pre><pre>{&quot;node-0&quot; [&quot;Daisy@example.com&quot; &quot;George@example.com&quot;],  <br> &quot;node-1&quot; [&quot;Mark@example.com&quot; &quot;Zack@example.com&quot;],<br> &quot;node-2&quot; [&quot;Heidi@example.com&quot;],<br> &quot;node-3&quot; [&quot;Carry@example.com&quot; &quot;Steve@example.com&quot;],<br> &quot;node-4&quot; [&quot;Bob@example.com&quot;]}</pre><p>Only 1 out of 8 keys got relocated!</p><p>With node-3 removed:</p><pre>(-&gt;&gt; emails <br>     (get-distribution (drop-last nodes)) <br>     (into (sorted-map)) <br>     pp/pprint)</pre><pre>{&quot;node-0&quot; [&quot;Daisy@example.com&quot; &quot;George@example.com&quot;],<br> &quot;node-1&quot; [&quot;Mark@example.com&quot; &quot;Zack@example.com&quot;],<br> &quot;node-2&quot; [&quot;Bob@example.com&quot; &quot;Carry@example.com&quot; &quot;Heidi@example.com&quot;<br>           &quot;Steve@example.com&quot;]}</pre><p>Only 2 out of 8 keys got relocated. We can see that node-3 keys are now with node-2. Keys with node-0 and node-1 have not changed at all.</p><h3>Caveats</h3><p>Our implementation above is not something that can be used in production. The problem is that when we have only a few nodes, we could land up in a situation like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*KnVZ8Ay-RvzOIsu1I9M6nQ.png" /><figcaption>Poorly distributed nodes</figcaption></figure><p>In the above situation, most of the keys will go to Node 1.</p><p>The solution is simple. Instead of having just one hash per node, we could have multiple hashes which map to the same node. This will ensure more randomness and a better distribution of keys. It will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Is1NzezgUzCU8tyY.png" /><figcaption>Virtual nodes</figcaption></figure><p>One more advantage of using virtual nodes is that you can have a heterogenous cluster where some nodes are more powerful than others. In such clusters, you can assign more virtual nodes to the powerful nodes for better utilization of resources.</p><h3>Conclusion</h3><ul><li>Consistent hashing is a simple and great caching strategy to make sure your databases are protected from hotspot in a distributed environment.</li><li>Consistent hashing is able to achieve this by getting rid of number of nodes as a parameter to hashing.</li><li>When nodes are added or removed, at most count(keys) / count(nodes) number of keys will be relocated.</li><li>Persistent databases like Cassandra and Dynamodb use consistent hashing for sharding data.</li><li>Many in-memory datastores today use consistent hashing.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5804f95960f8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/consistent-hashing-with-clojure-5804f95960f8">Consistent Hashing with Clojure</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configuring Emacs from Scratch — use-package]]></title>
            <link>https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-use-package-c30382297877?source=rss-d0d7f7e45e30------2</link>
            <guid isPermaLink="false">https://medium.com/p/c30382297877</guid>
            <category><![CDATA[elisp]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[emacs]]></category>
            <dc:creator><![CDATA[Suvrat Apte]]></dc:creator>
            <pubDate>Thu, 05 Dec 2019 18:11:12 GMT</pubDate>
            <atom:updated>2021-04-06T05:21:21.936Z</atom:updated>
            <content:encoded><![CDATA[<h3>Configuring Emacs from Scratch — use-package</h3><p><em>This is the third part of a series on “Configuring Emacs from Scratch”.<br>You can read the first part </em><a href="https://medium.com/@suvratapte/configuring-emacs-from-scratch-intro-3157bed9d040"><em>here</em></a><em> and the second part </em><a href="https://medium.com/@suvratapte/configuring-emacs-from-scratch-packages-220bbc5e55b7"><em>here</em></a><em>.</em></p><p>In the last two parts, we customized some defaults of Emacs. We also installed two packages and customized their default behavior. In this part, we will organize our configuration and make it portable using <strong><em>use-package</em></strong>.</p><p>After the first two parts, our init.el looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/040cc6216142a580f54fc0b6b2f65e08/href">https://medium.com/media/040cc6216142a580f54fc0b6b2f65e08/href</a></iframe><p>What problems do you see in this configuration? There is one major and one minor problem <em>(and there must be many which I have missed)</em>.</p><ol><li>This setup is not portable.</li><li>The structure of code is too flat.</li></ol><h4>This setup is not portable</h4><p>We cannot just take our init.el from one computer to other and then expect to have our Emacs setup portes. What do you think will happen when you copy your init.el to a new computer?</p><p><em>What is our code assuming?<br></em>Is our code telling Emacs to install spacemacs-theme and company? No!<br>We are assuming that the packages - spacemacs-theme and company will already be there on all computers.</p><p>When you open Emacs on a new computer, it will only have the default Emacs packages. So the <em>load-theme</em> call in our init.el will fail. Enabling company-mode will also fail since there is nothing called company-mode at this point. So all your configuration code related to spacemacs-theme and company will either fail or will be ineffective.<br>An example where it will not fail but will be ineffective is when you set spacemacs-theme-comment-bg to nil. This call will not fail. It will just create a variable. But it will be ineffective since there is no package which uses that variable.</p><p>So the conclusion is - <strong>There is no way to port this setup without manually installing all the packages.</strong></p><h4>The structure of the code is too flat</h4><p>Currently, our configuration code is not grouped based on packages. You can always group it manually by keeping all configurations for a package together. But that can get hard to maintain. As you go on adding packages and customizing little things here and there, it is easy to spill configuration of a package all over init.el.</p><p>This might not even feel like a problem at this point in time. But as our setup evolves, this will be a big problem for managing the setup.</p><p>There are many different solutions for the above two problems. The natural way of solving the first problem will be to maintain a list of packages that we use and check if those are installed. If some package is not installed, install it. Something like this:</p><pre>(defvar my-packages &#39;(spacemacs-theme company))</pre><pre>(dolist (p my-packages)<br>  (when (not (package-installed-p p))<br>    (package-install p)))</pre><p><em>Note: In Elips any function ending with a </em><em>-p is a predicate.</em></p><p>This solution works perfectly well.<br>But there is a very elegant solution for this (which happens to solve the second problem as well).<br>It is called <strong><em>use-package</em></strong>.</p><h3>Use-package</h3><p><a href="https://github.com/jwiegley/use-package"><strong><em>use-package</em></strong></a> is an Elisp <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Macros.html">macro</a> written by <a href="https://github.com/jwiegley"><strong>John Wiegley</strong></a>. It simplifies and groups together configuration for packages. Install use-package by pressing:<br><em>(By now, you should know how to install a package.)</em></p><pre>M-x package-install &lt;RET&gt; use-package &lt;RET&gt;</pre><p>A common use-package declaration looks like this:</p><pre>(use-package &lt;package-name&gt;<br>  :init<br>  &lt;code to be executed before loading the package&gt;</pre><pre>  :config<br>  &lt;code to be executed after loading the package&gt;</pre><pre>  :bind<br>  &lt;key bindings for this package&gt;)</pre><p><em>Note: Any word, preceded with a </em><em>: is called a </em><em>keyword in Elisp. You can consider keywords as being similar to strings, but with a different purpose and presence.</em></p><p>Let’s see how would a use-package declaration look for company. Our current config for company is:</p><pre>(global-company-mode t)</pre><pre>(define-key company-active-map (kbd &quot;C-n&quot;) &#39;company-select-next)<br>(define-key company-active-map (kbd &quot;C-p&quot;) &#39;company-select-previous)</pre><pre>(setq company-idle-delay 0.0)</pre><p>With <em>use-package</em>, it will look like this:</p><pre>(use-package company<br>  :bind (:map company-active-map<br>         (&quot;C-n&quot; . company-select-next)<br>         (&quot;C-p&quot; . company-select-previous))<br>  :config<br>  (setq company-idle-delay 0.3)<br>  (global-company-mode t))</pre><p>Here we are saying-</p><ol><li>This is a use-package declaration for the package company.</li><li>In company-active-map, bind C-n to company-select-next and C-p to company-select-previous.<br><em>Notice the minimal syntax here vs the </em><em>define-key call.</em></li><li>After company is loaded, set company-idle-delay to 0.3 and enable company mode everywhere.</li></ol><p>Now all the configuration related to company is naturally grouped together in the use-package declaration. Next time when we want to make some changes to company, we will come to this declaration and add our code. This way, our setup will always be well grouped. So use-package has solved our second problem.</p><p><em>But what about the first problem? Will use-package download the missing packages?</em></p><p>Yes, it will. You just have to add :ensure t to the declaration. For example, look at this:</p><pre>(use-package magit<br>  :ensure t<br>  :bind (&quot;C-x g&quot; . magit-status))</pre><p><em>Note: </em><a href="https://magit.vc/"><em>Magit</em></a><em> is an super awesome git client for Emacs that you </em><strong><em>must</em></strong><em> use.</em></p><p>:ensure t will make sure that magit is downloaded if it is not there. Also, look at the bind syntax here. When you want to make a global binding (unlike the bind in company, which was local to company-mode-map), the syntax is even more minimal. You just have to specify (key-binding . command) pairs. :)</p><p><strong><em>use-package</em></strong> has solved both of our problems! :)</p><p><em>Note: use-package has many more useful (and very well thought of) keywords. I recommend going through the </em><a href="https://github.com/jwiegley/use-package"><em>README.md</em></a><em>.</em></p><p><em>Note: Even though use-package is a handy macro, we must know what is going on under the hood when we use it. Since it is just a macro, you can expand it yourself and see what is going on. Read about macro expansion </em><a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Expansion.html"><em>here</em></a><em>.</em></p><p><em>So is our setup fully portable now?<br></em>Nope! There is still one small problem. use-package is not there in Emacs by default and you cannot use use-package to install itself. :P</p><p>So to make our setup fully portable, we need to check if use-package is installed and if it is not, we need to install it.</p><pre>(when (not (package-installed-p &#39;use-package))<br>  (package-refresh-contents)<br>  (package-install &#39;use-package))</pre><p>Now our init.el will look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c16fc7d78311b3f914a318ca6629d5da/href">https://medium.com/media/c16fc7d78311b3f914a318ca6629d5da/href</a></iframe><p>Here are a few packages (along with their configurations) that I think every Emacs setup should have:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a781b31542c9908cd1ed70d8bd91a3bf/href">https://medium.com/media/a781b31542c9908cd1ed70d8bd91a3bf/href</a></iframe><h3>Conclusion</h3><p>In this three part series on configuring Emacs from scratch, we have seen,</p><ul><li>The Emacs way of text editing.</li><li>Using the Help system to know what is going on.</li><li>Tweaking the defaults of Emacs.</li><li>Installing packages.</li><li>Organizing your setup.</li><li>Making your setup portable.</li></ul><p>While doing this, we have seen how customizable Emacs is! :)</p><p>I have not covered even a percent of the capabalities that Emacs has. <a href="https://orgmode.org/">Org-mode</a> in itself is a topic of another (huge) series of posts. And there are many more packages which make Emacs special and unique.</p><p>But I think we have covered enough to be able to proceed on our own now.<br><strong>The key is to know how to use Emacs’s help system.</strong> <br><em>(Sorry for repeating this over and over again, but you will realize the power and convenience of Emacs’s help as you use it.)</em></p><p>You can find my Emacs setup <a href="https://github.com/suvratapte/dot-emacs-dot-d">here</a>. Feel free to borrow things. If you find better ways, please send pull requests. :)<br><em>(It is strongly recommended to have your Emacs configuration under version control.)</em></p><p><em>I must mention and thank </em><a href="https://github.com/narendraj9"><em>Narendra Joshi</em></a><em> and </em><a href="https://github.com/vedang"><em>Vedang Manerikar</em></a><em> as I’ve always stolen (and look forward to stealing) things from their Emacs configurations.</em></p><h3>Tips on “Evolving your Emacs setup”</h3><p>The strategy I use to improve my Emacs setup is called <strong>UFFL</strong>.<br><em>(Honestly, only I call it UFFL.)</em></p><h4><strong>Use - Find painpoints - Fix - Loop</strong></h4><p><em>As you would have guessed, I have totally made this up just to make it analogous to </em><a href="https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop"><em>REPL</em></a><em>. But it does describe the strategy well. :)</em></p><p>After making an edit to my configuration, I use it for a few days for my day to day tasks. I often find some painpoints <em>(i.e. things that can be improved / automated / achieved in lesser number of key strokes) </em>while using it. Then I find solutions to those problems.<br>Usually, I try to solve the problems myself by finding the right configuration parameters or by writing some Elisp. Then I check if there are solutions on <a href="https://emacs.stackexchange.com/">Emacs Stack Exchange</a> (or anywhere on the Internet). In most of the cases, solutions from the Internet are better and more elegant than my own solutions.<br>But I still recommend finding/writing your own solution before looking up online, so that you get better at Elisp and at the Emacs way of thinking. :)</p><p>And this goes on forever …</p><p><strong><em>The joy is in the journey!</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*6O2fiWWJy7Ban_oF5AJejg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c30382297877" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-use-package-c30382297877">Configuring Emacs from Scratch — use-package</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configuring Emacs from Scratch — Packages]]></title>
            <link>https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-packages-220bbc5e55b7?source=rss-d0d7f7e45e30------2</link>
            <guid isPermaLink="false">https://medium.com/p/220bbc5e55b7</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[elisp]]></category>
            <category><![CDATA[emacs]]></category>
            <dc:creator><![CDATA[Suvrat Apte]]></dc:creator>
            <pubDate>Mon, 02 Dec 2019 18:03:17 GMT</pubDate>
            <atom:updated>2021-04-06T05:21:16.990Z</atom:updated>
            <content:encoded><![CDATA[<h3>Configuring Emacs from Scratch — Packages</h3><p><em>This is the second part of a series on “Configuring Emacs from Scratch”.<br>You can read the first part </em><a href="https://medium.com/@suvratapte/configuring-emacs-from-scratch-intro-3157bed9d040"><em>here</em></a><em>.</em></p><p>In the last part, we customized some defaults of Emacs. But Emacs is not at all limited to customizing the defaults. Emacs has a <strong>huge</strong> eco-system of external packages that you can install. The default package manager for Emacs is called “package”. Package can fetch packages from multiple sources. Elpa (Emacs Lisp Package Archive) is the source that it uses by default. But users usually add <a href="https://melpa.org/#/">Melpa</a> and <a href="https://marmalade-repo.org/">Marmalade</a> to their list of package sources.</p><p>In this part, we will add a few useful packages and learn how to configure them according to our needs.</p><h3>Installing packages</h3><p>First, we will tell package to include Melpa in its list of package archives. To do that, add these lines to your init.el:</p><pre>(require &#39;package)<br>(package-initialize)<br>(add-to-list &#39;package-archives<br>             &#39;(&quot;melpa&quot; . &quot;<a href="https://melpa.org/packages/">http://melpa.org/packages/</a>&quot;) t)</pre><p>The first line tells Emacs to load the feature/file called “package”. This will load functions and variables from package.<br>The second line is some initialization that package does. <br>The third line tells package to add melpa to package-archives. In Elisp, a pair can be constructed with (&lt;element 1&gt; . &lt;element 2&gt;). The last argument t means that the pair must be appended to the list. Without t, the pair would be prepended to package-archives.</p><p>Go to the end of (require &#39;package) line and execute it by pressing C-x C-e. Do the same with (package-initialize). Now check what is the value of package-archives with C-h v package-archives as we have done all along. Execute the add-to-list function call and then check the value of package-archives to confirm that melpa is added. Also, notice that the value is a list of pairs.</p><p>Let’s see all the packages available for Emacs (from sources listed in package-archives of course). Press M-x and run package-refresh-contents. This will fetch updated content. Now you can see all the packages with M-x package-list-packages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rwoP08Le2y9qmC84bZulnw.png" /><figcaption>package-list-packages</figcaption></figure><p>At the time of this writing, there are 4705 packages (from gnu + melpa). Now let’s install some package from this list. The default Emacs theme is quite unamusing, so let’s change the theme. Search for spacemacs-theme (at the time of writing this, it was on line #4008). Press RET while point is on the name. Details of spacemacs-theme will open up in a new window. Click on install (yes, I’m asking you to click using your mouse/trackpad. 😛) and then on Yes on the confirmation box.</p><p>This package contains a light and a dark version of the spacemacs theme, to apply the dark version, press M-x load-theme &lt;RET&gt; spacemacs-dark &lt;RET&gt;. Emacs will ask you if it is okay if this theme runs some code - answer with a y. Then it will ask if it should treat this theme as <em>safe</em> for future sessions - answer with a y. And there you have a beautiful dark theme (used by <a href="http://spacemacs.org/">Spacemacs</a>) ! :)</p><p>Emacs must have added some code at the end of your init.el. When you said <em>yes</em> to mark this theme safe for future Emacs sessions, how is Emacs supposed to know this in future sessions? By adding code to init.el!<br>If you read the code, you will understand that it is just adding this theme in custom-safe-themes.</p><p>To tell Emacs to add such code to some other file instead of init.el, write this line in init.el:</p><pre>(setq custom-file &quot;~/.emacs.d/custom-file.el&quot;)</pre><p>Execute this line. Then create custom-file.el in ~/.emacs.d/ and paste the code added by Emacs to this file and remove it from init.el. Also, add this line in init.el to make sure that the custom-file is loaded.</p><pre>(load-file custom-file)</pre><p><em>Note: Adding </em><em>load-file here might seem strange and unnecessary. But we will later see why it is required.</em></p><p>Think about what we just did: We went to the list of all packages, installed a package, and then used it. What do you think will happen when you restart Emacs? Will the package be there? Will the theme stay?</p><p>Well, try it out! Quit Emacs (not with the cross button; with C-x C-c) and then start it again.</p><p><em>What happened?</em> <br>Emacs went back to its old bland theme. :(</p><p><em>Was the package there?<br></em>It’s easy to check this. In your terminal, execute this command:</p><pre>ls ~/.emacs.d/elpa/</pre><p>You will see spacemacs-theme here. So the package <em>is</em> there on disk storage.<br><em>(By default, Emacs stores it’s packages in the </em><em>elpa directory.)</em></p><p><em>Why did this happen?</em><br>It’s very simple. When you start Emacs, it opens init.el and executes the code in it. Is there any code in init.el which tells Emacs to use the spacemacs-dark theme? No!<br>So let’s add that code. At the end of init.el, add this line:</p><pre>(load-theme &#39;spacemacs-dark)</pre><p>Execute this line and you’ll see that the theme is applied!<br>Now whenever you restart Emacs, it will start with the spacemacs-dark theme.</p><p><em>Note: We had written a </em><em>load-file line where I had said I will explain why it was required. Now is the time to explain that:<br>When we had loaded the theme for the first time, Emacs had asked us whether it was safe to use this theme in future sessions and then Emacs had added some code to mark it safe. We moved that code to </em><em>custom-file.el. Now, we need to make sure that the code in </em><em>custom-file.el is executed </em><strong><em>before</em></strong><em> the </em><em>load-theme line, otherwise Emacs will again ask us the same questions. That is why the </em><em>load-file line is necessary.</em></p><p>As usual, do not forget to write good comments. Your init.el should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/820006d6eca534331f575ee7c0c39421/href">https://medium.com/media/820006d6eca534331f575ee7c0c39421/href</a></iframe><h3>Customizing a package</h3><p>Every package has a few variables through which you can customize its behavior. All the variables are prefixed by the package name. To see the variables of spacemacs-theme, press C-h v spacemacs-theme- &lt;TAB&gt;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5ASSa1vNOPMTlZyXCNLpIA.png" /></figure><p>You can read details about each variable by pressing C-h v &lt;variable-name&gt;. I’m stressing on many C-h commands because it is very important to internalize the habit of using Emacs’ help system.</p><p>One thing I don’t like about the spacemacs theme is that the comments have a different background color. Let’s change that. You can do this by writing this code in init.el:</p><pre>(setq spacemacs-theme-comment-bg nil)</pre><p>This line must be added before the load-theme line because the theme needs to be reloaded after setting this var.</p><p>Like this, to customize any package, you should look at all the variables that it exposes. It is very convinient to search variables exposed by a package since they are prefixed with their package name.</p><p>Another way to customize it to use the customize GUI menu. Customize is extremely helpful and it is important to know this way of customization as well. So let’s make spacemacs-theme display comments in <em>italics</em> with Customize.</p><p>Type M-x customize &lt;RET&gt;. You will see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vf8DFK8K7xFX8Nq_F3olDw.png" /><figcaption>Customize menu</figcaption></figure><p>Go to the search box and type spacemacs-theme &lt;RET&gt;. You will see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JVXBQbEFlO0Rcat2sEWbTA.png" /><figcaption>spacemacs-theme customize menu</figcaption></figure><p>The first toggle, Spacemacs Theme Comment Bg is off and is marked as CHANGED outside Customize. This is correct since we have changed it by writing Elisp in init.el.</p><p>Now expand Spacemacs Theme Comment Italic and click on Toggle to turn it on. Click on Apply and Save and click on Yes on the confirmation box. Press q to quit the spacemacs-theme customization buffer and then q again to quit the customize buffer (your point might be on the search box - in that case, move it out of the search box and then press q). Load the theme again for the change to take effect.</p><p>Now how will Emacs (or spacemacs-theme) know to make comments italic when it restarts? It must have added some elisp code somewhere. Guess where? The custom file!</p><p>Go to the customize file and you will see two changes - spacemacs-theme-comment-bg is being set to nil and spacemacs-theme-comment-italic is being set to t. These were the changed values (different from the default values) in the customize buffer. When you clicked on Apply and Save, Emacs added this code to custom-file.</p><p>Customize menu is a good place to discover customizations, but we should always bring the changes back to our Elisp code. So here, we will add the following line to init.el before the load-theme line:</p><pre>(setq spacemacs-theme-comment-italic t)</pre><p>After this, we can remove these two lines form the custom file:<br><em>(Do not unbalance paranthesis while doing this. Check paranthesis carefully.)</em></p><pre>&#39;(spacemacs-theme-comment-bg nil)<br>&#39;(spacemacs-theme-comment-italic t)</pre><p>One (annoying) thing you must have noticed is that we don’t have auto completion while writing Elisp. Let’s fix this. <br>Install the company (COMPlete ANY) package by typing M-x package-install company &lt;RET&gt;. To enable it for this and future Emacs sessions, write (global-company-mode t) in init.el and execute this.<br>Try writing some Elisp code, and you will see a small completion window displaying possible completions. For example, when you write load-, you will see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/1*XHqOAbo_Nx8iIm4w32orlA.png" /><figcaption>Possible completions for “load-”</figcaption></figure><p>That’s nice! But how do you navigate between these completions?<br>Arrow keys. :(<br>We want to avoid moving away from the <a href="https://en.wikipedia.org/wiki/Touch_typing#/media/File:QWERTY-home-keys-position.svg">standard touch typing position</a>. So let’s change the key bindings and use C-n and C-p for navigating between the completions. Also, the completions appear after a delay of 0.5 seconds. Let’s make them appear without any delay.<br>For this, write this code at the end of your init.el:</p><pre>(define-key company-active-map (kbd &quot;C-n&quot;) &#39;company-select-next)<br>(define-key company-active-map (kbd &quot;C-p&quot;) &#39;company-select-previous)</pre><pre>(setq company-idle-delay 0.0)</pre><p>Here, we are telling Emacs to use C-n for company-select-next and C-p for company-select-previous. We want these bindings only when we have auto completions buffer displayed. Otherwise, we will end up overriding the bindings to go to next and previous lines. So we tell Emacs to define these bindings only in company-active-map. There are package specific keymaps and it is very useful to define certain bindings only for a certain package.<br>Then we tell Emacs to show possible completions without any delay by setting company-idle-delay to 0.0.</p><p>Execute these lines and try writing some more Elisp. It will much easier with auto-completion enabled. Try installing and customizing another package. Now you have powerful autocompletion in your arsenal! :)</p><p>As usual, do not forget to write good comments. Your init.el will look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/040cc6216142a580f54fc0b6b2f65e08/href">https://medium.com/media/040cc6216142a580f54fc0b6b2f65e08/href</a></iframe><h3>Conclusion</h3><p>In this part, we installed two packages and changed their default configuration. We saw how to discover configuration options by browsing variables or by going through the <em>Customize menu.</em> Customize menu is a great tool for discovering different configurations (but it is recommended to move the changed configurations back to your own Elisp code).</p><p>You would have also realized that the customization options in Emacs are far more granular than any of the IDEs. We can customize even the tiniest details and this gives us the power to make Emacs behave the way we want! :)</p><h4>Next: <a href="https://medium.com/@suvratapte/configuring-emacs-from-scratch-use-package-c30382297877">Making your setup organized and portable</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=220bbc5e55b7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-packages-220bbc5e55b7">Configuring Emacs from Scratch — Packages</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Configuring Emacs from Scratch — Intro]]></title>
            <link>https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-intro-3157bed9d040?source=rss-d0d7f7e45e30------2</link>
            <guid isPermaLink="false">https://medium.com/p/3157bed9d040</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[emacs]]></category>
            <category><![CDATA[elisp]]></category>
            <category><![CDATA[configuration]]></category>
            <dc:creator><![CDATA[Suvrat Apte]]></dc:creator>
            <pubDate>Tue, 12 Nov 2019 03:38:32 GMT</pubDate>
            <atom:updated>2021-04-06T05:20:53.874Z</atom:updated>
            <content:encoded><![CDATA[<h3>Configuring Emacs from Scratch — Intro</h3><h3>Intro</h3><p>I have been using Emacs for 6 years now and for the last couple of years, Emacs has been my primary tool for writing Clojure at Helpshift Inc. Over the years, I have realized the value of Emacs. It has made me productive at work and org-mode has certainly made me more disciplined. The idea of having an editor which you can customize as it runs is itself amazing! :)</p><p>Unfortunately, I have also realized that getting on-boarded on Emacs is not an easy path. Many people try it for a few days and go back to their IDEs. So I’m going to try and write about customizing Emacs from scratch. My goal is to make this process simpler (or at least well documented) and help people get comfortable with Emacs until the point they realize its true value! :)</p><h3>Installation and interactive tutorial</h3><p>I’m going to use Emacs 26.1 on macOS. You can get it here for <a href="https://emacsformacosx.com/"><em>macOS,</em></a><em> </em><a href="http://ubuntuhandbook.org/index.php/2019/02/install-gnu-emacs-26-1-ubuntu-18-04-16-04-18-10/">Linux</a> or for <a href="https://www.thecoderworld.com/ide/how-to-install-emacs-text-editor-on-windows-10/">Windows</a>.<br><em>(The version doesn’t really matter. Most of the things written here will work for any Emacs version.)</em></p><p>Once you start Emacs, you will see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fv-ceMnQ6AHnhH0HWK3SRg.png" /><figcaption>Startup screen</figcaption></figure><p>I would advise everyone to go through the “Emacs Tutorial” section displayed above. The Emacs way of doing things is a bit different and this interactive tutorial does a great job explaining it. It is a bit long but you won’t regret spending time on it.</p><p>But before you go through the tutorial, please consider mapping the Caps Lock key to control. Control is used a lot in Emacs, and if you don’t use Caps Lock as control, you’ll suffer from <a href="http://onlineslangdictionary.com/meaning-definition-of/emacs-pinky"><em>Emacs pinky</em></a>. Here’s how to do it for <a href="http://xahlee.info/kbd/osx_swapping_modifier_keys.html">macOS</a>, <a href="https://unix.stackexchange.com/questions/114022/map-caps-lock-to-control-in-linux-mint">Linux</a>, and <a href="https://superuser.com/questions/949385/map-capslock-to-control-in-windows-10">Windows</a>.</p><p>If you don’t want to read this (you really should though!), here’s the gist of the tutorial:</p><h4>Tutorial Summary</h4><p><em>(If you have followed the interactive tutorial, skip this section and go to the </em><strong><em>Customizing defaults</em></strong><em> section)</em></p><ul><li>Emacs uses the control and meta (alt/option) keys a lot. The key bindings with control and meta will be described as C-&lt;key&gt; and M-&lt;key&gt; respectively.</li><li>Press C-x C-c (C-x and then C-c)to exit Emacs.</li><li>C-g will quit the current operation. So if you are stuck and you don’t know what do to, press C-g.</li><li>The cursor location is called “point” in Emacs.</li><li>Use C-f for moving point forward, C-b for backward, C-n for going to next line and C-p for previous line.</li><li>C-a for going to beginning of current line. C-e for end of current line.</li><li>C-v for going to next page. M-v for previous page.</li><li>The above key bindings when used with meta will work on a word level abstractions. For example, M-f will move the cursor one word ahead (vs one character with C-f). M-a, M-e will move between sentences.</li><li>Files are opened in “buffers” in Emacs. There could be a buffer which is not associated with any file. But a file will always have a buffer associated with it when you open it in Emacs.</li><li>M-&lt; (Meta + Less than) will take you to beginning of a buffer and M-&gt; (Meta + Greater than) will take you to end of a buffer.</li><li>Press C-x k &lt;RET&gt; to kill a buffer.</li><li>Press C-x 3 to split the Emacs window horizontally. Press C-x 1 to make the split screen go away.</li><li>Press C-x C-f to open a file.</li><li>C-x C-s to save your edits.</li><li>When you open a few file, the previous buffer still remains active. To go to the previous buffer, press C-x b and type the buffer name (file name) and press &lt;RET&gt;.</li><li>To see the list of buffers, press C-x C-b. Press q to quit this window.</li><li>The term “Windows” in Emacs is not the same as the one used by your OS. In Emacs, the OS “windows” are called “frames”. The split screens in Emacs are called “windows”. For example, when you split your screen, you can say that you have 2 “windows”.</li><li>Use C-x o to move among windows.</li><li>C-d will delete a character in backward direction.</li><li>C-k will delete text from point to end of current line.</li><li>To search for text, press C-s and type the text to be searched for. Pressing C-s after that will take you to the next occurence of the text. C-s moves in the forward direction and C-r moves in the backward direction. Both have a wrapping behavior. Press RET to stop search and move the point to the current occurence.</li><li>If your point is on some text you want to search for, press C-s w to fill the search minibuffer with text under point.</li><li>You can use prefix arguments to repeat commands. Press C-u to give a prefix argument to a command. For example, to go 10 pages ahead, you can press C-u 10 C-v. Or to add a line of 72 *s, press C-u 70 *.</li><li>Instead of dragging to select text, press C-&lt;SPC&gt; (Control + Space) where you want to start dragging and then move the point to where you want to end. Text you have selected will be marked with a highlight. Press C-w to cut or M-w to copy.</li><li>To paste, press C-y. Pasting is called “yanking” in Emacs terms. “Cutting” is called “killing”.</li><li>You can also yank text that was killed previously, press C-y and then keep pressing M-y. You will go on seeing your kill history. This is because Emacs saves the text you kill in a ring data structure called “kill ring”.</li><li>Press C-/ to undo. Undo in Emacs is very different from other editors. <em>(You can read about it in the Tutorial; or just play with it to know how it works.)</em></li><li><strong>Emacs has a concept of modes. For any buffer, there is one major mode and there could be multiple minor modes. For example, if you open a python source code file, the major will be python-mode. Emacs will try to detect and use a proper major mode for any file that you open. Minor modes are modes that do small tasks such as auto indenting, spell checking, showing line numbers, etc. So more than one minor mode can be enabled per buffer.</strong></li><li>The line that you see in the lower part of Emacs:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DdFCF87vYrfgkomIZ_7sAg.png" /><figcaption>Mode line</figcaption></figure><ul><li>It is called “mode line” and it displays useful information about the current buffer and the status of Emacs. For example, it displays which is the major mode that is currently enabled. In the above image, it is “Fundamental” mode.</li></ul><h3>Customizing defaults</h3><p>Now that we have completed the interactive Emacs tutorial, we don’t want Emacs to show the tutorial message when Emacs starts. When Emacs starts up, it looks for one of these 2 files: ~/.emacs.d/init.el or ~/.emacs. If it finds one of these, it will start executing the code in these files. The code in these files is supposed to be Elisp code. Emacs Lisp or Elisp is the language created for and used by Emacs.</p><p>So now we will go ahead and create this file.</p><ul><li>Press C-x f and write .emacs.d/init.el (~/ will already be there) and press &lt;RET&gt;. A new buffer will open up.</li><li>Write these contents in that buffer: (setq inhibit-startup-message t) and save (C-x C-s).</li><li>Quit Emacs and restart.</li><li>Emacs will not show the tutorial screen and instead it will start with a buffer named *scratch*. By convention, buffers which have names starting and ending with a * do not have any files associated with them.</li></ul><p>Congratulations on your first Emacs customization! :)</p><p>Now let’s try to read and understand the Elisp code that we just wrote.</p><pre>(setq inhibit-startup-message t)</pre><p>In simple terms, setq is a function which sets value of the given variable to the given value. In this case, it will set inhibit-startup-message to t.</p><p>Now is a good time to use the self documenting feature of Emacs. setq is a function and we can get help on functions by pressing C-h f( C-h for help and f for functions). Press C-h f, type setq and press &lt;RET&gt;. A window will open up a buffer named *Help*(again, not associated with a file) which will contain information about the setq function. Press C-x 1 to maximize *scratch* again.</p><p>Now let’s see what inhibit-startup-message is. Since it is a variable, we will ask Emacs to tell us more about variables by pressing C-h v. Press C-h v, type inhibit-startup-message and press RET. Notice that for variables, their current value is also shown. This time to quit the documentation window, switch to it using C-x o and then press q.</p><p>Why was this help window quit when we pressed q? The *scratch* window not get quit on pressing q, it will just insert the letter q. Let’s find out why!</p><p>In Emacs, each key is bound to a function and there is a way to figure out which key is bound to which function. And these bindings are per buffer. So q must be bound to two different things as we see two different behaviors on pressing q.</p><p>Switch to the help buffer by pressing C-x b *Help* &lt;RET&gt;. Now let’s find out what q is bound to. Press C-h k and press q. “q runs the command quit-window”. Now we know why q is making the window go away; it was executing the function quit-window.</p><p>Let’s figure out what q is bound to in the *scratch* buffer. Quit the help window by pressing, guess what, q! You’ll be back on the *scratch* buffer. Now press C-h k and then q. “q runs the command self-insert-command”. self-insert-command just inserts the character that you have typed. So all the printable characters on the keyboard must be bound to “self-insert-command”. You can try verifying this. Also, you can read documentation for “self-insert-command” just like the way we read the documentation for setq.</p><p>Now we fully understand why q was acting differently in different buffers. And guess what, <strong>we did not leave Emacs even once!</strong> :)<br><em>(Something fun to try on your own: Find out what </em><em>C-h k is bound to.)</em></p><p>Since we like to use the keyboard for doing all our tasks, the tool bar is not very useful for us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*n1Odj3hdsp7B8tVfYf9WVg.png" /><figcaption>Tool bar</figcaption></figure><p>So let’s disable it. Go to your init.el buffer and write (tool-bar-mode -1) on a new line. Now let’s make this code take effect without restarting Emacs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/805/1*nC9lUKQab7dYQB1x2pYzJg.png" /><figcaption>init.el</figcaption></figure><p>Execute this code by going one character after the closing parenthesis. Press C-x C-e to execute this code. The tool bar is gone!<br>Congratulations on your first live Emacs customization! :)<br><em>(Now you should find out what </em><em>C-x C-e is bound to. It is a good habit to always check which functions you are using instead of just remembering key bindings.)</em></p><p>Now let us disable the menu bar and the scroll bar as well. Put this code in a new line in ini.el:</p><pre>(menu-bar-mode -1)<br>(scroll-bar-mode -1)</pre><p>To clearly see which line you are on, you can have current line highlighted with this code: (global-hl-line-mode t). You will need to execute this line as we did before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/678/1*dxvRS53yWa8KZco1VQNBMA.png" /><figcaption>global-hl-line-mode</figcaption></figure><p>Another important thing which is missing is knowing the line numbers and column numbers. For that, add (line-number-mode t). You’ll see (line #, column #) coordinates of the point location in the mode line. To go to a particular line, press M-g M-g, enter the line number and press &lt;RET&gt;.</p><p><em>[If you use macOS, it is easier to use the “command” keys as “meta”. Add this line to make this happen: </em><em>(setq mac-command-modifier &#39;meta).]</em></p><p>What we have in the last 4 lines of Elisp above is just changing minor modes. We have disabled minor modes which display the tool bar, the menu bar and the scroll bar; we have enabled a minor mode which highlights the current line in the buffer. If you want more information on the currently enabled major and minor modes, press C-h m. On pressing this, a minibuffer will open up. Amogst other things, this buffer will describe commonly required key bindings for the respective modes.</p><p>Now that we have seen how to configure Emacs defaults to suit your needs, you can read the “<a href="https://github.com/suvratapte/dot-emacs-dot-d/blob/e7f44d981004b44bb9ae23b6b67c421404ea6b4e/init.el#L19">Use better defaults</a>” section of my Emacs config and take the things that would be useful to you.<br><em>[Linux and macOS users: Please go through the “</em><a href="https://github.com/suvratapte/dot-emacs-dot-d/blob/e7f44d981004b44bb9ae23b6b67c421404ea6b4e/init.el#L94"><em>Better interaction with X clipboard</em></a><em>”.]</em></p><p>Finally, let’s have a habit of writing comments for all the code that we have written. Our init.el will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dbd6d07e8e1dc9e03bfe8134f8bf66bd/href">https://medium.com/media/dbd6d07e8e1dc9e03bfe8134f8bf66bd/href</a></iframe><h3>Conclusion</h3><p>Throughout this session, we have used C-h &lt;some key&gt; many times. C-h is the prefix key for getting help in Emacs. We have used it quite well here and you should continue following this pattern of using help to know more about Emacs! :)<br>Knowing how to use the help system is crucial for mastering Emacs!</p><h4>Next: <a href="https://medium.com/@suvratapte/configuring-emacs-from-scratch-packages-220bbc5e55b7">Installing and configuring new packages</a></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3157bed9d040" width="1" height="1" alt=""><hr><p><a href="https://medium.com/helpshift-engineering/configuring-emacs-from-scratch-intro-3157bed9d040">Configuring Emacs from Scratch — Intro</a> was originally published in <a href="https://medium.com/helpshift-engineering">helpshift-engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>