Switching around the normal word order is called inversion, and this specific type is called subject-auxiliary inversion. Wikipedia has a list of usages of subject-auxiliary inversion, including interrogative constructions (e.g. Did you eat?), but the following is the declarative section:
Declarative sentences with negative elements (i.e. never or not) are formed. See also Negative inversion.
- Example #1: Never again shall I watch that opera!
- Example #2: Not since childhood did she eat cotton candy.
Declarative sentences with restrictive elements (i.e. only or so) are formed.
- Example #1: Only on Fridays does he go to the bar.
- Example #2: So hard did she work that she overslept the next day.
- Example #3: So did I.
I found a blog called Practice English which has a laudably comprehensive post on the topic of inversion:
In statement it is usual for the verb to follow the subject, but sometimes this word order is reversed.
We can refer to this as inversion. There are two main types of inversion:
- when the verb comes before the subject (optional inversion)
In the doorway stood her father. (or …her father stood.)
- when the auxiliary comes before the subject and the rest of the verb
phrase follows the subject (inversion is usually necessary)
Rarely had he seen such a sunset. (not Rarely he had seen…)
Inversion brings about fronting, the re-ordering of information in a sentence
to give emphasis in a particular place. Often this causes an element to be
postponed until later in the sentence, focusing attention on it.
- Inversion after negative adverbials
When we begin a sentence with a negative adverb or adverbial phrase,
we sometimes have to change the usual word order of subject and
verb (often using an auxiliary verb) because we want to emphasise
the meaning of the adverb. We use inversion when we move a negative adverb
which modifies the verb (never, nowhere, not only, hardly etc.) to the beginning
of a sentence. For example:
I had never seen so many people in one room. (= normal word order)
Never had I seen so many people in one room. (= inversion)
There are adverbs and adverbial expressions with a negative,
restrictive or emphatic meaning, which are followed by inversion
when placed first in a sentence. The most common adverbs ad adverbial
expressions with negative, restrictive or emphatic meaning that are
followed be inversion are:
Seldom, Rarely, Little, Nowhere, Nor even one, In no way
Scarcely/Hardly/Barely … when, No sooner … than, Not only … but (also)
On no occasion/account/condition, In/Under no circumstances
Only after, Only later, Only once, Only in this way, Only by,
Only then, Only when, Only if, Not till/until, Never, Never
before, Not since, Neither/Not/So, Well (formal) etc:
This is only the first 15% or so. Though not the highest quality of writing (it contains a few typos, etc), IMO it represents the contexts of proper inversion admirably well and staggeringly comprehensively.
The only real (albeit minor) disagreement I have seen that I have with it involves the following:
We can put the verb before the subject when we use adverbs expressing
direction of movement, such as along, away, back, down, in, off, out, up with verbs such as come, fly, go. This pattern is found
particularly in narrative, to mark a change in events:
The door opened and in came the doctor. (less formally …and the doctor came in)
As soon as I let go of the string, up went the balloon, high into the sky. (less formally …the balloon went up)
Just when I thought I’d have to walk home, along came Miguel and he gave me a lift. (less formally …Miguel came along and gave me …)
As far as I have seen, it's not necessarily formal to say in came the doctor - in fact, the doctor came in seems more consistent with a formal context. (It also could be that the author meant to say less informally, and if so, I'd have agreed completely).