Adding cyanide vs nitrite for nucleophilic substitution and why does it change when we use silver salts for it On adding $\ce{NaCN}$ to $\ce{RX}$, we get

$$\ce{NaCN + RX -&gt; RCN + NaX}$$

And not $\ce{RNC}$. Clayden gives the explanation as follows:


  Although linear cyanide (which is isoelectronic with $\ce{N2}$) has a lone pair on nitrogen and a lone pair on carbon, the nucleophilic atom is usually anionic carbon rather than neutral nitrogen as the $\mathrm{sp}$ orbital on carbon is of higher energy than that on the more electronegative nitrogen, and therefore constitutes the HOMO.


So according to Clayden, Carbon being less electronegative, has a higher HOMO than Nitrogen, thus it's lone pair attack the LUMO, to form a bond as the less the difference in energy between HOMO and LUMO, the stronger is the bond 

But when we consider the same case with $\ce{NaNO2}$, so according to Clayden, nitrogen being less electronegative than oxygen, Nitrogen should have a higher energy HOMO and thus in a nucleophilic substitution reaction it is from the nitrogen side, that  the reaction should take place but ...
$$\ce{NaNO2 + RX -&gt; RONO + NaX}$$

The reaction takes place from the oxygen side. Why?

Also when we have the silver salts of them, e.g., $\ce{AgCN}$, the reaction becomes:
 $$\ce{AgCN + RX -&gt; RNC + AgX}$$
Similarly,
 $$\ce{AgNO2 + RX -&gt; RNO2 + AgX}$$

Why does it change?
Potassium cyanide ($\ce{KCN}$) is a predominantly ionic compound ($\ce{K+}$ is Hard Acid while $\ce{CN-}$ is Soft base) and would dissociate completely to give cyanide ions in solution. Cyanide ion ($\ce{CN-}$) is considered an ambient nucleophile, which means that the negative charge can associate with either carbon or nitrogen. However, the negative charge of cyanide ion concentrated on carbon than nitrogen. Thus, reaction with haloalkanes yields cyanides (nitriles) since $\ce{C-C}$ bond is thermodynamically more stable than $\ce{C-N}$ bond (simplest explanation). 

Silver cyanide ($\ce{AgCN}$) is largely covalent and it does not dissociate easily to give $\ce{CN-}$ due to soft-soft interaction ($\ce{Ag+}$ is Soft Acid and $\ce{CN-}$ is Soft base). Thus, negative charge of $\ce{CN-}$ ion is mostly available at nitrogen, and hence the attacking centre of the nucleophile will be $\ce{N}$-atom. As a result, you get nucleophiluc substitution of haloalkanes to give isocyanides (isonitrile).

Same argument can be given for nitrite formation with $\ce{KNO2}$ and nitro formation with $\ce{AgNO2}$. In this case, $\ce{KNO2}$ is a predominantly ionic compound ($\ce{K+^-O-N=O}$) where one of oxygen atoms of nitrite ion has negative charge. Therefore, the nucleophilic attack of $\ce{NO2-}$ on haloalkane would result alkyl nitrite ($\ce{R-O-N=O}$). Silver nitrite ($\ce{AgNO2}$) is, on the other hand, largely covalent and would not dissociate easily to give free $\ce{NO2-}$ ions. As a results, the lone pair on nitrogen acts as an attacking site, forming nitroalkanes (high electronegativity of oxygen compared to nitrogen prevented it from donating its lone pair, as explainnation given by Clayden).