I read that the distribution defined as: $$ \lim_{\varepsilon \rightarrow 0}\frac{1}{x+i\varepsilon}$$ is equal to
$$p.v. \frac{1}{x} -i\pi \delta(x)$$
So that for any test function $f$, $$\lim_{\varepsilon\rightarrow 0} \int_{-\infty}^\infty \frac{f(x)dx}{x+i\varepsilon} = p.v.\int_{-\infty}^\infty \frac{f(x)}{x}dx - i\pi f(0).$$
But when I calculate the integral of $f(z)/z+i\varepsilon$ along this contour:

and then the limit $R\to \infty$, the result I arrive at is the following:
$$\lim_{\varepsilon\rightarrow 0} \int_{-\infty}^\infty \frac{f(x)dx}{x+i\varepsilon} = \lim_{\varepsilon\rightarrow 0} \left( \int_\varepsilon^\infty \frac{f(x+i\varepsilon)dx}{x+i\varepsilon} + \int_{-\infty}^{-\varepsilon} \frac{f(x+i\varepsilon)dx}{x+i\varepsilon} \right) -i \int_0^\pi f(\varepsilon e^{i \theta} -i\varepsilon) d\theta$$
I can more or less accept the second term being equal to $-i\pi f(0)$, but I don't see why the first one should be the principal value - the epsilons inside the integral are bugging me. But any change of variables, say $z=x+i\varepsilon$, would damage the contour so it would no longer look like a p.v. How can I retrieve the desired result from this?
Without doing any complex integration, we can easily see that
$$\lim_{\epsilon \to 0} \frac{1}{x + i\epsilon} = \lim_{\epsilon \to 0} \frac{x - i\epsilon}{x^2 + \epsilon^2} = \lim_{\epsilon \to 0} \frac{x }{x^2 + \epsilon^2} -i \lim_{\epsilon \to 0} \frac{\epsilon}{x^2 + \epsilon^2} $$
Applying the limit to the first term gives $\frac{1}{x}$, while the second limit is clearly a delta function (that hasn't been normalized), since
$$ \lim_{\epsilon \to 0^+} \frac{\epsilon}{x^2 + \epsilon^2} = \begin{cases} \infty & x = 0 \\ 0 & x \neq 0 \end{cases} $$
You can normalize the delta function by integrating over all space,
$$ \int_{-\infty}^{\infty} \frac{\epsilon}{x^2 + \epsilon^2} = \pi \quad \textrm{for } \epsilon > 0 $$
Which gives you the expression you need $$\lim_{\epsilon \to 0} \frac{1}{x + i\epsilon} = \frac{1}{x} -i\pi\delta(x)$$