Lekh

Nano’s friendliness, Rust’s muscle. A tiny terminal text editor that’s easy on day one and surprisingly powerful on day one hundred.

lekh — hello.rs Theme: Default
hello.rs 🦀 Rust 100% Ln 1/1 Col 1 LF

↑ It’s a real (tiny) editor. Go on, type something—or try Ctrl+T (5 themes), Ctrl+N, and Ctrl+K.

What Lekh does

Designed for quick config tweaks on remote servers and focused coding sessions alike—every feature earns its bytes without bloating the binary.

16 Languages Highlighted Out of the Box (Zero Regex Engine, Zero External Grammars)

  • 🦀 Rust
  • 🐍 Python
  • 🟨 JavaScript / TypeScript
  • ⚙️ C / C++
  • ☕ Java / C# / Kotlin / Swift
  • 🐹 Go
  • 🐚 Shell / Bash / Zsh
  • 🌐 HTML / XML (+ embedded JS & CSS)
  • 🎨 CSS / SCSS
  • {} JSON
  • ⚙️ TOML / YAML / INI
  • 📝 Markdown
  • 🌙 Lua
  • 💎 Ruby
  • 🗄️ SQL
  • ⚡ Zig

How Lekh works under the hood

Lekh is ~4,500 lines of strictly safe Rust (#![forbid(unsafe_code)]) split into five clean, single-purpose modules—no shell-outs, no network calls, and no executed config scripts.

  1. 01 · lib.rs

    0% Idle Event Loop

    Sleeps in the OS kernel on event::read() using zero CPU until a key, mouse, or bracketed paste arrives—then drains queued bursts before drawing once.

  2. 02 · editor.rs

    State & Coordinates

    Validates cursor bounds, word jumps, smart auto-indent, 10k-char bracket matching, and multi-click selection.

  3. 03 · buffer.rs

    Reversible Op Log

    Stores lines as Vec<char> for O(1) column access and records algebraic inverses (Ins↔Del, Split↔Join)—never cloning full snapshots.

  4. 04 · syntax.rs

    Cached Tokenizer

    Single-pass scanner with multi-line state + per-line tok_cache. Editing line k only invalidates cache from line k downward.

  5. 05 · ui.rs

    Single-Flush Frame

    Queues only visible viewport rows into a 64 KB BufWriter and flushes once per frame—zero terminal flicker.

🔒 4-Step Crash-Proof Atomic Save (fileio.rs)

  1. Private O_EXCL Temp File: Writes to .filename.lekh-pid-nanos.tmp with mode 0600 so planted symlinks are refused and secrets are never briefly world-readable.
  2. Symlink & Metadata Preservation: Resolves symlinks so saving updates the real target without breaking your symlink, copying original permissions and preserving BOM + LF/CRLF line endings.
  3. Hardware fsync(): Calls sync_all() on both the temp file and parent directory so bytes hit physical storage.
  4. Atomic POSIX rename(): Swaps the directory entry in one atomic kernel operation—power loss can never leave a truncated 0-byte file.

🛡️ Hostile-Input & Modification Guards

  1. Refuses Traps: Blocks directories, FIFOs, and device nodes (running lk /dev/zero exits cleanly instead of hanging or eating RAM).
  2. Binary & Size Protection: Refuses non-UTF-8 or NUL-byte binaries so executables are never silently mangled, with a configurable 64 MB cap (LEKH_MAX_MB).
  3. Disk-Change Detection: Checks file mtime before saving—if another process modified the file after you opened it, Lekh asks before overwriting.
  4. Panic-Safe Terminal Restore: A RAII TermGuard + custom panic hook guarantees raw mode and alternate screen are always cleaned up.

Download & install

The fastest way to get started is downloading a precompiled release binary directly from GitHub Releases—or building with Cargo in one command.

📦 Official Prebuilt Binaries on GitHub Releases

Every tagged release includes ready-to-run binaries (both lk and lekh) so you can download, extract, and run immediately without installing Rust.

Run on macOS (Apple Silicon & Intel)

1. Download lekh-macos-arm64.tar.gz from GitHub Releases, extract it, and move lk & lekh into your PATH:

# Extract the downloaded release archive & install:
tar -xzf lekh-macos-arm64.tar.gz
xattr -d com.apple.quarantine lk lekh 2>/dev/null || true
chmod +x lk lekh
sudo mv lk lekh /usr/local/bin/

# Verify & open any file:
lk --version
lk hello.rs

macOS Terminal tip: Enable Use Option as Meta key in Terminal → Settings → Profiles → Keyboard so Option+←/→ jumps by word.

How to use Lekh

Run lk for speed or lekh for clarity. Press Ctrl+G or F1 inside the editor anytime to pull up the built-in shortcut overlay.

lk index.html

Open an existing file (or start a new file that won’t touch disk until you press Ctrl+S).

lk +120 src/editor.rs

Open directly at line 120—ideal when jumping to compiler errors or stack traces.

LEKH_MAX_MB=256 lk app.log

Raise the safety file-size guard (default 64 MB) when inspecting huge log files.

Complete Keyboard & Mouse Reference
Category Shortcut What it does
File Ctrl+S / Ctrl+O / Ctrl+Q Atomic Save, Save As, and Quit (prompts if buffer has unsaved edits)
Undo & Clipboard Ctrl+Z / Ctrl+Y · Ctrl+C/X/V Group-merged Undo/Redo; Copy, Cut, Paste (with OSC 52 clipboard sync)
Line Power Ctrl+K · Ctrl+D · Tab / Shift+Tab Cut whole line, Duplicate line, Indent / Dedent single line or multi-line selection
Find & Replace Ctrl+F · F3 / Shift+F3 · Ctrl+R Live incremental smart-case search, next/previous match, Replace All
Navigation Ctrl+L · Ctrl+←/→ · Home / End Go to line[:col], jump by word (Option+←/→ on Mac), smart Home toggle
Selection & Mouse Shift+Move · Ctrl+A · Mouse Click to place cursor, drag to select, double-click word, triple-click line, wheel scroll
Themes & View Ctrl+T · Ctrl+N · Ctrl+P · Ctrl+G Cycle 5 themes, cycle Absolute/Relative/Off line numbers, toggle auto-indent, Help
How Lekh Compares
Capability GNU Nano Vim / Neovim Lekh (lk)
Learning curve Zero (on-screen hints) Steep modal grammar Zero (familiar Ctrl shortcuts)
Memory safety C (manual memory) C / Lua 100% Safe Rust (forbid(unsafe))
Syntax highlighting Regex files required Built-in / Tree-sitter 16 languages compiled into binary
True-Color themes & bracket glow Limited Via config 5 built-in themes on Ctrl+T
Trojan-Source Unicode defense No Plugin needed Built-in red ? badge + warning
How do I paste text from my browser or another app into Lekh?

Use your terminal’s standard system paste shortcut (Cmd+V on macOS, Ctrl+Shift+V on Linux/Windows Terminal). Lekh supports bracketed paste—meaning pasted code won’t trigger runaway auto-indentation, and dangerous control bytes are stripped automatically.

Why are there two commands: lekh and lk?

Both binaries are built from the exact same codebase: lekh is the expressive full name, and lk saves keystrokes when you’re opening dozens of files a day in the terminal.

What license does Lekh use?

Lekh is free and open-source software released under the permissive MIT License (Copyright © 2026 khajumsanjog). Anyone is free to use, copy, modify, merge, publish, distribute, or bundle Lekh commercially or privately, provided the copyright and permission notice are preserved. Read the full text in LICENSE on GitHub ↗.