The stable operation of quantum computers will rely on error correction, in which single quantum bits of information are stored redundantly in the Hilbert space of a larger system. Such encoded qubits are commonly based on arrays of many physical qubits, but can also be realized using a single higher-dimensional quantum system, such as a harmonic oscillator1-3. In such a system, a powerful encoding has been devised based on periodically spaced superpositions of position eigenstates4-6. Various proposals have been made for realizing approximations to such states, but these have thus far remained out of reach7-11. Here we demonstrate such an encoded qubit using a superposition of displaced squeezed states of the harmonic motion of a single trapped 40Ca+ ion, controlling and measuring the mechanical oscillator through coupling to an ancillary internal-state qubit12. We prepare and reconstruct logical states with an average squared fidelity of 87.3 ± 0.7 per cent. Also, we demonstrate a universal logical single-qubit gate set, which we analyse using process tomography. For Pauli gates we reach process fidelities of about 97 per cent, whereas for continuous rotations we use gate teleportation and achieve fidelities of approximately 89 per cent. This control method opens a route for exploring continuous variable error correction as well as hybrid quantum information schemes using both discrete and continuous variables13. The code states also have direct applications in quantum sensing, allowing simultaneous measurement of small displacements in both position and momentum14,15.