I find myself often wanting to do simple commands like:
cp /really/long/path/from/file.txt /really/long/path/to/file.txt
Although I already use shortcuts like !!
and !$
often, it would be nice to be able to reference the last argument in the current command line (and optionally expand it for editing). Is there a way to do this in zsh, or some other equivalent trick to save time?
In general, you can refer to individual words in the current command line using history expansion.
$ cp /really/long/path/from/file.txt !#:1:s/from/to
or
$ cp /really/long/path/from/file.txt !#:$:s/from/to
The !#
is history expansion for the command line typed so far. :1
specifies the first argument in that expansion (in ths case, the long file path). :$
could be used instead to refer to the last argument, independent of how many arguments have been typed so far. :s/from/to
performs text substitution on the selected word.
For this task, you can also use brace expansion:
$ cp /really/long/path/{from,to}/file.txt
(Note: both of these are taken from bash
, but also work in zsh
. There may be other zsh
-only tricks that I am not aware of.)
You can hit Tab to expand stuff on zsh. For example:
If I do this command first
% ls /etc
And in this next line I do
% !!<Tab>
The !!
will be replaced with
% ls /etc
So I can edit this the way I want. This works for a lot of things like *
and Environment variables. For example tapping the Tab key after $TERM
, will replace (expand it) with (for example in my case) xterm-256color
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