Kevin Taylor was on the Leeds-bound service when he missed the chance to get off at Normanton, so confronted the driver, Leeds Crown Court was told.
Prosecutor James Gelsthorpe said the train had set off from Castleford at 6.05pm on July 20, 2020 before making its usual stop at Normanton.
As the doors closed to set off, the driver said he heard shouting which he assumed was coming from the platform, but then the door to the cab opened and Taylor entered and began shouting and swearing at him and said: "Stop the train, I need to get off at Normanton because my pregnant girlfriend was at the platform."
The driver immediately applied the emergency brake but Taylor continued to be verbally abusive.
The train was delayed for 105 minutes and the total cost came to £1,521.
The train eventually returned to Castleford where Taylor was met by police.
He initially denied a charge of obstructing an engine but later changed his plea to guilty.
Taylor, of Montague Street, Agbrigg, appeared in court via video link from HMP Leeds and also admitted a charge of a racially-aggravated public order offence dating back to June 4 last year.
He had walked into the BP garage on Doncaster Road in Wakefield and was spotted on CCTV putting cheese into his bag. But when he was confronted by two members of staff, he began racially abusing them, telling them it was "his country" and they should "go back their own country", among other insults.
He also became physically aggressive during the confrontation, pushing his chest out, the court was told.
He was arrested and admitted taking the cheese, but initially denied being racially abusive. He was due to stand trial, but failed to turn up and was later arrested. He changed his plea at a later date.
However, he also burgled a ground floor flat in Lower York Street, Wakefield, on April 11 this year when the occupier left the window slightly ajar.
Taylor stole a laptop worth £200 and a security camera worth £20. His fingerprints were found at the scene. He admitted a charge of burglary.
The court heard that he has a staggering 104 convictions for theft, and 10 for drugs.
Mitigating, Jessica Heggie said Taylor had been a heroin addict for 22 years, but at the age of 40 had managed to conquer his habit.
She added: "In trying to keep away from heroin he was in drinking to excess. In effect, he swapped heroin for alcohol."
She said he was still clean from drugs in prison, and was working as a janitor in HMP Leeds, a trusted position.
He also has an NVQ in hairdressing and hopes to become a barber once released.
Miss Heggie said: "It's the first time in a long time that he is feeling stable.
"He is in the best position he has ever been, and it's a sorry state of affairs that he is remanded into custody.
"He is desperate not to lead the same life he has led since he was 18."
Judge Simon Phillips QC gave him a 27-month jail sentence and said: "You have an appalling record.
"The driver of the train was shocked and appropriately pulled the emergency brake.
"But for his training, there could have been a significant further degree of risk of danger to the train and its passengers.
"It was a highly dangerous action from you in drink. A deterrent sentence is necessary and unavoidable."