DEV Community 👩‍💻👨‍💻

Darth Espressius
Darth Espressius

Posted on

Replacing terms using ^ in the Linux Terminal

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

the output is

star wars
Enter fullscreen mode Exit fullscreen mode

If I type the following after the above

^wars^trek
Enter fullscreen mode Exit fullscreen mode

The output becomes

star trek
Enter fullscreen mode Exit fullscreen mode

Let me show that in-terminal so you get an idea of exactly what we're doing
Basic Replacement

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

followed by

^luke^leia^:&
Enter fullscreen mode Exit fullscreen mode

the result is the equivalent of running:

echo leia leia
Enter fullscreen mode Exit fullscreen mode

Again, to see the continuity of how these commands work, we need to see them in-shell:

Full replace

And now back to the original example (ignore the CondaError as this was necessary to cancel the original command)

conda command

Top comments (0)

DEV

Thank you.

 
Thanks for visiting DEV, we’ve worked really hard to cultivate this great community and would love to have you join us. If you’d like to create an account, you can sign up here.