Edit previous commands in shell before running them

Let's say you run a certain command with some parameters (like opening a file in vim). Then you want to run basically the same command, but instead of opening the file in vim, you just want to view the contents with cat.

The most common way of doing this would be either:

Recently I found out you can do it a different way and it's acually better for my workflow once I got used to it.

As you can see below, this will run the previous command, but replacing the word vim with cat

user~$ sudo vim /etc/apt/sources.list.d/nginx.list
user~$ !!:gs/vim/cat
sudo cat /etc/apt/sources.list.d/nginx.list
deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/debian bookworm nginx

This can be useful e.g when you edit a file and then want to print out the edited contents into the shell to copy to a ticket or documentation.


Revision #10
Created 5 July 2024 19:55:12 by Marek
Updated 6 July 2024 02:48:37 by Marek