I think what you are trying to accomplish is very hard if not impossible, even when you can create a wrapper function to do what you want and an alias to call the wrapper instead of the actual command it all goes wrong because you usually run
sudo pacman -S and sudo breaks all functions and aliases by switching contexts.
But not all is lost, you can list all packages explicitly installed with
pacman -Qe, so if at any time you wish to reinstall the system or copy the environment to another machine you can get the list to a file, transfer the file to the target machine and run pacman to install all packages.
To make the file:
pacman -Qe | cut -d ' ' -f 1 | tr "\n" " " >the-file.txt
pacman -Qe: Lists all explicitly installed packages.
cut -d ' ' -f 1: is to remove the package versions and keep the names only.
tr "\n" " ": is to change newlines to spaces.
>the-file.txt: Is to redirect the output to a file, feel free to change the file name and path to your needs.
This will output a space separated list to the-file.txt, you then transfer the file to the target machine and run:
sudo pacman -Sy - <the-file.txt
Voila, pacman is installing all packages from the list and you will get the same result.
You can also add the
--needed argument to pacman so it wont reinstall up-to-date packages
pacman -S --needed ....
And if you are doing the new machine remotely you can skip the file transfer and just pipe the results to ssh and it will carry on to the remote machine:
pacman -Qe | cut -d ' ' -f 1 | tr "\n" " " | ssh -t user@host "sudo pacman -S --needed -"
-t ssh allocates a pseudo terminal, otherwise sudo will fail.