CRIME writer Ian Rankin has revealed he almost killed off Inspector John Rebus at the end of his first book in the series.

Rankin, 61, said he only changed his mind about killing Rebus when he began typing up the book because readers ‘don’t like it when the hero dies’.

The revelation comes as the National Library of Scotland prepares to lift the lid on Rankin’s archive, which spans five decades and fills 77 boxes in the collection centre.

The haul includes an unpublished first novel by Rankin, written before he created his inspector character, as well as the original manuscripts for the detective series.

But a crucial part of the Rebus story is missing - a handwritten manuscript from the first novel, Knots and Crosses, in which Rankin killed the Edinburgh policeman.

Dad-of-two Rankin said: “The original ending has just been lost in the mists of time.

“I think I changed the ending very early on, possibly before I got round to typing up the book.

“I used to write in longhand.

“I thought I wasn’t going to use the character again and that he might as well die - it would have been a surprise for everybody if the hero died.

“But then I thought that nobody likes it if the hero dies.

“It never got as far as going to an editor.

“I decided to change it before I typed up the book, edited it and sent it off into the wilderness.”

The Rankin exhibition comes after the author donated his archives when he “downsized” to a new home in Edinburgh last year.

The material also includes song lyrics for his band Dancing Pigs, poems and music reviews of acts like Madonna and R.E.M.

Archive curator, Rosemary Hall, read her first Rebus novel, Black And Blue, when she was at high school in Maryland, in the United States and landed the job last year.

Ms Hall, who studied Scottish culture and literature at Edinburgh and St Andrews universities, said: “I was a big fan of Ian?s writing, although I hadn’t read all of the Rebus books, and I’d studied his work at university.

“But I was also really interested in the nature of the job in going through an entire archive from start to finish and eventually making something so important available to everybody.

“One of the most interesting things was how much research he did for each book.

“Sometimes he would spend a good year before he started writing, including researching local dialects for characters and making contact with police officers to get their procedures right.

“He really cared about everything being accurate.

“He has been writing for so long and is so successful, but is obviously still so meticulous.

“I also came across his hand-written lyrics for his band Dancing Pigs, which are a lot of fun, and a track listing for an album that was never released - my favourite song title was Anarchy in Cowdenbeath.

“One of my favourite items is a big orange notebook from around 1990-91.

“On the front of it he has written: ‘This book belongs to Ian Rankin the writer. It’s his big book of ideas so no peeking.’

“What a lot of people will find interesting are his thoughts on crime fiction as a genre.

“He was not exactly happy.

“He had some very strong opinions at the time.

“He did not feel that crime fiction was really respected enough and wanted to change that.

“He didn’t set out to become a crime writer.

“He always wanted to be taken seriously as a literary writer.

“He has worked really hard to make crime fiction respected as a genre.?