The formation of new memories requires transcription. However, the mechanisms that limit signaling of relevant gene programs in space and time for precision of information coding remain poorly understood. We found that, during learning, the cellular patterns of expression of early response genes (ERGs) are regulated by class IIa HDACs 4 and 5, transcriptional repressors that transiently enter neuronal nuclei from cytoplasm after sensory input. Mice lacking these repressors in the forebrain have abnormally broad experience-dependent expression of ERGs, altered synaptic architecture and function, elevated anxiety, and severely impaired memory. By acutely manipulating the nuclear activity of class IIa HDACs in behaving animals using a chemical-genetic technique, we further demonstrate that rapid induction of transcriptional programs is critical for memory acquisition but these programs may become dispensable when a stable memory is formed. These results provide new insights into the molecular basis of memory storage.