The ground decided the city's shape. When Ice Age glaciers pushed in from the west, Castle Rock split the ice, which scooped out a hollow on three sides and left a tail of rubble trailing east. People camped at Cramond around 8500 BC. The rock was Din Eidyn, chief stronghold of the Gododdin, until Northumbrian forces besieged it in 638 and the Angles took Lothian.
Around 950 the fortress passed to the Scots, and King David I founded the royal burgh in the early 12th century. The town grew down the rubble tail, which became the Royal Mile. English armies held it for long stretches during the Wars of Independence and burned it in 1544. Hemmed in by its walls, Edinburgh built upwards: tenements commonly reached 11 storeys or higher, and residents emptied their slops from the windows with a shout of "Gardyloo". The smell of sewage and coal smoke earned it the name Auld Reekie.
Mary, Queen of Scots came back from France in 1561 as a Catholic queen in a Protestant town, and John Knox preached against her from St Giles. After the murders at Holyrood she was forced to abdicate in 1567. The town council then got a royal charter in 1582 for what became the University of Edinburgh.
Power drained south in two steps. In 1603 James VI inherited the English throne, and in 1707 the Scottish Parliament merged into the new one at Westminster, despite riots in Edinburgh's streets. The city lost its political weight. It found another kind.
In the late 18th century David Hume, Adam Smith and the geologist James Hutton walked the same streets, and the city's learning and neoclassical buildings earned it the name Athens of the North. In 1766 a 27-year-old architect, James Craig, won the competition to plan a New Town on the ridge north of the castle. Its street names, George, Queen, Hanover and Princes, were a loyal nod to the new royal family. The Nor Loch between the towns, by then a sewage dump, was drained by the 1820s, and the earth dug for New Town foundations became the Mound.
From the 1770s the professional classes left the Old Town for the New. Glasgow outgrew Edinburgh by 1821, though printing, brewing and distilling kept the capital busy. Lord Provost William Chambers began rebuilding the Old Town in the 1860s, so much of what you see along the Royal Mile today is Victorian rather than medieval.
Edinburgh stayed Scotland's cultural capital, and in 1947 it launched the International Festival. Since 1999 it has been a seat of government again, with a devolved Scottish Parliament that moved into Enric Miralles's building by Holyrood in 2004. The city is also the UK's second financial centre after London, its offices spreading west of the castle into old brewery land at Fountainbridge.