Nanotube encapsulation is a powerful technique for coaxing solids into unconventional configurations. By synthesizing materials within the interior confines of hollow nanotubes, lower dimensional morphologies, such as one-dimensional chains or nanoribbons, are favored. We have used carbon nanotube encapsulation to realize ultranarrow, atomically precise HfTe2 nanoribbons. A local, electron-beam-stimulated transition from the metallic 1T phase to the previously unreported semiconducting 1H phase is observed. We study computationally how charging can drive the phase transition and the stability of the different atomic configurations.