Edward Sutherland threatened to stab the 20-year-old through the eye and kill her as she tried to fight him off.
Sutherland, 27, who has 58 previous convictions and was assessed as a very high risk of re-offending, struck a month after he was freed on early release from a previous jail sentence.
Lord Burns told him at the High Court in Edinburgh: "It plainly was a terrifying experience for that girl, having regard to the nature of your attack on her and the prolonged time over which it took place."
The judge ordered Sutherland should be placed on supervision for a further three years. He was also put on the Sex Offenders' Register.
The attack happened on October 25, last year.
The woman had gone to buy groceries and as she walked along the street heard a noise coming from bushes and found Sutherland eating food.
As she returned from the shop he was still there and he struck up a conversation with her.
He introduced himself with a false name and began to walk with her, eating a tortilla wrap that he later discarded.
It was later recovered with other items by police and was found to have a DNA match with Sutherland.
The woman told him she should not be talking to him, but Sutherland, of no fixed abode, told her to come with him or he would stab her.
He grabbed her arms and said: "Don't shout, don't scream. I'll kill you. Get against the wall."
A struggle ensued and Sutherland brought out a pen and pointed it at the woman's neck and said: "I'll kill you if you don't do as I say."
She still struggled and shouted for help and rang the doorbell of a flat. She broke free and ran into the flat as Sutherland fled.
Days later, a plainclothes police patrol saw Sutherland in Great Western Road and he was detained.