<?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 Maritza Esparza on Medium]]></title>
        <description><![CDATA[Stories by Maritza Esparza on Medium]]></description>
        <link>https://medium.com/@maritzaesparza05?source=rss-ceaf97b3dce5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bYhU7_SoOlcIxjXP5BFyPg.jpeg</url>
            <title>Stories by Maritza Esparza on Medium</title>
            <link>https://medium.com/@maritzaesparza05?source=rss-ceaf97b3dce5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 09:42:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maritzaesparza05/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[Want to create executable documentation? Doing Literate DevOps? Take a look at Emacs]]></title>
            <link>https://medium.com/@maritzaesparza05/want-to-create-executable-documentation-doing-literate-devops-take-a-look-at-emacs-459fe361af82?source=rss-ceaf97b3dce5------2</link>
            <guid isPermaLink="false">https://medium.com/p/459fe361af82</guid>
            <category><![CDATA[literateprogramming]]></category>
            <category><![CDATA[documentation]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[emacs]]></category>
            <dc:creator><![CDATA[Maritza Esparza]]></dc:creator>
            <pubDate>Thu, 28 Jul 2022 19:52:30 GMT</pubDate>
            <atom:updated>2022-07-28T19:56:06.612Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zqG8Wsl3OIu0lPqb" /><figcaption>Photo by <a href="https://unsplash.com/@pinjasaur?utm_source=medium&amp;utm_medium=referral">Paul Esch-Laurent</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Have you ever needed to run a lot of commands and maybe share them in some way? Perhaps you needed to share them as part of some documentation or a tutorial? Perhaps you needed some kind of “executable documentation”?</p><p>I found myself in exactly this situation recently. Not only did I need to frequently run this documentation, but I also needed to run it with small tweaks each time. In summary, I needed a tool that could do the following:</p><ul><li>Allow me to write blocks of source code (shell commands in my case).</li><li>Be able to run the source blocks in some way (this way I don’t need to copy and paste commands on the command line — hey maybe I’m lazy but there were a lot of commands!).</li><li>Support some kind of variables (the small tweaks between each iteration).</li><li>Allow adding documentation related to the code blocks.</li><li>Be able to export the documentation and source blocks.</li></ul><p>Ok so maybe a provisioning framework… something like <a href="https://www.ansible.com/">ansible</a> or <a href="https://www.chef.io/">chef</a>? They support variables and can run the commands for you. Mmm, but I needed to share the commands with other people who wanted to see them first before giving the approval to run them, and they wanted the “shell” commands. Also, having documentation explaining the process would be nice. I could add some comments around the instructions in any of those frameworks but it’s not the same. I can’t export it as a markdown file or some other format. And most importantly, it seems very overkill for just running a bunch of shell commands.</p><p>I spent my days looking for a tool that could be the best fit without realizing that I was already using the perfect tool for the job: <a href="https://www.gnu.org/software/emacs/">Emacs</a>! My editor 🤯</p><p>Now, I’m not an Emacs expert, I just have used <a href="https://github.com/doomemacs/doomemacs">Doom Emacs</a> for a couple of months. Although I agree that it’s the “swiss army knife” of the code editors, it’s well known that it has a steep learning curve. But hear me out. Let me show what a noob like me can do with Emacs in a couple of days and how it allowed me to create <em>executable documentation</em>.</p><h3>Install an emacs distribution</h3><p>First things first. You’ll need an Emacs distribution. I use Doom Emacs because I love evil mode and the <em>vim</em> language. Yes, for me it is a whole language… if you’re curious I would recommend taking a look at this <a href="https://www.youtube.com/watch?v=wlR5gYd6um0">video</a> or this <a href="https://benmccormick.org/2014/07/02/learning-vim-in-2014-vim-as-language">post</a> that summarizes the ideas on the video.</p><blockquote><em>If you don’t want to switch to a different editor and you prefer the super popular vscode IDE it has an </em><a href="https://marketplace.visualstudio.com/items?itemName=tootone.org-mode&amp;ssr=false#overview"><em>extension</em></a><em> that could be handy to follow this tutorial. I haven’t used it so I can’t guarantee that will work the same but you could read the documentation and try the examples. Still keep reading, this post will give you an idea of the kind of things that you can accomplish.</em></blockquote><p>Ok, do you have your Emacs distribution installed (or extension setup)? I can wait ☕️… Come on you have Google no need to guide you through the installation… Do you have it? Awesome!</p><p>Before getting started, let me make something clear: I can’t explain Emacs in a single article, as I’m not a complete Emacs geek… yet. So I’ll just explain the basics so you can create your executable documentation ✨</p><h3>Org what??</h3><p>First let me talk to you about literate programming, in a nutshell, literate programming combines documentation with code but in a more visible way. Think about it as your source code embedded in a markdown document, obviously with more powerful features. If you have used or heard about Jupyter notebooks before you may have an idea about what I’m talking about. I find this approach suitable for creating guides, doing reproducible research, recording your thoughts while you learn some new technical skill, and in my case for Literate DevOps (setting up an environment while executing the steps and writing documentation for teammates). Emacs supports this style of programming through the Org mode. So let’s take a look at the structure of an org file.</p><p>To start, we need to create a file, you can name it as you wish just make sure that it has the “org” extension.</p><p>We will start easy, adding a header. Emacs headers start with *You need a * for each level in the hierarchy.</p><pre>* Title<br>** Subtitle<br>*** Subtitle of the subtitle</pre><p>And you get the idea, org files are similar to markdown in that they have hierarchical content. You are just replacing the # by *</p><p>Time to add our snippet! They follow this syntax:</p><pre>#+begin_src sh<br>echo &quot;Hello world&quot;<br>#+end_src</pre><blockquote>A tip, in emacs everything has shortcuts. If this seems like a lot to type just use: <strong>&lt;sTAB</strong> and there you have your snippet.</blockquote><p>All the code blocks follow this structure. You first specify the language of the block. A lot of languages are supported, if you’re curious you can find the complete list <a href="https://orgmode.org/worg/org-contrib/babel/languages/index.html">here</a> (make sure that you have the language installed on your machine first).</p><p>You should end with something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*54HX16qHSlWpwxUnem-uFw.png" /></figure><p>Can you see the screenshot? Do you see something cool on it? Yes… it has the results of running the code 🤯 You can get your results by typing <strong>Ctrl-c Ctrl-c</strong> while having your cursor in the snippet.</p><blockquote>Generally to initialize or refresh new org components you run Ctrl-c Ctrl-c while having the cursor over the element, so this will be what I will be doing when running code blocks and setting properties.</blockquote><p>Are you starting to see the possibilities now? You can write the steps in snippets like the one above and execute the code without the need of retyping the commands in a terminal. And by the way, you can use the language you want in each snippet 😎. And we’re just getting started…</p><h3>Source blocks can have arguments?</h3><p>You can tweak your source blocks in all kinds of interesting ways. Here I’ll explain some of the most popular switches that you can use.</p><h4>Variables (:var)</h4><p>As the third criterion for the tool I needed, I mentioned that it would be nice to have some kind of variable. You know, maybe I want to create a specific directory first, and later on, I want to follow the same steps with another directory. Well, variables become handy in this situation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ts7PFCKuBUdmTMmo0gS9GQ.png" /></figure><p>As you can see the variables are defined in the snippet starting with <em>:var</em> followed by all the variables that you would like to define, if you want to define multiple variables you just need to separate the declarations by a space.</p><h4>Where to run the command? (:dir)</h4><p>Another useful switch is <em>:dir</em>, it lets you specify where you want to run the command.</p><p>If you want to run it from my documents directory we can use something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iUJncHvmnBJzvwN8jeL57w.png" /></figure><p>We aren’t limited to directories in your machine, we can also run commands in <strong>other</strong> <strong>hosts:</strong></p><p>#+begin_src sh :dir /ssh:&lt;server address&gt;:&lt;directory&gt;</p><p>Let’s go crazy and imagine that we have some kind of middle (bastion) server through which we have to login in order to access the real server. Well, we can do it just by chaining the servers with the | character:</p><p>#+begin_src sh :dir /ssh:user@bastion-server|ssh:user@real-server:directory</p><blockquote>If you’re curious this syntax is from the <a href="https://www.emacswiki.org/emacs/TrampMode">tramp</a> utility used by Emacs to connect or edit files in remote hosts.</blockquote><p>And if you were curious you can also run commands in docker containers!! 🐳</p><p>#+begin_src sh :dir /docker:user@container:directory</p><h4>Formatting the output (:results)</h4><p>The <em>:results</em> switch can handle the customization of 4 kinds of results:</p><ul><li>What should be the actual result (what was printed, what was returned)</li><li>Which type of result should be returned (a list, a table, or a file)</li><li>The org format used in the results (as another block, raw text, etc.)</li><li>How the results are rendered in the org file (replace existing results, append results, etc.)</li></ul><h4>Specify what is the actual result</h4><p>Now let’s try an example with the python language:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WjQlBedwdQZtEkYjglD6OQ.png" /></figure><p>You can see that the result is “None” the null value in python. So there’s no result? not exactly, this is because by default the <em>:results </em>switch is :results value so the result will be the value returned by the block, and if you think about it, in a print statement weren’t returning anything, we’re just redirecting data to the standard output. For example, the following block works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iIGkmYXmx9qRfmLAB5tY2g.png" /></figure><p>If we want to use print statements we have to specify that we want to consider the results as what was printed in the standard output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J9EUdoUehbjHpkYjhpWbEQ.png" /></figure><h4>Type of results</h4><p>Maybe you have noticed that the result looks like a string, they look like what you would see if you run the command in your command line or REPL. But you have the option to return the results as special types in org-mode. Why is this useful? Well, this may be a spoiler but I will need to change the type of the result when passing the output of one block as the input to another (yep it’s as good as it sounds!)</p><h4>1. Lists</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W0bG1zVfFn8rF_SBxKUMNw.png" /></figure><p>Instead of returning the results as a string, the list type returns the results as an “Org list”. Besides showing a collection of results in an organized way, you can use this kind of output to pass it as input for other code blocks as shown in the following snippet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tHpZRr6EGDRKVDFV6cQXSw.png" /></figure><p>Here you can see how you can give a name to a block. This way we can reference the block in any other block. You can chain blocks by their results and they aren’t constrained to the same language. You can use the results of a shell command as the input of a Ruby block, check the example below. (Are you seeing all the possibilities now? 🤯)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7m3_nk4CQeBmJCbcHPvOXA.png" /></figure><h4>2. Tables</h4><p>This is self-explanatory: you can also create “Org tables”. Besides helping with displaying more complex results, you can also use their values in a similar way to how we use matrices in a programming language:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gVGF7-DHUN8lnr91NacG1g.png" /></figure><p>In the Python block, you can see how every row is a list where each item is a column of the table.</p><h4>3. Files</h4><p>You also can save the results in a different file. Here, for example, I’m saving the results in the my-script.py file so you’ll see a link to the file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rivb7fYJQ2Y-QHmisgeKbA.png" /></figure><p>These are just some of the most common types, but there are more like verbatim or scalar. You can check the org documentation <a href="https://orgmode.org/manual/Results-of-Evaluation.html">here</a> for a complete list of the types.</p><h4>The results format</h4><p>This option can be easily confused with the one that specifies the result type but it’s more oriented to how the results are displayed in the Org file. In general, the format will be the kind of block in which the result will be wrapped. For example, using :results output code the result will be wrapped in a code block (which can be run and tweaked as any other code block):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gq8X8rTcWJTLUqJRlYv7Mg.png" /></figure><p>There are several available formats: code, latex, HTML, raw, and graphics.</p><h4>How the results are handled in Org</h4><p>This one is more related to how the results are inserted in the emacs buffer. Among the options available, we have:</p><ul><li><strong>replace</strong> (the default one): if you run the block again the previous result will be replaced by the new one.</li><li><strong>silent</strong>: the results aren’t inserted in the org file. But in Emacs, you’ll see the result in the status bar.</li><li><strong>none</strong>: as you could guess it doesn’t show anything.</li><li><strong>append</strong>: the results of each run are inserted one after the other.</li><li><strong>prepend</strong>: similar to the one above except that the most recent results are inserted at the top.</li></ul><h4>Prologue and Epilogue (:prologue, :epilogue)</h4><p>There are a lot more headers that I would like to talk about but this article is just a brief introduction to Org in Emacs. So I’ll just mention the ones that I use the most while trying to address my problem (and actually needed to hack them and create new ones but that’s for a later post) I’m talking about the <em>:prologue</em> and <em>:epilogue</em> switches. They are kind of a setup and teardown instructions for the block. Let’s say that you want to create a block that shows the commands for manipulating a file. Well, your prologue could create the file and your epilogue could take care of deleting the file. If you have done some testing before this could look familiar. It is like preparing the environment so the code can be run and doing the cleaning at the end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qNgGSpWsXkASPQHKa2uWyw.png" /></figure><p>In the block above, you can see in the <em>:prologue</em> how we create a test.txt file with the line “<em>This is a line</em>” so this is run first before running the body of the block. Then <em>:epilogue</em> takes care of deleting that file and, as you can see in the second block, the file isn’t there anymore.</p><h4>Global and local switches (Properties)</h4><p>The last thing that I would like to mention before addressing the exporting of your org file is <em>properties</em>.</p><p>Once I started to work on my org file, I quickly realized the following:</p><ul><li>I was repeating a lot of variables and switches throughout many blocks.</li><li>I needed some kind of environment variables to prevent the work of rewriting the variables in each block when applying the steps to the staging and prod environments.</li></ul><p>These problems can be easily solved with properties. To declare variables and settings that can be used throughout all the blocks in the document, you just need to add a global property:</p><pre>#+PROPERTY: header-args: :var &lt;your list of vars&gt; &lt;your other switches&gt;</pre><p>You can also create global properties per programming language:</p><pre>#+PROPERTY: header-args:python :var &lt;your list of vars&gt; &lt;your other switches&gt;</pre><p>And if you don’t want everything set up in a single line and want properties in multiple lines for clarity’s sake, you can add the + so the properties can be appended instead of rewritten:</p><pre>#+PROPERTY: header-args:python :var my_var=1</pre><pre>#+PROPERTY: header-args:python+ :var other_var=2</pre><p>Now, these are global properties that will be applied throughout the file but you can also create properties per section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YEuxjGFwhsFA-BddqUVxRA.png" /></figure><p>Here you can see the scope of the properties, you should have noticed a different syntax for what can be thought of as “<em>local properties</em>”:</p><pre>:PROPERTIES:<br>:header-args:sh+: :var bye=&quot;See you&quot;<br>:END:</pre><p>These are commonly known as “drawers”. They exist so you can keep data related to a section in a single place (In Emacs, drawers will be folded automatically to reduce visual clutter, but you can always expand them by placing the cursor on the drawer and pressing TAB)</p><h4>Environment variables… Well, sort of… (setupfile)</h4><p>I struggled to find some kind of convention for how to handle environment variables, so here I’ll talk about my approach to the problem.</p><p>Turns out that an org file can call another file as his setup file, and this file will be loaded while opening the org file. Likewise, it can be updated by running <em>Ctrl-c Ctrl-c</em> with the cursor over the SETUPFILE directive, and the file will be parsed as if it were in the document.</p><p>So we could move our global properties to separate files, one for each environment. And when the time comes to move to the next environment we would just need to update the line that points to the setup file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KMKiMl1jVPSGt9LMHDhpHA.png" /></figure><h3>Exporting</h3><p>So now the time has come to share our file with our teammates. In my project, we need to share the commands that we are planning to run and get the green light to go ahead.</p><p>As the org file isn’t exactly popular neither among the programming community (maybe just among Emacs enthusiasts) we will need to export our file in another format. Fortunately, you have a lot of available formats to export this kind of file. Among the most popular you can find markdown, HTML, plain text, and Latex.</p><p>In my case, I decided to go for markdown. Ok, so some points to remark before exporting our file:</p><ul><li>The variables won’t be expanded in the exported file. What am I talking about? Well, when you export a block with variables you’ll get:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nQZIneHEC-qQjtfazxusVg.png" /></figure><p>Here I have the org file and the exported file on the side. As you can see, the code block in the exported file shows the command but not the value of the message variable. This is because when exporting files the variables aren’t expanded. To expand a code block you can run <em>Ctrl-c Ctrl-v v</em> while having your cursor on the block and this is when you can see the variables being assigned. So take this into consideration.</p><p>I needed to show the values of the variables to my team so I tweaked my org configuration to show them. Emacs can be tweaked to personalize almost everything but for that, you need to know Emacs Lisp. If you’re afraid of languages that use a lot of parentheses this could sound scary but I promise I was able to do this in a couple of days and without previous knowledge of the Lisp language. A useful resource for me to learn Emacs Lisp was the <em>System Crafters</em> Youtube playlist, if you want a quick introduction to the language you can check them <a href="https://www.youtube.com/watch?v=RQK_DaaX34Q&amp;list=PLEoMzSkcN8oPQtn7FQEF3D7sroZbXuPZ7">here</a>.</p><ul><li>Your commands will be run while exporting the file. This definitely wasn’t what I wanted given that I just can run the commands once they are approved, to prevent this from happening you can add this switch to the global properties: :eval no-export</li></ul><p>Now you’re ready to export the file. Run <em>Ctrl-c Ctrl-e</em> to see the exporting menu and choose a format.</p><p>I should mention that blocks have an <em>:exports</em> switch to choose what should be exported, you can choose between ‘code’, ‘results’, ‘both’, or ‘none’.</p><p>Here we just scratched the surface of what you can do on an org file. There are a lot of things that you can accomplish by mixing switches and customizing blocks. But just wanted to give you a basic idea of how you could set up a kind of executable documentation. In later posts, I hope to give you tips and hacks that can come in handy with not just org documents but with Doom Emacs in general.</p><p>Here are some resources that helped me while creating my org file:</p><ul><li>A <a href="http://howardism.org/Technical/Emacs/literate-programming-tutorial.html">post</a> that provides an awesome walkthrough org mode and literate programming.</li><li><a href="http://howardism.org/Technical/Emacs/literate-devops.html">Another example</a> of Literate DevOps.</li><li><a href="https://eschulte.github.io/org-scraps/">Useful samples of blocks setup</a>.</li><li><a href="https://org-babel.readthedocs.io/en/latest/">Babel reference card</a> for detailed documentation on block switches.</li><li><a href="https://orgmode.org/manual/Structure-of-Code-Blocks.html">Basic structure of code blocks.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=459fe361af82" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>