Ahmad Ismail
Ahmad Ismail

Reputation: 13852

Function To Repeat The Last Command With a Substitution

I normally change part of the previous command using

!!:gs/Change/ChangeTo

To simplify I have created a function

funciton re() {
    !!:gs/$1/$2
}

Now the output is like following

[~/Desktop]$ print -P '\033[34mThis is the same color as in your solarized palette\033[0m'
This is the same color as in your solarized palette
[~/Desktop]$ !!:gs/34/35                                                                
[~/Desktop]$ print -P '\033[35mThis is the same color as in your solarized palette\033[0m'
This is the same color as in your solarized palette
[~/Desktop]$ re 35 36                                                                   
re:1: no such file or directory: !!:gs/35/36
[~/Desktop]✕127$

So, it is giving error re:1: no such file or directory: !!:gs/35/36 when I am invoking the function.

I have also tried

funciton re() {
    ^$1^$2^:G
}

It says command not found

[~/Desktop]$ re 35 36
^35^36^:G: command not found
[~/Desktop]✕127$ 

What might be the solution here?

Upvotes: 0

Views: 86

Answers (2)

phi
phi

Reputation: 1

Mr. glenn jackman, I found that

re 2>/dev/null 

suppresses all output.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246764

You'll want to use the fc command. After a little experimentation:

re() { fc -e - "$1=$2"; }

Then:

$ echo foo bar
foo bar
$ re bar qux
echo foo qux
foo qux

I'm not sure how to suppress the edited command from being printed, if that's important for you.

Upvotes: 1

Related Questions