The limited potassium-ion intercalation capacity of graphite hampers development of potassium-ion batteries (PIB). Edge-nitrogen doping is an effective approach to enhance K-ion storage in carbonaceous materials. One shortcoming is the lack of precise control over producing the edge-nitrogen configuration. Here, a molecular-scale copolymer pyrolysis strategy is used to precisely control edge-nitrogen doping in carbonaceous materials. This process results in defect-rich, edge-nitrogen doped carbons (ENDC) with a high nitrogen-doping level (up to 10.5 at %) and a high edge-nitrogen ratio (87.6 %). The optimized ENDC exhibits a high reversible capacity of 423 mAh g−1, a high initial Coulombic efficiency of 65 %, superior rate capability, and long cycle life (93.8 % retention after three months). This strategy can be extended to design other edge-heteroatom-rich carbons through pyrolysis of copolymers for efficient storage of various mobile ions.