Nitrogen-doped microporous carbon, which features a uniquely tunable porosity, exhibits promisingly high adsorption prospects in the greenhouse gas capture and water treatment. Herein, we propose a series of novel Ndoped microporous carbons (CMPAn-C1000), and precisely tune their porosities via a direct pyrolysis of rigid conjugated microporous poly(aniline)s (CMPAn, n = 1, 2, 3) with pore size difference in a molecular level, as achieved by simply tuning their linkers with varying sizes, planarity, and symmetry. The CMPA3-C1000, with a satisfactory amount of nitrogen heteroatoms, high SBET of similar to 800 m(2) g(-1), and large ultra-micropore volume of 0.25 cm(3) g(-1), has excellent CO2 capture ability (i.e., 5.1 mmol g(-1) at 273 K, 1 atm) and Hg(II) adsorption performance (i.e., Qe = 571 mg g(-1); k(2) = 0.0031 g mg(-1) min-1), respectively. CMPA3-C1000 might perform chemisorption towards Hg(II), according to calculations using density functional theory and experimental data; nitrogen atoms in porous carbon contributed to this excellent Hg(II) adsorption.