Linenoise: The Minimalist Line Editor for Your Terminal Apps
Ever found yourself building a CLI tool and wishing it had that polished, interactive feel? You know, with proper line editing, history navigation, and tab completion—just like your shell. But then you look at adding readline, and suddenly you're wrestling with a heavyweight, license-complicated dependency. It feels like overkill.
That's exactly the problem Linenoise solves. Created by antirez (of Redis fame), it's a self-contained, zero-dependency library that drops a capable line editor into your terminal application with almost no fuss. It's for developers who want to elevate their CLI tools from simple scanf-based input to something much more user-friendly, without the bloat.
What It Does
In a nutshell, Linenoise provides a small, embeddable alternative to GNU Readline and libedit. You integrate it into your C project, and it handles the user's line input for you. It offers familiar keyboard shortcuts for moving the cursor, editing the line, and scrolling through a persistent history. It also supports customizable tab completion. All of this happens in a single, portable .c and .h file pair.
Why It's Cool
The beauty of Linenoise is in its constraints. It's not trying to be everything for everyone. It's focused on doing the core job of line editing exceptionally well with minimal footprint.
- Zero Dependencies: It only needs a standard C library and a POSIX-like terminal. You can literally just copy
linenoise.candlinenoise.hinto your project and you're 99% done. There's no complex build system to wrestle with. - Surprisingly Capable: Despite its size (under 1500 lines of C), it gives you the essentials: Emacs-style keybindings (Ctrl-A, Ctrl-E, Ctrl-K, etc.), history with up/down arrows, and basic but functional tab completion. It even handles multi-line editing and has optional UTF-8 support.
- Battle-Tested: This isn't a toy. It was originally written for Redis, where it powers the
redis-cliREPL. It's proven itself in a widely-used, production-grade tool. - The License is Painless: It's released under the BSD license, which is about as friendly as it gets for incorporating into any project, open source or commercial.
How to Try It
The quickest way to see it in action is to check out the repository and build the example. The whole process is refreshingly straightforward.
git clone https://github.com/antirez/linenoise.git
cd linenoise
make
This will build an example executable. You can run it to play with a simple shell that has history (try the up/down arrows) and built-in tab completion for a few commands.
To use it in your own project, you don't even need to "install" it. Just:
- Add
linenoise.candlinenoise.hto your source tree. #include "linenoise.h"in your code.- Use
linenoise()instead offgets()or similar to get user input. - Use functions like
linenoiseHistoryAdd()andlinenoiseSetCompletionCallback()to add history and completion.
The README is excellent and has clear, concise examples of all the APIs.
Final Thoughts
Linenoise is one of those classic "solve one problem well" Unix-style tools. It won't give you the every-possible-feature kitchen sink of readline, and that's the point. For probably 90% of CLI tools that need better input, it's more than enough. It removes the friction and legal overhead of a major external dependency and replaces it with a few KB of clean, understandable code.
If you're hacking on a database client, a custom REPL, a configuration wizard, or any tool where you want to move beyond basic standard input, give Linenoise an hour of your time. It might just be the final touch that makes your tool feel solid and professional.
Follow us for more cool projects: @githubprojects
Repository: https://github.com/antirez/linenoise