A shell exclamation mark is not for yelling. Be lazy.
This article dives into the often-overlooked power of shell history expansion, showing how exclamation marks and the fc command can dramatically reduce typing. It appeals to the 'lazy bastards' in us all, offering elegant solutions for command-line repetition beyond simple arrow-key mashing. Discover these obscure but powerful techniques to enhance your terminal efficiency and practice command-line DRY principles.
The Lowdown
This article explores the potent but underutilized features of shell history expansion, primarily focusing on exclamation marks (!) and the fc command. It argues that by leveraging these tools, users can significantly boost their command-line efficiency and reduce repetitive typing, applying the 'Don't Repeat Yourself' (DRY) principle to the terminal.
- Event Designators: Learn to recall entire previous commands using shorthand like
!!for the last command,!sshfor the most recent command starting with 'ssh', or!-2for two commands back. - Word Designators: Discover how to extract specific arguments or parts of previous commands, such as
!:$for the last argument,!:1for the second argument, or!:*for all arguments. - Modifiers: Explore advanced transformations like
:h(dirname),:t(basename),:r(strip extension), and:s/old/new/for in-place string substitutions. - The
fcCommand: Understand the POSIX-standardfcutility, which allows editing one or multiple previous commands in an external text editor or applying substitutions across a range of history entries. - Safe Usage: Implement features like
histverify(for Bash/Zsh) or the:pmodifier to preview command expansions before execution, preventing accidental or unintended operations. - Customization: Learn how to disable the
!feature or reassign its trigger character if it conflicts with other workflows.
The piece concludes by encouraging users to practice these techniques, highlighting the substantial keystroke savings and workflow improvements that even a few mastered designators can provide throughout a developer's career.