Hack 61. Substitute words from history commands

by Ramesh

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

  1. # ls anaconda-ks.cfg
  2. anaconda-ks.cfg
  3.  
  4. # vi !!:$
  5. vi anaconda-ks.cfg

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).

  1. # cp anaconda-ks.cfg anaconda-ks.cfg.bak
  2. anaconda-ks.cfg
  3.  
  4. # vi !^
  5. vi anaconda-ks.cfg