Recalling a word from the current command line


I often find myself using a command like this to make a backup of a file:

cp /etc/slop/gunk.conf /etc/slop/gunk.conf_before_v2_upgrade

Since the first portion of the second argument is always the same as the first argument (the green text), it bugged me to have to do tab completion for it. Luckily, zsh and bash provide an event designator for the current command line (ie the one you’re currently typing). It is !# and it works just like the other events (eg !! is the previous command). See Zsh Documentation – History Expansion. Bash’s syntax is very similar.

So now when I want to make a crude backup like above, I use something like this:

cp /etc/slop/gunk.conf !#:1_before_v2_upgrade

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!