2025-12-08
Courtesy of
help fc
alias r='fc -s'
With the fc -s [pat=rep ...] [command] format, command is re-executed after performing the OLD=NEW substitution.
A useful alias for it is r='fc -s', so that:
- typing
rre-executes the last cmd. - typing
r ccexecutes your last command beginning withcc. - typing
r mkdi=mkdirappliespatt=repsub to the last command in history. - typing
r v1=v2 curlwould search for the lastcurlcommand in history and re-execute it with applied replacement.
bash
r='fc -s'# add `alias r='fc -s' to your ~/.aliasrc to make it permanent.
$ cc file.c -o progrm$ r cc cc file.c -o program
$ cat README$ r cat README
$ mkdi -p a/b/c/dir$ r mkdi=mkdir mkdir -p a/b/c/dir
$ curl https://api.example.com/v1$ r v1=v2 curl curl https://api.example.com/v2
In the same vein, you can add ^pat^rep and !! to your term-fu arsenal, the difference being that, these only apply to the previous command in history:
bash
$ cd /usr/locl/bin$ ^locl^local cd /usr/local/bin
$ tar tf backup.tar.gz# list (t)able of contents for (f)file $ ^tf^xzf tar xzf backup.tar.gz# (x)extract (z)gzip (f)file
$ ls$ !! ls
$ vim /etc/hosts$ sudo !! sudo vim /etc/hosts
$ ecgo test$ !!:s/ecgo/echo# vim users, do you recognize this? echo hi
$ curl https://api.example.com$ !! | jq curl https://api.example.com | jq
$ ls *.tmp$ !! | xargs rm ls *.tmp | xargs rm