The distribution of angles subtended between pairs of galaxies and the line of sight, which is uniform in real space, is distorted by their peculiar motions, and has been proposed as a probe of cosmic expansion. We test this idea using N-body simulations of structure formation in a cold dark matter universe with a cosmological constant and in two variant cosmologies with different dark energy models. We find that the distortion of the distribution of angles is sensitive to the nature of dark energy. However, for the first time, our simulations also reveal dependences of the normalization of the distribution on both redshift and cosmology that have been neglected in previous work. This introduces systematics that severely limit the usefulness of the original method. Guided by our simulations, we devise a new, improved test of the nature of dark energy. We demonstrate that this test does not require prior knowledge of the background cosmology and that it can even distinguish between models that have the same baryonic acoustic oscillations and dark matter halo mass functions. Our technique could be applied to the completed BOSS galaxy redshift survey to constrain the expansion history of the Universe to better than 2 per cent. The method will also produce different signals for dark energy and modified gravity cosmologies even when they have identical expansion histories, through the different peculiar velocities induced in these cases.