I want to make a mapping like this:
:inoremap foreach foreach() {<CR>}<ESC>k$3hi
The end result being that when I type:
foreach
I should get
foreach() {
}
Sounds more like you're reaching for an abbreviation rather than
a mapping.
:iab foreach foreach() {^M}^[O
where ^M is control+V followed by the <enter> key, and the ^[ is
control+V followed by the <esc> key (and that's an "oh", not a
zero. If you don't want a fresh blank line, on which to start,
you can change the "O" to an "I" (that's "capital oh" to "capital
eye" in the event fonts are ambiguous on your end of things)
With this mapping, you can type
foreach
followed by <space> or <tab> and it will automatically insert the
code as you describe.
It shouldn't have the same hidden/pause behavior that is
rightfully annoying you. :)
For more info, you can read about abbreviations (why is that such
a long word..."you can learn more about abrvs...") in the help under
:help :ab
(the short paragraph above this landing spot might also be
helpful to include in your reading)
HTH,
-tim