lex the hacker
I prefer to be called a hacker.
Describe what you want in plain English, and lex writes the shell command for you.
$ lex "search the dir for 'xyz'"
$ grep -rn 'xyz' .
While lex thinks, you get a spinner. Then it shows the command with a short
explanation of how it works, and offers it to you — pick it (just press Enter)
to copy it to your clipboard:
╭──────────────────────────────────────────────────╮
│ grep -rn 'xyz' . │
│ │
│ Searches every file under the current directory │
│ for the text "xyz", printing each match with its │
│ file name and line number. │
╰──────────────────────────────────────────────────╯
? copy to clipboard? (Use arrow keys)
» grep -rn 'xyz' .
skip
✓ copied to clipboard
Either way, the command also lands on your command line, ready to run.
It never executes anything itself. You always read the command and press Enter (or edit it, or throw it away). Nothing runs without you.
On typing. Putting text on your prompt uses the
TIOCSTIioctl, which current Linux kernels disable by default (dev.tty.legacy_tiocsti = 0) because it was a privilege-escalation vector. When it's unavailable — a hardened kernel, no terminal, a pipe — or when the command spans more than one line, the command simply doesn't land on your prompt. That's a normal outcome, not an error: it's still right there in the panel, and on your clipboard. Either way,lexnever presses Enter for you.
Install
pip install lex-the-hacker
This gives you the lex-the-hacker command. lex is a common command name
(the lexical analyzer generator has had it since the 70s), so lex doesn't
claim it — but nothing stops you:
alias lex='lex-the-hacker'
The examples in this README assume that alias.
Configuration
lex talks to any OpenAI-compatible chat API — hosted OpenAI, or a local
Ollama server. Point it at one with three environment
variables:
| Variable | Required | What it is |
|---|---|---|
LEX_MODEL |
yes | The model name to request, e.g. gpt-4o-mini or llama3.1. |
LEX_API_KEY |
yes | The API key. Ollama ignores it, but the client still requires a non-empty value — ollama works fine. |
LEX_BASE_URL |
no | Base URL of the endpoint. Defaults to OpenAI's hosted API. |
Hosted OpenAI:
export LEX_MODEL=gpt-4o-mini
export LEX_API_KEY=sk-...
A local Ollama server:
export LEX_MODEL=llama3.1
export LEX_API_KEY=ollama
export LEX_BASE_URL=http://localhost:11434/v1
lex also tells the model which shell you're in, so the command it writes
matches your shell's syntax. Run lex shell to see what it detected.
Clipboard on Linux. Copying first tries your system clipboard via pyperclip, which on Linux needs one of
xclip,xsel, orwl-clipboardinstalled. Without one,lexfalls back to OSC 52 — asking your terminal itself to set the clipboard, no install needed — which modern terminals (VS Code, kitty, alacritty, WezTerm, iTerm2, foot, …) support, even over SSH. If neither route works, the copy is skipped quietly.
Usage
Call lex with a description of what you want to do, in quotes:
$ lex "find every python file changed in the last week"
lex puts its best guess on your prompt:
$ find . -name '*.py' -mtime -7
From there it's your command like any other — run it, tweak it, or clear the line and try a different description.
More examples:
$ lex "undo my last git commit but keep the changes"
$ git reset --soft HEAD~1
$ lex "compress this folder into a tar.gz"
$ tar -czf folder.tar.gz folder/
$ lex "show what's listening on port 8080"
$ lsof -i :8080
Aliases and scripts
lex alias <name> <description> gives you the command and a one-liner that
writes the alias to your shell's config file — ~/.zshrc for zsh, ~/.bashrc
for bash, and so on — so it survives beyond the current session:
$ lex alias ll "list files recursively, long format"
$ echo "alias ll='ls -laR'" >> ~/.zshrc
(New shells pick it up automatically; source ~/.zshrc brings it into the
current one. If lex doesn't know your shell's config file, it offers the
bare alias definition instead.)
lex script <name> <description> gives you the command and a one-liner that
writes it to an executable script, with the extension and shebang for your
shell:
$ lex script convert "convert every png in this folder to jpg"
$ cat > convert.sh <<'LEX_EOF'
#!/bin/bash
for f in *.png; do magick "$f" "${f%.png}.jpg"; done
LEX_EOF
chmod +x convert.sh
The alias or script command is the one you're offered to copy — the panel shows the underlying command above it, so you can see what you're about to wrap. The script one spans several lines, which is the "more than one line" case in the note on typing above: it goes to your clipboard rather than your prompt.
As always, lex only writes the command — it doesn't touch your config file
or create the script itself. Nothing changes until you run it (lex shell
shows which shell and config file it detected).
Why you're always safe
lex only ever writes a command to your shell. It doesn't run it, and it can't run it — executing is up to you. So if a suggestion looks wrong, you'll see it before anything happens.
Metadata
Release files for lex-the-hacker 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lex_the_hacker-0.3.0.tar.gz | 121.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lex_the_hacker-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 134.7 kB
Release files / lex_the_hacker-0.3.0.tar.gz
| Download URL | lex_the_hacker-0.3.0.tar.gz |
|---|---|
| Size | 121.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e4a812ac09e2a60129ab243e4b29493c0e28d9040be7011e27db85d649fdd83
|
|
BLAKE2b-256 checksum How to use checksums |
f1fb51244058861fa47395cd036760f33e15cf64bdf0a77bff4cc5457315e61b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / lex_the_hacker-0.3.0-py3-none-any.whl
| Download URL | lex_the_hacker-0.3.0-py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26699656b51a06d1f1dd94497cbf0ad587b40b2fc35a80bad3c02615135f7894
|
|
BLAKE2b-256 checksum How to use checksums |
e90a8039cd8fcc4152cfa466241ddf888ec0dfdbdc52b10d49493ebbea347a92
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|