In formal English, especially in writing, we sometimes change the normal order to make the sentence more emphatic. This structure is common with negative adverbial expressions such as not only, not until, never, no sooner.

When we use inversion after the above expressions, we change the order of the subject and auxiliary verb:

Never have I heard such a ridiculous idea

In the present simple and past simple tense, rather than simply inverting the subject and verb, we use do/does/ did + subject + main verb

Inversion is also used after the expressions only then, only when, hardly, scarcely, rarely

Not only did I pass my exam, but also I also got excellent marks

Only when you leave home  do you realize how expensive everything is

No sooner had the football match started than it began to snow heavily

Not until you become a parent yourself do you understand what it really means

Rarely have I read such a badly written novel