While terminal programs may seem wildly different at first glance, many follow a set of implicit conventions that make them easier to use. These rules aren’t formally documented in standards like POSIX, yet they have emerged over decades of terminal usage.
Some conventions, like using Ctrl-C
to interrupt a process, are well known. Others, such as limiting colors to a basic set or disabling colors in pipes, are less obvious but widely followed. This article explores these unwritten rules and their impact on user experience in the command-line environment.
Common Unwritten Rules in Terminal Programs
1. Non-Interactive Programs Should Exit on Ctrl-C
Most non-interactive programs (such as cat
, ls
, or grep
) terminate when the user presses Ctrl-C
. This is because SIGINT
(the signal sent when Ctrl-C
is pressed) terminates a process unless it has a custom handler.
However, this rule does not apply to interactive programs like vim
, less
, or python3
. In these cases, Ctrl-C
is used to interrupt the current operation rather than exiting the program.
2. Text-Based UI (TUI) Programs Should Exit with q
Programs that display a full-screen text-based interface, such as htop
, less
, or top
, often exit when the user presses q
. This convention allows for an intuitive way to quit applications that lack traditional command-based interfaces.
However, not all TUI programs follow this rule. Tools like tmux
or text editors require specific commands (exit
, :q!
, Ctrl-X
, etc.) to close properly.
3. REPLs Should Exit on Ctrl-D
at an Empty Line
Read-Eval-Print Loops (REPLs) like python3
, sqlite3
, and bash
allow users to exit by pressing Ctrl-D
on an empty line. This shortcut sends an End-of-File (EOF) signal, which many programs interpret as a termination command.
Some exceptions exist. The Erlang REPL, for example, does not follow this convention and requires the user to type q().
to exit.
4. Programs Should Limit Themselves to 16 Colors
Most terminal programs stick to the 16 basic ANSI colors to ensure compatibility across different terminal configurations. Hardcoding colors using hex codes can create visibility issues, especially for users with custom themes.
Some advanced applications, such as text editors (Helix
, NeoVim
), override this rule to offer rich syntax highlighting. However, for general-purpose terminal tools, adhering to ANSI colors helps maintain consistency.
5. Programs Should Mimic Readline Keybindings
Many command-line programs implement common keybindings similar to readline
, making text editing more predictable. Some widely used shortcuts include:
Ctrl-E
: Move cursor to the end of the line.Ctrl-A
: Move cursor to the beginning of the line.Ctrl-W
: Delete the last word.Ctrl-U
: Delete the entire line before the cursor.Ctrl-K
: Delete from the cursor to the end of the line.
Programs like ipython
, fzf
, fish
, and zsh
often implement these keybindings even if they do not use readline
directly. However, exceptions exist, particularly among text editors and tools with complex input handling.
6. Disable Colors When Writing to a Pipe
Programs that support colored output, such as ls
and grep
, usually disable colors when writing to a pipe or a file to avoid adding unreadable escape sequences.
For example:
ls --color=auto
Will display colors in a terminal but not when redirected to a file:
ls --color=auto > output.txt # No color codes in the output file.
If colored output is needed in a pipeline, users can override the default behavior with:
grep --color=always "error" file.log | less -R
Alternatively, the unbuffer
command can force color output:
unbuffer grep "error" file.log | less -R
7. -
Should Represent stdin/stdout
Many command-line utilities interpret -
as a placeholder for standard input (stdin
) or standard output (stdout
).
For example, formatting Python code from the clipboard in macOS:
pbpaste | black - | pbcopy
Here, black -
processes the piped input and returns formatted output, which is then copied back to the clipboard using pbcopy
.
This convention allows tools like tar
, gzip
, awk
, sed
, and sort
to work seamlessly in pipelines.
Learning These Rules Takes Time
These unwritten rules make terminal programs more predictable, but they take time to learn. Initially, it may seem like each tool has its own behavior, but over time, patterns emerge.
Recognizing these patterns enhances efficiency and simplifies navigation in the command-line environment. While exceptions always exist, understanding these conventions makes it easier to work with new tools and integrate them into daily workflows.