Douglas Lawrence inflicted 45 knife wounds on Carolyn Ellis, 32, and throttled her with a belt as neighbours in Tollcross, Edinburgh, tried to save the student.
The brutal attack left the broken tip of the knife embedded in Ms Ellis's head.
Another woman was stabbed in the arm before police arrived and sprayed Lawrence, 29, with CS gas.
He was accused of murdering Ms Ellis but at an earlier hearing at the High Court in Edinburgh his guilty plea to a reduced charge of culpable homicide was accepted.
He also admitted assaulting Pauline Smith by striking her with a knife, struggling with and kicking Christeen MacKenzie and punching Peter Scolley.
Lawrence further admitted attacking nursing assistant John Stewart on February 2 last year while he was being held in the State Hospital.
Yesterday, Lawrence's lifelong struggle with Asperger's Syndrome and schizophrenia was described in court.
Consultant Dr Ian Dewar said: "We know from Lawrence himself, psychiatric records and his family Lawrence has struggled with aggressive thoughts over many, many years."
The doctor said his treatment was going to be challenging and complicated and it was absolutely clear he should be kept in a high-security hospital.
Judge Lord Uist made an order keeping Lawrence in the State Hospital. He cannot leave without the agreement of the Scottish ministers. He said: "It is a tragic case all round."
Earlier, advocate depute Michael Stuart, prosecuting, told the court that despite his problems Lawrence obtained an honours degree in philosophy from Edinburgh University and seemed to be managing his difficulties.
Ms Ellis suffered from borderline personality disorder as a result of childhood experiences and met Lawrence when both were psychiatric out-patients. Mr Stuart said: "Around November 2010, the accused and Ms Ellis formed an intimate relationship. This was the accused's first, and only, intimate relationship."
Their mental health difficulties led to a split a year later.
Mr Stuart told the court that in November 2011 Lawrence had twice spoken of his thoughts of violence towards his ex-girlfriend to psychiatrists.
On the latter occasion a review of his case was fixed for February 12 last year.
But a month before that appointment, Lawrence went to Ms Ellis's flat. They talked for a while in her sitting room before she asked him to leave. Mr Stuart said: "He became angry and attacked the deceased."
Neighbours Ms Smith and Ms MacKenzie heard Ms Ellis shouting for help.
They opened their front door to see Lawrence with his arm round his ex-girlfriend's throat and him punching her head.
At this point they realised Lawrence had wrapped a belt round Ms Ellis's neck and was twisting the belt to tighten it, Mr Stuart told the court.
Ms MacKenzie was kicked to the floor as she tried to pull him away but their cries for help were heard by Mr Scolley, who helped wrestle the belt from Lawrence.
After punching Mr Scolley, Lawrence began kicking Ms Ellis and stamping on her head.
Lawrence disappeared into Ms Ellis's flat and came back with a knife which he began swinging at Ms Smith's head before gashing her right arm, leaving her scarred for life.
Mr Stuart described Lawrence holding the knife in both hands as he stabbed Ms Ellis's head.
He said: "The accused used such force that the tip of the knife broke off."
Police arrived at the flat to see, through the Perspex door, Lawrence continuing to stab Ms Ellis.
They gained entry and used CS gas to subdue him.