<?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 Kelechi Ogbonnaya on Medium]]></title>
        <description><![CDATA[Stories by Kelechi Ogbonnaya on Medium]]></description>
        <link>https://medium.com/@deltronfr?source=rss-3584be0b7ff2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NVBsHpTUMA9j0lyfHHEtrg.jpeg</url>
            <title>Stories by Kelechi Ogbonnaya on Medium</title>
            <link>https://medium.com/@deltronfr?source=rss-3584be0b7ff2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:45:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deltronfr/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[Implementing Tab AutoCompletion: Raw Modes, TTYs, and Input Handling]]></title>
            <link>https://medium.com/@deltronfr/implementing-tab-autocompletion-raw-modes-ttys-and-input-handling-bce4095e6f79?source=rss-3584be0b7ff2------2</link>
            <guid isPermaLink="false">https://medium.com/p/bce4095e6f79</guid>
            <category><![CDATA[tty]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[asni]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[autocomplete]]></category>
            <dc:creator><![CDATA[Kelechi Ogbonnaya]]></dc:creator>
            <pubDate>Fri, 16 Jan 2026 21:51:35 GMT</pubDate>
            <atom:updated>2026-01-16T21:51:35.524Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently took my first step into systems programming by building a shell, one of the most commonly used application by developers. A shell is a program that acts as an interface between the operating system and the user, it allows users to interact with the operating system by entering commands.</p><p>I initially thought it would be a straightforward project, after all it is essentially an REPL, but i quickly found out it was anything but, particularly when implementing tab autocompletion. While autocompletion may appear to be a simple text-matching problem, tab autocompletion heavily depends on how the terminal delivers input to programs. Before any completion algorithm, you need complete control over how keystrokes are handled and these details are usually hidden by the operating system.</p><h3>Understanding the TTY</h3><p>Before we get into tab autocompletion, we need to understand what the TTY is. The TTY, teletype, provides a text-only interface for interacting with the system. But I find it ideal to think of the TTY as a middleman that sits between your keyboard and any software you are running(in this case, the shell).</p><p>In a terminal, when you type, the characters don’t go straight to the program. They go to the <em>Line discipline(which is a part of the TTY) </em>first. This is important because the line discipline handles special commands, signal generation, etc by default, and leaves normal key characters as is. This is perfectly in line with the UNIX philosophy of keeping applications as simple as possible. But it simply won’t cut it for interactive applications, such as the shell, you have to be the one to handle each interpretation of the user key presses and not the TTY. The TTY will only give our shell the data we need when we press the ‘ENTER’ key, you can see how this won’t work when I am trying to autocomplete with ‘TAB’ without giving my shell input. So how do we take control of input handling from the operating system? By switching the terminal into raw mode.</p><h3>Cooked vs. Raw</h3><p>In order to get Tab autocompletion, you have to start demanding every single byte from the user immediately, ‘ENTER’ should just be another byte handled and not the event your entire program is waiting for. To do this, you have to disable your terminal from cooked(canonical) mode — where nothing moves until <em>‘\n’. </em>If you’ve ever seen raw ANSI escape sequences printed to the screen when you press the arrow keys, it usually means the application is not interpreting the input it is receiving. Arrow keys always emit escape sequences (for example \x1b[A), but in cooked mode the line discipline consumes and interprets them before the program ever sees the bytes. In raw mode, those bytes are delivered directly to the application, which must parse and handle them itself.</p><p>Raw mode, however, disables the “wait for newline flag”(<strong>ICANON</strong>) in the terminal and also disables <strong>ECHO</strong> flag(characters typed by a user are immediately displayed on the screen). But it allows us to capture what the user types byte by byte immediately. Most programming languages come with the termios library, the UNIX interface for controlling terminal I/O settings, if you really want to be in control of the terminal, this is the way. But most programming languages also have external libraries that handle this well and abstract away the termios settings, for instance, golang’s <em>term</em> package — <a href="http://pkg.go.dev/golang.org/x/term">pkg.go.dev/golang.org/x/term</a>.</p><h3>Catching the Tab Key</h3><p>Now how do you actually handle the Tab key? First of all you will need read loop that keeps on reading each single byte sent by the user and a buffer to store regular characters and print it back to the user. The Hex value for the tab key is <strong>0x09</strong>, to reference other <a href="https://en.wikipedia.org/wiki/ANSI_escape_code#:~:text=%5B14%5D-,C0%20control%20codes,-%5Bedit%5D">keys</a>. If it is a Tab key, trigger your autocompletion logic, considering it is a shell you can simply use user prefix for your search. But if you get a match, how do you render this result to the terminal? You’d need to take a quick detour on ANSI codes.</p><p>ANSI escape codes are special sequences of characters that control how text appears in a terminal. In this case, we’d need these characters to move our cursor to the correct position after the matched result is displayed. Another quick detour on your cursor positioning, you would need to always know where your cursor is at, for a quick shell implementation you could easily use an integer to store the position and logic to prevent overflows.</p><p>The anatomy of an ANSI escape sequence usually starts with the <strong>CSI(Command Sequence Introducer)</strong>: <em>\x1b.</em></p><p>Common commands you’ll need:</p><ul><li>\x1b[K: Clear from the cursor to the end of the line.</li><li>\x1b[D: Move the cursor back(Left).</li></ul><p>Reference <a href="https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797">this</a> for more. During tab autocompletion, you usually clear everything from the cursor to the end of the line and move the cursor back and forth with the necessary ANSI escape codes to rewrite the line while maintaining the cursor position.</p><h3>Conclusion</h3><p>The main aim of writing this was to highlight the less obvious parts of building tab autocompletion for a terminal application. The operating system ususally abstracts a lot of these concepts, and building this forced me to engage with terminal modes, line discipline and how to render user inputs (backspace was an interesting one).</p><p>For those interested, the complete implementation lives in a minimal shell I built in go: <a href="https://github.com/deltron-fr/dshell">dsh</a>.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bce4095e6f79" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>