We obtain a formula for the Heegaard Floer homology (hat theory) of the three-manifold $Y(K_1,K_2)$ obtained by splicing the complements of the knots $K_isubset Y_i$, $i=1,2$, in terms of the knot Floer homology of $K_1$ and $K_2$. We also present a few applications. If $h_n^i$ denotes the rank of the Heegaard Floer group $widehat{mathrm{HFK}}$ for the knot obtained by $n$-surgery over $K_i$ we show that the rank of $widehat{mathrm{HF}}(Y(K_1,K_2))$ is bounded below by $$big|(h_infty^1-h_1^1)(h_infty^2-h_1^2)- (h_0^1-h_1^1)(h_0^2-h_1^2)big|.$$ We also show that if splicing the complement of a knot $Ksubset Y$ with the trefoil complements gives a homology sphere $L$-space then $K$ is trivial and $Y$ is a homology sphere $L$-space.