The importance and the difficulty of achieving good B0 homogeneity over the volume of interest in in vivo NMR spectroscopy are well known. A fast, reliable, versatile, and fully automatic shimming procedure has been developed, using 3D chemical-shift imaging to measure the field distribution using only the water peak in the sample of interest. The procedure minimizes the mean-square error in the field distribution with respect to a constant field where the signal exists (e.g., the head). This produces a set of linear equations that can be solved by standard matrix methods. The procedure has been applied on a commercial imager producing water linewidths from the entire head as low as 8 Hz at 1.5 T. The mean linewidth and standard deviation from 94 head studies were 11.7 +/- 1.9 Hz.