Paul Knipe, 44, struck South African holidaymaker Lyle Thole several times as he was returning to his hotel in York, leaving him needing 30 stitches.
He told 78-year-old Mr Thole he was mentally ill and was going to kill him during the attack in July last year.
Knipe was sentenced to imprisonment for public protection at York Crown Court today after earlier pleading guilty to attempted murder.
Judge Stephen Ashurst, the Recorder of York, told him he would serve at least four-and-a-half years in jail before being considered for release.
“You attacked a completely blameless stranger, who you had met near York Minster when Mr Thole was on holiday from South Africa,” he said.
“The attack on Mr Thole was an extremely dangerous one.”
Judge Ashurst continued: “You picked up a cobble stone and struck him repeatedly around his head. He was in a terrible state and had shed a significant amount of blood.”
He added: “It’s fortunate indeed that a number of members of the clergy at York Minster were on hand and you did not persist with the attack.
“You made it clear that God was telling you to kill this random stranger.”
Mr Thole, who was on a golfing holiday at the time, received injuries to his head, elbow and fingers and was said to have been left traumatised by the attack.
He was returning to his hotel at around 8.30pm on July 1 last year when he took a wrong turning near York Minster and found himself at a dead end.
As he turned around, Knipe, who had been following him for around 15 to 20 minutes, struck him to the back of the head with a cobble.
He continued the attack, striking him a further three or four times, while Mr Thole was on his hands and knees.
When he asked Knipe what he wanted, the defendant replied: “I’m mentally ill and I’m going to kill you.”
A witness described Knipe as speaking in a “menacing but controlled manner” as he heard him say: “One more blow and I will kill you.”
Nicholas Barker, prosecuting, said: “The defendant was described as having a face that looked dead, with a vacant look.
“Another witness described him as being in a trance-like state.”
Mr Barker said a member of the clergy asked Knipe what had happened.
He said: “The defendant replied: ‘I attacked him.’ When asked why, he said: ‘Because God told me to.’.”
Knipe, of Spring Hill, Tadcaster, North Yorkshire, was arrested and gave a prepared statement to police, in which he said: “I have been hearing voices for around a year, telling me to kill people. I have told my psychiatrist on a number of occasions about these voices but they’ve told me as long as I don’t act on them, everything will be ok.”
He added: “Last night, the voices kept telling me to hurt somebody and if I did, they would leave me alone. I saw a man walking and I hit him with a stone.”
David Dixon, defending, said Knipe led a “normal, responsible and caring life” but had changed after his mother died a few years ago.
He had been receiving treatment for mental health problems.
The court heard Knipe was detained under the Mental Health Act in April last year after he told a police officer he was going to be on the front page of newspapers because he was going to kill someone.
Mr Barker said he told the officer he carried a knife at night and hid in bushes because he wanted to attack women.
He also claimed to have strangled a nurse in hospital.
Judge Ashurst said he believed Knipe was a risk to members of the public and would remain in prison until his mental health improved.
Knipe, who stood in the dock flanked by staff from a mental health unit, looked straight ahead and showed no emotion as he was sentenced.
Detective Chief Inspector Nigel Costello, of North Yorkshire Police, said: “The attack was a nasty and vicious assault on an innocent member of the public.
“Such was the ferocity of the assault only the fitness and good health of the victim prevented the injuries from causing more serious complications.
“We are pleased Knipe pleaded guilty in November, sparing the victim the ordeal of a trial. He now has to face up to the consequences of what he has done.
“I would also like to add my thanks to the members of the public who came to the victim’s aid and those who witnessed the incident and came forward to assist the police investigation.”