Yemeni President Ali Abdullah Saleh has signed a deal under which he will step down after months of unrest.

Mr Saleh signed the agreement, brokered by Yemen’s Gulf Arab neighbours, in the Saudi capital, Riyadh.

Under the plan, he will transfer his powers to his deputy ahead of an early election and in return will get immunity from prosecution.

But protesters rallying in the Yemeni capital, Sanaa, said they would reject any deal giving the president immunity.

The demonstrators said the Gulf initiative ignored the “blood of martyrs”, BBC Arabic correspondent Abdullah Ghorab in Sanaa reports.

A crackdown on anti-government protests, which began in February, has left hundreds of people dead and thousands wounded in Yemen.

The 69-year-old leader – who has ruled since 1978 – came close to signing the deal several times in the past, only to pull out at the last minute.

Meanwhile, clashes broke out between pro-Saleh troops and gunmen loyal to dissident chief Sheikh Sadiq al-Ahmar in Sanaa on Wednesday.

No casualties were immediately reported in the fighting in Sanaa’s al-Hasaba district.

Mr Saleh signed the agreement in the presence of Saudi King Abdullah and other senior Saudi officials after flying to Riyadh on Wednesday morning.

Under the plan, the president will hand over power to deputy Abdrabuh Mansur Hadi in return for immunity from prosecution.

Mr Hadi is then expected to form a national unity government and also call for early presidential elections within 90 days.

The deal envisages that Mr Saleh will remain an honorary president for three months after signing the agreement.

In Riyadh, Mr Saleh pledged to co-operate with the new government which would include the opposition.

He also called on all Yemenis to be partners in rebuilding the conflict-torn country.

The breakthrough comes after intensive talks in Yemen by the UN envoy to the country, Jamal Benomar.