In my workflow I do very usually:
cat this/very/long/filename.txt cd !$ bash: cd: this/very/long/filename.txt: Not a directory
Which is to be expected :(
And now I recover the last command, remove manually the file part, and repeat the cd
, which now works. That is too much typing!
It would be sooo nice if there was a bash
shortcut like:
cd !§
Which could give me the parent of the last used parameter. I know !§
does not exist, I just wished it did! Is there something which can satisfy this?
Et voilà. History modifiers come to the rescue!
$ cd !!:$:h
which can be abbreviated to
$ cd !$:h
This command takes the last argument of the previous command, and removes the trailing path name.
In more details:
!!
expands to the previous command:$
expands to the last argument of the previous command:h
takes the header; that is, removes the file name (which is the trailing part of the above last argument)As an aside,
!!:$:t
does exactly the opposite.
For an in-depth discussion, please refer to the Bash documentation.
This shorter version would also work:
cd !$:h
Details:
!$
is synonymous to !!:$
which presents the last argument of the previous command.:
separates the modifier from the event-word designator.h
is the modifier that removes the trailing file name component.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With