I don’t usually install or un-install many packages on my Gentoo, but sometimes I want to try new stuff. Since I have quite short leash on my system, therefore it usually would pull in some dependencies.

For example:

$ emerge -qp1 freedink
[ebuild  N    ] games-rpg/freedink-data-1.08.20121209
[ebuild  N    ] media-sound/fluidsynth-1.1.6
[ebuild  N    ] media-libs/audiofile-0.3.6-r1
[ebuild  N    ] media-libs/libsdl-1.2.15-r4
[ebuild  N    ] media-libs/sdl-ttf-2.0.11
[ebuild  N    ] media-libs/sdl-gfx-2.0.24
[ebuild  N    ] media-libs/sdl-image-1.2.12
[ebuild  N    ] media-libs/sdl-mixer-1.2.12-r3
[ebuild  N    ] games-rpg/freedink-1.08.20121209

8 extra packages are pulled in as dependencies. If it’s only one or two, I’d probably copy-and-paste to my notes for un-installation if that would happen. The format above isn’t ready for being fed to emerge for un-installation, so I came up with this command:

$ emerge -qp1 freedink | sed 's/.* \(.\+\) .*/=\1/' | tr -d '\n'
=games-rpg/freedink-data-1.08.20121209 =media-sound/fluidsynth-1.1.6 =media-libs/audiofile-0.3.6-r1 =media-libs/libsdl-1.2.15-r4 =media-libs/sdl-ttf-2.0.11 =media-libs/sdl-gfx-2.0.24 =media-libs/sdl-image-1.2.12 =media-libs/sdl-mixer-1.2.12-r3 =games-rpg/freedink-1.08.20121209

The output can be used directly, they are full atom. A complete command history would be:

$ emerge -qp1 freedink | sed 's/.* \(.\+\) .*/=\1/' > thelist
$ sudo emerge -q1 freedink
$ emerge --depclean -p $(<thelist)
$ sudo emerge --depclean $(<thelist)

This method is very manual and not so convenient because of the sed, but that’s what I got. I actually found a package app-portage/demerge, which is said for “revert to previous installation states”, but recent version 0.047 was from May, 2007. I don’t even try it, even 90% I believe it’s still working. But between a package and a sed, I chose sed.

Note that tr isn’t necessary when you feed back to emerge because of Bash’s word splitting—or any POSIX shells I believe—with `cat thelist`.

The pretended --depclean should gives you exact number of packages to be removed:

$ emerge --depclean -p $(<thelist)
[snip]
Number to remove:     9
$ wc -l thelist
9 thelist

If the numbers don’t match, that’s probably something is also done after the emerge, such installing packages which depend on the 8 extra dependencies. Either way, just remove whatever still is left on the removal list.