Stop mashing the up-arrow: shell history expansion and fc, explained
Most terminal users repeat commands by scrolling through history one arrow-press at a time. This piece argues that the DRY principle we preach in code should extend to the command line, and that bash, csh, tcsh, and zsh already ship the tools to do it. The core mechanism is the event designator — the exclamation mark — which lets you reference earlier commands and their individual arguments without retyping. The syntax follows a consistent three-part shape: which line to pull from (!!, !-2, !ssh, !?text?), which word of that line (:0 for the command, :$ for the last argument, :* for all args), and an optional modifier to transform it (:h for the directory, :t for the filename, :r to drop the extension, :s/// to substitute). Familiar shortcuts like sudo !! and cd !$ are just special cases of the same grammar.
For a POSIX-portable alternative, the author points to fc, which pulls one or more past commands into your real editor for arbitrary edits before rerunning them — handy for reworking long invocations, though minimal shells like dash and busybox ash omit it. The realistic pitch is modest: these tricks won’t make you a 10x engineer, but they save a handful of keystrokes hundreds of times a week over a career.
Because blindly expanding history can misfire, the article recommends safety nets: bash’s histverify (shopt -s histverify) and zsh’s HIST_VERIFY expand the command inline on Enter so you can review or discard it, and the :p modifier prints what would run without executing it while still logging it to history for a deliberate rerun.
Read the full article
Continue reading at Hacker News →This is an AI-generated summary. Read the original for the full story.