Bash history substitution trick

I saw the following neat little trick in this link:

# renaming a long file-name using a regex
$ touch config.txt
$ mv config.txt !#:1:s/txt/json

The exact meaning of the !# is, quoting from the man page:

Event Designators       An event designator is a reference to a command line entry in the history list.  Unless the reference is absolute, events are relative to the current position in the history list.
       !      Start a history substitution, except when followed by a blank, newline, carriage return, = or ( (when the extglob shell option is enabled using the shopt builtin). 


 !#     The entire command line typed so far.

From ‘man bash’

i.e. most people know that bash can insert things from history using commands like !! and !50 (i.e. rerun last command and rerun the 50th history item respectively).

But !# let’s you take something from the existing line and re-use it.

Here is another example, from here:

curl > ~/bin/ack && chmod 0755 !#:3 

In this case, !#:3 takes the 3rd element from the existing command. Starting from zero, that turns out to be ~/bin/ack in this case.