I use the terminal quite a lot in my day-to-day activities. This includes, but is not limited to: copying files, installing packages, running updates, searching for folders, etc. Sometimes, the commands are simple ls -la
or grep
to show files and search for text respectively.
But sometimes, these commands are longer, MUCH longer, and this is past the point of realizing that I should have written this in a script file to run that way. AND, God forbid that I happen to make a mishap in typing some unnecessarily convoluted command and hit enter OR that I need to use a similar command again, then I have to go through the trouble of either typing it from scratch or hunting through terminal history to find, modify and re-execute the command.
Until I discovered the ^
operator....
Let's start with an example, let's say I wanted to use conda
(a package manager commonly used for data-science development), to create a new environment, in this case I'm using the following code from NVIDIA's RAPIDS install instructions for ease of demonstration.
conda create -n rapids-21.12 -c rapidsai -c nvidia -c conda-forge \
rapids=21.12 python=3.8 cudatoolkit=11.5 dask-sql
However, let's say that I realised that I wanted to actually create the environment using a different version of python (again, for demonstration purposes, although there are packages that work with 3.7 but not 3.8).
In order to replace the 3.8
from the above command, here's the code I can use for in-place substitution:
^3.8^3.7
The above code will replace the first occurence of 3.8
from my previous command with 3.7
and proceed to re-execute the command.
I realise the above is REALLY application-specific, so let's break this down:
If I type:
echo star wars
the output is
star wars
If I type the following after the above
^wars^trek
The output becomes
star trek
Let me show that in-terminal so you get an idea of exactly what we're doing
The ^
operator is a shorthand way of using the gs
command for global substitution. In other terms, ^wars^trek
is equivalent to
!!:gs/wars/trek
Now let's say I wanted to replace all instances of a term from the most-recently run command, so if I ran:
echo luke luke
followed by
^luke^leia^:&
the result is the equivalent of running:
echo leia leia
Again, to see the continuity of how these commands work, we need to see them in-shell:
And now back to the original example (ignore the CondaError
as this was necessary to cancel the original command)
Top comments (0)