VALPARAÍSO, Chile — This glorious yet dysfunctional port has always harbored a darker alter ego, a streetwise subculture founded on poverty. Its credo, spat between gritted teeth, head cocked in vague menace, sends shivers down the spine of any well-to-do Chilean. “Soy un chorro porteño — I’m a punk from the docks.”

Just yards from the city’s quaint cobbled districts, whose artsy residents live amid shingle-walled antiques stores and quirky mansions painted in fanciful shades of indigo and mango, the downtrodden of Valparaíso inhabit a world of urban blight and decay.

Now, as the wrappers come off after a six-year, citywide revamp that looks set to spark an influx of tourists, a battle for the collective heart and mind of Chile’s greatest urban treasure has descended into cacophony.

Long-term residents and newcomers, developers and preservationists, national and local government, rich and poor — all have differing views on the kind of city they want to inhabit. “There are many different factions in the city, all of whom disagree on everything,” sighed a weary European resident.

The factions appear to agree on only one thing: Valparaíso is worth fighting for.

By rights, the city should be one of South America’s must-see destinations. Just 90 minutes from
Chile’s capital, Santiago, the UNESCO World Heritage Site and onetime home to
the late Nobel Laureate poet Pablo Neruda boasts a rugged, quirky beauty and a compelling history.

In the mid-19th century, the city’s name became synonymous with commercial success. As a stopover for ships sailing from Europe to California via the Magellan Strait
, it was home to banks and global businesses, the country’s first public library, and its oldest newspaper, El Mercurio de Valparaíso. It’s a matter of local pride that Chile’s first stock exchange was erected here, not in Santiago.

With prosperity, Valparaíso expanded upward from a narrow coastal plain, scaling a series of cerro hillsides that form a natural amphitheater above the Pacific shoreline. On sinuous cobbled streets, the wealthy erected ornate villas, each jostling with its neighbor for the better ocean view.

Even today, multicolored houses of clapboard and corrugated iron — materials flexible enough to withstand frequent earthquakes — twist with the contours up gulleys and down ravines. In places, the hills are so steep that footpaths resemble staircases; in others, they give way entirely to diminutive funicular railways, or ascensores, whose tiny wooden cabins and clanking machinery were built more than a century ago.

Colin Barraclough for the Boston Globe

With its 19th-century prosperity, Valparaiso expanded upwards from a narrow coastal plain, scaling a series of cerro hillsides that form a natural amphitheater above the Pacific shoreline.

The fall, when it came, was abrupt. In 1906, a powerful earthquake devastated the port, in four tumultuous minutes killing 4,000 and injuring five times as many. Eight years later, when the opening of the Panama Canal effectively closed the Magellan Strait shipping route, the banks closed shop and the businessmen left town.

At first, decline seemed quaint. Artists and bohemians arrived, turning warehouses into ateliers and investing the forlorn port with the kind of whimsical charm that drew Neruda (1904-73) to breezy Cerro Bellavista in 1959. After stuffing his chaotic house with antiques, the romantic poet even penned odes to the scruffiness he found outdoors, delighting in a trash-strewn staircase or a housewife’s clothes hung haphazardly to dry.

Walls of graffiti became part of the city’s aspect, particularly after Catholic University art students painted 20 abstract murals on Cerro Bellavista’s staircases, forming the Museo a Cielo Abierto, or Open-air Museum.

Even during Chile’s 1973-90 military dictatorship, Valparaíso cultivated a reputation as an artsy enclave, its dirty stairways symbolizing a kind of smudged romance.

But decline soon turned to decay. The twisting alleyways became clogged with trash, the abandoned mansions became a haven for addicts and thieves. The middle classes fled, taking their economic activity and tax contributions with them, leaving a financing burden no mayor could fix. “It’s quite simple,” the current mayor, Jorge Castro, told me. “Valparaíso is a city without income.”

Infested by rats, consumed by termites, racked by earthquakes, and burned by one of the highest arson rates in Chile, the city’s destruction was almost biblical in its theatricality. “Valparaíso burns easily,” a theater director said. “Things fall down, things collapse.”

In 2003, when UNESCO added Valparaíso to its World Heritage list, the decision seemed aimed more at kickstarting its recovery than rewarding local authorities for its preservation.

Spurred by the listing, Chile’s national government decided to act. In 2006, after securing funding from the Inter-American Development Bank, it set up the $73 million Valparaíso Recovery and Urban Development Program to overcome the city’s worst ills.

Over six years, the program has bought and restored landmark buildings, including the gloriously eclectic Palacio Baburizza and the art museum it contains. It has repainted private homes, installed street lighting, renewed paving, modernized trash collection, and refitted the scruffiest ascensores. It has even sterilized 18,000 stray dogs.

Yet such achievements have met with a chorus of disapproval. Some locals simply hoped for more widespread or profound change after investing “70 years of dreams in just one project,” as Castro put it.

Others — who relish the city’s seamier side — remained obstinately opposed to renewal. (The fiercest rejectionists counterattacked immediately, vandalizing newly restored ascensores within days of their unveiling.) “There are people who believe that the essence of Valparaíso is its scuzziness,” architect Antonio Menéndez said. “I don’t think they’re in the majority.”

The loudest protests have come from preservation-minded residents’ associations. So keen are they to protect every centennial nail and historic doorknob, complain architects involved in the city’s renewal, that many historic buildings are likely to collapse before they can be rescued.

“Surely it’s better to take some action rather than let the city fall into complete ruin?” said architect Mathias Klotz, who recently transformed a near-derelict Victorian-style mansion in Cerro Alegre into the Hotel Palacio Astoreca, its stucco-and-brick exterior newly washed in a startling red.

For the foreign visitor, the citywide spruce-up has been an unmitigated success. The reopening of the Fine Arts Museum in the Palacio Baburizza after its 15-year closure is huge, along with the revitalization of the adjacent Paseo Yugoslavo promenade and other nearby streets, with their antiques stores and artists’ studios.

“The conversion of Cerros Concepción and Alegre into a touristy, gentrified area is a hugely positive development,” said Matt Ridgway, a British property developer who restored a traditional Cerro Alegre townhouse. “Without these changes, many of the historic mansions would simply have fallen down.”

Refreshed public space has turned a simple stroll between cerros into a walker’s delight, the joy of discovery compounded, too, by the streets’ maze-like ability to confuse.

Follow an apparently blind alley and chances are it will open to a row of stucco-and-brick Victorian-style cottages, perfectly ordered and maintained, their facades washed in vibrant shades.

Skip down a winding staircase and a sliver of glinting ocean could flick into view in a gap between townhouses or a dazzling burst of orange materialize as a patch of flowering strelitzia in the tiniest of cliff-top gardens.

And on Cerro Cárcel, the city’s former prison has reopened as a world-class rehearsal and performance space for dance, music, and theater.

Valparaíso’s long-term future may remain uncertain, but for the first time in decades, the city seems able to breathe. “The Valparaíso of the past, a city more prosperous than the country’s capital, is dead,” Jacobo Ahumada, the city’s culture director, said. “We must search once again to find what we want to be.”