Replacing large-scale fragments in human cells remains a substantial challenge. Here, we present a programmable gene replacement tool, named prime assembly (PA), which adapts prime editors to produce one or two pairs of 3′-flaps on both the genome and donor DNA. These 3′-flaps anneal to each other precisely, similar to Gibson assembly in DNA oligonucleotides, allowing megabase-scale genomic excision and/or kilobase-scale donor insertion at the gene of interest. PA accepts DNA plasmids and linear double-stranded DNA as donors, ranging from 1.0 to 6.5 kb in size. We demonstrate an efficiency of up to 57.8% in replacing endogenous sequences with a 2.9-kb donor DNA fragment in HEK293T cells, with an accuracy of >90% for integrated PA fragments. Furthermore, PA enables site-specific chimeric antigen receptor integration with up to 28.1% efficiency in primary human T cells. When PA containing a GFP donor is delivered to mice by hydrodynamic injection, an average integration efficiency of 4.3% is measured in GFP-positive hepatocytes. Large DNA fragments are annealed and inserted with prime editing.