The Hamiltonian, considered as an operator, has two parts: one with a second-order spatial derivative, and one with simple, regular multiplication by So, the second-to-last line you could think of as this:

(substitution)

(distribute)

( does not depend on , hence its spatial derivative is zero.)

So there's the second-to-last line.

For the last line, just add another function in there:

(substitution)

(distribute)

(again, doesn't depend on )

(left- and right- factor: operators can be tricky!)

(substitute).

Make sense?