<?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 Nethum Lamahewage on Medium]]></title>
        <description><![CDATA[Stories by Nethum Lamahewage on Medium]]></description>
        <link>https://medium.com/@nethumlamahewage?source=rss-544be9921d95------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6hgSxeG0RpFH74SMSjE0Rw.jpeg</url>
            <title>Stories by Nethum Lamahewage on Medium</title>
            <link>https://medium.com/@nethumlamahewage?source=rss-544be9921d95------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 18:13:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nethumlamahewage/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[Simultaneous keybindings with Karabiner Elements]]></title>
            <link>https://nethumlamahewage.medium.com/simultaneous-keybindings-with-karabiner-elements-ddeb3c4a6eaa?source=rss-544be9921d95------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddeb3c4a6eaa</guid>
            <category><![CDATA[dotfiles]]></category>
            <category><![CDATA[karabiner-elements]]></category>
            <dc:creator><![CDATA[Nethum Lamahewage]]></dc:creator>
            <pubDate>Thu, 29 Jun 2023 00:00:37 GMT</pubDate>
            <atom:updated>2023-06-29T17:57:01.723Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XXVZV6hlS53JH2vi" /><figcaption>Photo by <a href="https://unsplash.com/pt-br/@floriankrumm?utm_source=medium&amp;utm_medium=referral">Florian Krumm</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>This will be a short post. I just wanted to talk about my simultaneous keybindings with Karabiner Elements. They’ve been really useful for me, and I use them constantly.</p><p><a href="https://karabiner-elements.pqrs.org/">Karabiner-Elements</a></p><p>I feel like I also need to clarify what exactly I mean by “simultaneous keybindings”. When I press two keys on the keyboard, I want my computer to interpret that as <strong>one</strong> specific key (e.g. pressing a and b together is interpreted as c). I am <strong>not</strong> talking about pressing one key on the keyboard and having the computer interpret that as pressing two keys. I&#39;m making this distinction because whenever I searched about this, I usually came across articles and questions about the second case.</p><h3>Why simultaneous keybindings?</h3><p>I’m not really sure how this started, but I think it’s when I heard about how some Vim users configure j+k to the escape key, because they use the escape key quite often to go back to normal mode in Vim.</p><p>In <a href="https://vim.fandom.com/wiki/Avoid_the_escape_key#Alternative_mappings">this page</a> from the Vim Tips Wiki on the Fandom website, they talk about different ways to avoid pressing the actual escape key. One of the ways they suggest doing this is to map jj (pressing j twice in quick succession) to escape. As an alternative, they propose using jk instead, as it would be faster to type than jj.</p><p>At some point, I really took this to heart and added it to my Karabiner Elements configuration so that pressing j and k at the same time produced Escape. After using that for a while, and seeing just how convenient it is, I looked into what other keys I could do that with, and I eventually added enter, backspace, and delete to the simultaneous keybindings. The configuration in the “How?” section is my real set of simultaneous keybindings. Of those, the only one that I don&#39;t use constantly is the one for delete_forward and that&#39;s only because I don&#39;t usually need to delete forward.</p><p>I use those keybindings constantly. Escape is of course useful since I use Vim keybindings a lot. Enter and backspace should be self-explanatory. It’s really nice to be able to use them without even having to move my fingers away from the home row. Those keybindings have had a great effect on my typing, making it more fluid.</p><p>Also, in my config in the “How?” section, you’ll notice that I mapped hk to C-x. That&#39;s because C-x is my prefix key in <a href="https://github.com/tmux/tmux">tmux</a>.</p><h3>How?</h3><p>I currently use <a href="https://github.com/yqrashawn/GokuRakuJoudo">GokuRakuJoudo</a> to manage my Karabiner Elements configuration.</p><p>With Goku, the “complex modification” is simply this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8624562519060c73e2a1cee692932b32/href">https://medium.com/media/8624562519060c73e2a1cee692932b32/href</a></iframe><p>For completeness’ sake, this is a minimal but complete Goku config with just the simultaneous keybindings.</p><pre>{<br>  :profiles<br>    {:Main { :default true :sim 30 :delay 500 :alone 500 :held 500}}<br>  :main [<br>    {:des &quot;Simultaneous keys&quot;<br>      :rules [:Main<br>        [{:sim [:j :k] :modi {:optional [:any] }} :escape]<br>        [{:sim [:k :l] :modi {:optional [:any] }} :return_or_enter]<br>        [{:sim [:j :l] :modi {:optional [:any] }} :delete_or_backspace]<br>        [{:sim [:h :l] :modi {:optional [:any] }} :delete_forward]<br>        [{:sim [:h :k] } :!Tx]<br>      ]}<br>  ]<br>}</pre><p>Here’s the “complex modification” in the generated Karabiner JSON config for mapping jk and kl.</p><pre>{<br>  &quot;description&quot;: &quot;Simultaneous keys&quot;,<br>  &quot;manipulators&quot;: [<br>    {<br>      &quot;from&quot;: {<br>        &quot;modifiers&quot;: {<br>          &quot;optional&quot;: [&quot;any&quot;]<br>        },<br>        &quot;simultaneous&quot;: [<br>          {<br>            &quot;key_code&quot;: &quot;j&quot;<br>          },<br>          {<br>            &quot;key_code&quot;: &quot;k&quot;<br>          }<br>        ],<br>        &quot;simultaneous_options&quot;: {<br>          &quot;detect_key_down_uninterruptedly&quot;: false,<br>          &quot;key_down_order&quot;: &quot;insensitive&quot;,<br>          &quot;key_up_order&quot;: &quot;insensitive&quot;,<br>          &quot;key_up_when&quot;: &quot;any&quot;<br>        }<br>      },<br>      &quot;to&quot;: [<br>        {<br>          &quot;key_code&quot;: &quot;escape&quot;<br>        }<br>      ],<br>      &quot;type&quot;: &quot;basic&quot;<br>    },<br>    {<br>      &quot;from&quot;: {<br>        &quot;modifiers&quot;: {<br>          &quot;optional&quot;: [&quot;any&quot;]<br>        },<br>        &quot;simultaneous&quot;: [<br>          {<br>            &quot;key_code&quot;: &quot;k&quot;<br>          },<br>          {<br>            &quot;key_code&quot;: &quot;l&quot;<br>          }<br>        ],<br>        &quot;simultaneous_options&quot;: {<br>          &quot;detect_key_down_uninterruptedly&quot;: false,<br>          &quot;key_down_order&quot;: &quot;insensitive&quot;,<br>          &quot;key_up_order&quot;: &quot;insensitive&quot;,<br>          &quot;key_up_when&quot;: &quot;any&quot;<br>        }<br>      },<br>      &quot;to&quot;: [<br>        {<br>          &quot;key_code&quot;: &quot;return_or_enter&quot;<br>        }<br>      ],<br>      &quot;type&quot;: &quot;basic&quot;<br>    }<br>  ]<br>}</pre><h3>Improvements and limitations</h3><p>This method of doing simultaneous keybindings of course only works on macOS. Right now, I’m almost exclusively using macOS, so that’s fine for me, but there have been times that I’ve had to use Windows. I’ve tried to find a good way of doing it on Windows, but I couldn’t find one. If you know a method to do it, let me know. As for Linux, I have no idea. There’s almost definitely a way to do it on Linux, but since I don’t daily-drive it right now, I haven’t looked into it too much. I’ve also looked at <a href="https://github.com/kmonad/kmonad">KMonad</a>, but I haven’t really had time to try it out. If someone has, please let me know. I would like to have my keyboard config be cross-platform.</p><p>Also, please don’t simply tell me to use <a href="https://www.autohotkey.com/">AutoHotkey</a> to do this on Windows. I know it exists, and it worked for other stuff, but I couldn’t get a good simultaneous keybinding working on that. However, if you know of a specific way to do it in AutoHotkey, please do let me know.</p><p>Those 5 simultaneous keybindings are the only ones I’ve found to be useful. I would like to do the same with similar special keys, but I haven’t thought of any others that I would actually use.</p><p>And finally, I know simultaneous keybindings aren’t perfect. I still sometimes find myself failing to press both keys at the same time and instead pressing them one after the other and having to backspace the two letters I accidentally typed. This does depend on the keyboard, though. Mechanical keyboards seem to make it easier.</p><h3>Conclusion</h3><p>There’s not much of a conclusion for this. I’ve configured simultaneous keybindings on my computer with Karabiner Elements, and I use those keybindings constantly. That’s pretty much it.</p><p><em>Originally published at </em><a href="https://nethuml.github.io/posts/2023/06/simultaneous-keybindings-with-karabiner-elements/"><em>https://nethuml.github.io</em></a><em> on June 29, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddeb3c4a6eaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating spellchecking into a QTextEdit widget in PyQt5 with enchant]]></title>
            <link>https://nethumlamahewage.medium.com/integrating-spellchecking-into-a-qtextedit-widget-in-pyqt5-with-enchant-f025f4097e5c?source=rss-544be9921d95------2</link>
            <guid isPermaLink="false">https://medium.com/p/f025f4097e5c</guid>
            <category><![CDATA[spell-checker]]></category>
            <category><![CDATA[pyqt5]]></category>
            <dc:creator><![CDATA[Nethum Lamahewage]]></dc:creator>
            <pubDate>Thu, 19 Aug 2021 18:54:44 GMT</pubDate>
            <atom:updated>2021-08-19T18:54:44.736Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*o61Px23czzT02ciF" /><figcaption>Photo by <a href="https://unsplash.com/@cdr6934?utm_source=medium&amp;utm_medium=referral">Chris Ried</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>I was working on a project using PyQt5 when I found myself in need of spellchecking in a QTextEdit widget. I tried to find a reasonable implementation of it, but I didn&#39;t find one. Maybe it exists somewhere out there, but, it was an interesting little side project, so I decided to try making it myself. If you just want the code, you can get it from the GitHub repository.<br>If you are interested in an explanation of the code, read on.<br>First, a little introduction to what will be used for this.</p><h4>PyQt5</h4><p><a href="https://www.riverbankcomputing.com/software/pyqt">Riverbank Computing | Introduction</a></p><p>This is a set of Python bindings for the cross-platform <a href="https://www.qt.io/">Qt framework</a>. There is another set of bindings called <a href="https://wiki.qt.io/Qt_for_Python">PySide2</a>. I won&#39;t go into the differences between them here, but all of the code here should work fine with PySide2 as well, with only the relevant import statements needing to be changed.<br>You can install it using pip install PyQt5.</p><h4>enchant</h4><p><a href="https://github.com/AbiWord/enchant">GitHub - AbiWord/enchant: enchant spellchecking library</a></p><p>This is a spellchecking library written in C and C++. There are other spellchecking libraries of course, but I chose this as it seemed to work better than the others I tried. If you want to use a different library for the spellchecking, you can do that by simple replacing the implementation of the wrapper in the next section. You can install the library from the <a href="https://github.com/AbiWord/enchant">GitHub repository</a>, and you can install the Python bindings using pip install pyenchant.</p><h3>Wrapping enchant</h3><p>While this part isn’t necessary, I initially did this in case I needed to change the library doing the spellchecking without needing to change the rest of the code. This wrapper will provide an interface to get a list of suggestions given a word, add a word to the personal word list, and to check a particular word’s spelling. The code itself should be pretty self-explanatory, but I have added some comments as additional explanation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1c829ef508a6f9b047b1243700f20ae/href">https://medium.com/media/c1c829ef508a6f9b047b1243700f20ae/href</a></iframe><h3>Custom QSyntaxHighlighter</h3><p><a href="https://doc.qt.io/qt-5/qsyntaxhighlighter.html">QSyntaxHighlighter Class</a></p><p>Qt conveniently has a QSyntaxHighlighter class which we can use to show any words that are misspelled. Here, we will subclass it to use a SpellCheckWrapper instance to check the spellings of all the words, and show the usual red line under any misspelled words.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f575535c0bac27e5d0f5a06525a5733f/href">https://medium.com/media/f575535c0bac27e5d0f5a06525a5733f/href</a></iframe><h3>Correction action</h3><p><a href="https://doc.qt.io/qt-5/qaction.html">QAction Class</a></p><p>Now we need to create a simple <a href="https://doc.qt.io/qt-5/qaction.html">QAction</a> that will fire a custom signal when clicked, and pass its text as an argument. We will be using this to create the list of suggested words in the context menu.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb3035b16057fdfe161ff3898f2700dc/href">https://medium.com/media/cb3035b16057fdfe161ff3898f2700dc/href</a></iframe><h3>Subclassing QTextEdit</h3><p>Now we come to the main part of this article. I will break down this class into a few sections. The first one is for the imports and the constructor. Each one after that will be for the other methods in the class.</p><h4>Imports and constructor</h4><p>This part should be self-explanatory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c222c01671db7ac4fb098ebf73c08f6/href">https://medium.com/media/4c222c01671db7ac4fb098ebf73c08f6/href</a></iframe><h4>Set speller</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e173510b39a313668db1deb9b471fc85/href">https://medium.com/media/e173510b39a313668db1deb9b471fc85/href</a></iframe><h4>Mouse press event</h4><p>This is a little hack to make it so that right-clicking will move the text cursor to the mouse position. If the mouse press event is a right click, then we change that into a left click.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbf92c661310940fb82e5f385d27e78d/href">https://medium.com/media/fbf92c661310940fb82e5f385d27e78d/href</a></iframe><h4>Context menu event</h4><p>Here, we need to build the context menu. First, we can use the built-in createStandardContextMenu method to make the basics. Then, we add on the list of suggestions, and a button to add to dictionary.<br>createSuggestionsMenu is explained in the next section.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b42edb3f8ecdadf80eb592071b115911/href">https://medium.com/media/b42edb3f8ecdadf80eb592071b115911/href</a></iframe><h4>Create suggestions menu</h4><p>We create the suggestions menu from the given list of suggestions. Here, we use that SpecialAction from before. When one is clicked, the correctWord method is called.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac3b05a3e8ef2e7aea2f59e95cc19733/href">https://medium.com/media/ac3b05a3e8ef2e7aea2f59e95cc19733/href</a></iframe><h4>Replace the selected word with the given correction</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16606fc5a1c050d6d5b617a5bdc04a34/href">https://medium.com/media/16606fc5a1c050d6d5b617a5bdc04a34/href</a></iframe><h4>Add to dictionary</h4><p>This adds the selected word to the dictionary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c46bffa23b8b0c2f056d2ba7ba617d1/href">https://medium.com/media/1c46bffa23b8b0c2f056d2ba7ba617d1/href</a></iframe><h3>Conclusion</h3><p>There you have it. If you want to try it out, I have included a small <a href="https://github.com/NethumL/pyqt-spellcheck/blob/main/src/example.py">example application</a> in the GitHub repository.<br>There are of course some improvements to be made. For example, with the current implementation, if a user selects some text and right-clicks, the selection will change to the word under the pointer. As I wrote this code for use in another project of mine, this basic functionality was enough.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f025f4097e5c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up a global leader key for macOS using Hammerspoon]]></title>
            <link>https://nethumlamahewage.medium.com/setting-up-a-global-leader-key-for-macos-using-hammerspoon-f0330f8a7a4a?source=rss-544be9921d95------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0330f8a7a4a</guid>
            <category><![CDATA[hammerspoon]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[macos]]></category>
            <dc:creator><![CDATA[Nethum Lamahewage]]></dc:creator>
            <pubDate>Mon, 10 May 2021 19:15:32 GMT</pubDate>
            <atom:updated>2021-05-10T19:15:32.417Z</atom:updated>
            <content:encoded><![CDATA[<h4>Make your keyboard your own</h4><h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0I3xY8Za5PxzuUix" /><figcaption>Photo by <a href="https://unsplash.com/@aguynamedlukas?utm_source=medium&amp;utm_medium=referral">Lukas Hellebrand</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I&#39;ve been using macOS for a little over a year now. Before that, I had only ever known Windows. Once I moved to macOS, it took quite some time to get used to it. I do still occasionally boot into Windows (using Bootcamp), but it&#39;s only when I need to use some software that doesn&#39;t run on macOS.</p><p>At first, I used macOS much like I used to use Windows. However, since I’m doing a CS degree, I needed to get familiar with programming related tools, and that’s how I found Vim. I found it interesting and gave it a shot. I won&#39;t go into what that was like here, as there are countless people on the internet who have told their stories of learning Vim. Eventually, I gained an interest in configuring my system to suit me better and match my workflow, and along came Hammerspoon.</p><h3>Hammerspoon</h3><p><a href="https://www.hammerspoon.org/">Hammerspoon</a></p><h4>What is Hammerspoon?</h4><p>Simply put, it is an automation tool running on Lua. It has numerous APIs for macOS functionality, and with it, you can control pretty much whatever you want to. Basically, you&#39;re only limited by your imagination (and your skill at writing the relevant code).</p><p>If you’re already familiar with Hammerspoon, you can skip this section.</p><h4>Installing</h4><p>Install using either <a href="https://brew.sh/">Homebrew</a> with the command brew install hammerspoon or download from their <a href="https://github.com/Hammerspoon/hammerspoon/">GitHub repository</a>.</p><h4>Configuration</h4><ul><li>Create a ~/.hammerspoon folder and create a file init.lua in it</li><li>This is the starting point of your Hammerspoon config. You can split your config into separate files (and you should, if you do any serious configuring), and then use the require function to import them</li><li>To configure Hammerspoon, a basic knowledge of lua is required to start with. If you’re already familiar with any other programming languages, then a quick run through of the basics of lua and the syntax should be enough, as it is easy to get started with lua. <a href="https://learnxinyminutes.com/docs/lua/">This</a> tutorial should suffice for that. For those not familiar with programming at all, Lua shouldn’t be too difficult to learn (at least to learn what you will need to know to follow along with the rest of this article).</li></ul><p>Some basic configuration</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d953bc0dd410e478b9109c8f35c7f0/href">https://medium.com/media/e6d953bc0dd410e478b9109c8f35c7f0/href</a></iframe><p>Here, hs is a globally available object, through which you access the Hammerspoon API. hs.hotkey.bind (<a href="https://www.hammerspoon.org/docs/hs.hotkey.html#bind">docs</a>) takes a table of modifiers, a key, and a function to call when that key binding is pressed.</p><p>Click on the Hammerspoon icon on the menubar and click on “Reload config”. After that, you can use the shortcut ⌘+⌥+^+h to reload the Hammerspoon config (here, alt means option).</p><p>Now, when the Wi-Fi network you’re connected to changes, you will receive a notification from Hammerspoon.</p><p>More information:</p><p>You can go to Hammerspoon&#39;s <a href="https://www.hammerspoon.org/go/">Getting Started Guide</a> to see some more examples. And if you want to know more about any part of the API, you will find that it is very well <a href="https://www.hammerspoon.org/docs/index.html">documented</a>.</p><p>You can also find &#39;Spoons&#39;, which are basically plugins which provide additional functionality, at <a href="https://www.hammerspoon.org/Spoons/">this</a> link. And you can find many resources out there about setting up various things using Hammerspoon.<br>Now let&#39;s jump into the RecursiveBinder Spoon.</p><h3>RecursiveBinder Spoon</h3><p><a href="https://www.hammerspoon.org/Spoons/RecursiveBinder.html">Hammerspoon docs: RecursiveBinder</a></p><h4>About</h4><p>When I first started configuring Hammerspoon, I set up a hyper key, and added a few key bindings. But I soon hit a roadblock where I was using up all the keys (or at least the most easily accessible ones)<br>It wasn&#39;t that long since I had been introduced to Vim, and I was getting comfortable with the leader key system. It turned out that there was a Spoon for Hammerspoon called RecursiveBinder that could do the same thing.</p><p>After setting it up and configuring it, this is what my main RecursiveBinder helper text looks like today:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RtT-gN99P_-7WK0tow4-IA.png" /><figcaption>My current helper text</figcaption></figure><p>As you might guess, there are some levels of nesting in there.</p><h4>Installing</h4><p>The spoon can be downloaded from <a href="https://www.hammerspoon.org/Spoons/RecursiveBinder.html">here</a>.</p><p>Copy it into ~/.hammerspoon/Spoons. The ~/.hammerspoon directory should now look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*i_6viTZhBksLUrNGgoRgVw.png" /><figcaption>~/.hammerspoon directory structure</figcaption></figure><h4>Configuring</h4><p>singleKey is a convenience function used to easily create a table representing a key binding with no modifiers, and also automatically translate capital letters to normal letters with shift modifier. For example, singleKey(&#39;o&#39;, &#39;open&#39;) returns {{}, &#39;o&#39;, &#39;open&#39;}, and singleKey(&#39;O&#39;, &#39;open&#39;) returns {{&#39;shift&#39;}, &#39;o&#39;, &#39;open&#39;}</p><p>Here is a simple config for RecursiveBinder</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf814f5ca5c8da6c93deb2ae23e128eb/href">https://medium.com/media/bf814f5ca5c8da6c93deb2ae23e128eb/href</a></iframe><ul><li>First load the spoon using hs.loadSpoon (<a href="https://www.hammerspoon.org/docs/hs.html#loadSpoon">docs</a>)</li><li>RecursiveBinder.escapeKey is the key binding used to abort</li><li>The next line is just for convenience, so that I can use singleKey without having to type out spoon.RecursiveBinder.singleKey every time</li><li>Next, I create a table of key bindings. The first two are to open the browser and the terminal respectively, and the next set is a nested group.</li><li>Pressing ⌥+space will trigger RecursiveBinder. Helper text will pop up at the bottom of your screen with the ‘browser’, ‘terminal’, and ‘domain+’ key bindings</li><li>Pressing ‘b’ or ‘t’ will call the functions provided, and open Firefox and Terminal respectively (and also dismiss the helper text)</li><li>Pressing ‘d’ will enter the next layer, and the helper will change to show the ‘g’ and ‘y’ key bindings, and pressing one of those will call the corresponding functions, and open GitHub and YouTube respectively (in your default browser)</li></ul><p>The helper text can be styled as well</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ff8b4c3b72b3e108955e55072c2f453/href">https://medium.com/media/1ff8b4c3b72b3e108955e55072c2f453/href</a></iframe><p>Refer to the hs.alert.defaultStyle <a href="https://www.hammerspoon.org/docs/hs.alert.html#defaultStyle">documentation</a> for general styling, and hs.styledtext (<a href="https://www.hammerspoon.org/docs/hs.styledtext.html">docs</a>) for text styling</p><h3>Leader key</h3><p>Finally, we have reached the main part of this article.</p><h4>Loading from config.json</h4><ul><li>To make later configuration simpler, I set it up so that it loads as much of the config as possible from an easily editable JSON file, using hs.json (<a href="https://www.hammerspoon.org/docs/hs.json.html">docs</a>).</li><li>The config.json file is in the private folder, which is where personal aspects of the config are stored. This way, you can separate those from the main configuration (especially useful if you want to upload your Hammerspoon config somewhere, without uploading your personal preferences for that config).</li></ul><pre>local config = hs.json.read(&quot;private/config.json&quot;)</pre><p>This is the format of the config.json for the Hammerspoon config I will be explaining. You can add more to the lists of applications, domains, and notes according to your needs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97d3f897cdad78de0efeae3f3ec0a1b7/href">https://medium.com/media/97d3f897cdad78de0efeae3f3ec0a1b7/href</a></iframe><p>If your config.json is getting too big, it might be a good idea to convert it into a different file type, such as YAML (as it is easier to read/write). I’ll leave that as an exercise for the reader (partly because I haven’t done that yet either, though I do intend to). As a starting point, you may want to look into <a href="https://github.com/gvvaughan/lyaml">this</a>.</p><h4>Applications &amp; Domains key maps</h4><ul><li>Here, I&#39;m iterating through the list of applications in my config, and adding them to the key map one by one. For this, I can use a function in Hammerspoon called hs.fnutils.each(<a href="https://www.hammerspoon.org/docs/hs.fnutils.html#each">docs</a>). It takes in a table and a function, which will be called for each element in the table</li><li>For each application, I&#39;m assigning the corresponding key and a function that will launch it using Hammerspoon&#39;s hs.application.launchOrFocusByBundleID (<a href="https://www.hammerspoon.org/docs/hs.application.html#launchOrFocusByBundleID">docs</a>) (you can use hs.application.launchOrFocus instead, if you prefer)</li><li>If you want to find the bundle ID of an application, the following line of AppleScript will return it: id of app &#39;Firefox&#39; (just replace Firefox with the application name, as it appears in your Applications folder). You can also run this in a shell like this:</li></ul><pre>osascript -e &quot;id of app &#39;Firefox&#39;&quot;</pre><p>If you’re wondering what AppleScript is, <a href="https://macosxautomation.com/applescript/firsttutorial/index.html">here</a>.</p><p>The following lua code will add the applications and the domains to two separate lua tables. The code to build the key map for domains is structurally similar, as you can see.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb0259dc5aa0760dc0bc61458e73d030/href">https://medium.com/media/bb0259dc5aa0760dc0bc61458e73d030/href</a></iframe><p>If you looked at the config above, you may have noticed the notes section. I also set up a key map to open those notes in the browser. I think the format of the config is self-explanatory, so let’s dive into the actual lua code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/006e65bd4bb628067632e278d62d8d2a/href">https://medium.com/media/006e65bd4bb628067632e278d62d8d2a/href</a></iframe><p>This one is more complicated, but I&#39;m including it to show you just how much you can achieve with this.<br>Let’s go through it part by part.</p><ul><li>All of my notes are in a folder called notes_html in my $HOME folder (aka ~/), and I&#39;ve categorized some into sub-folders. For example, there is a sub-folder named programming, with separate notes for each programming language (you can skip this section if you don’t have your notes in a similar format).</li><li>generate is a recursive function that is called on the notes section of the config. It iterates over the list provided, and for each element, it does one of two things.</li><li>If it is a sub-folder (a simple way to check this is to check for the contents attribute), then it calls the function again for that folder&#39;s list of entries(files or folders), and assigns it to the corresponding key in the key map</li><li>If it is a file, then it just assigns the corresponding key in the key map and attaches the function to open the note</li><li>For any programmers reading, the idea is similar to a depth first search of a tree</li><li>To open the note, I&#39;m using the hs.urlevent.openURL function. They are all HTML files, so they are automatically opened in my default browser</li><li>While recursively going through the notes, I&#39;m also passing along the current path when calling the function and in the case of a sub-folder appending it to the end of the path</li><li>Now to use this, you don&#39;t really need to understand all of this. Just set all of it in the config.json, making sure to set the correct config.notes.rootPath as well.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7af76dfe7b37e93a9c1de17f1553e4c4/href">https://medium.com/media/7af76dfe7b37e93a9c1de17f1553e4c4/href</a></iframe><p>Here, I&#39;ve also included a couple of key bindings for Hammerspoon. One to reload the config, and the other to open it in VS Code</p><h3>Bonus</h3><ul><li>If you used this, you may have noticed that the order of the keys in the helper text is not consistent. To fix this, I added some more code to sort the helper text before showing.</li><li>The following code is to be added to RecursiveBinder.spoon/init.lua</li><li>Not much needs to change. A function called compareLetters is added, and, the beginning of the for loop(in showHelper) and the part just before it are changed as shown.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e2dd75f742ebedda0a63c147d283441/href">https://medium.com/media/1e2dd75f742ebedda0a63c147d283441/href</a></iframe><p>To cleanly integrate this into RecursiveBinder, much more changes are required, but for now, this works for me.</p><h3>Conclusion</h3><p>OK, time for some closing words.</p><p>I’ve been using Hammerspoon for less than a year, and so far, I am beyond impressed. The power it brings is just amazing, and there’s <a href="https://github.com/ashfinal/awesome-hammerspoon">so much</a> you can do with it. Like I said in the beginning, you are only limited by your imagination. On top of all that, it’s an open source project.</p><p>If you’re looking for ideas on how to improve your Hammerspoon configuration, look at some <a href="https://github.com/Hammerspoon/hammerspoon/wiki/Sample-Configurations">sample configurations</a>. Even just browsing through the Hammerspoon API should give you some ideas of what you might want to do.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0330f8a7a4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>