Line editing in the original ksh is quite limited. You may want to use one of its "clones" like zsh¹ or bash instead.
For ksh93, I find that if you use the
vi editing mode (
ksh -o vi) as opposed to the
gmacs one, then recalling multiline commands does work, though the newline characters are rendered as
^J. That's also what you get with
pdksh and its derivatives in all editing modes.
Note that you can also enter those newline characters with Ctrl+VCtrl+J for that multi-line code to be entered on one line and then be able to recall it later even in
Another option is to use
fc to edit the previous line in a real text editor:
Would call your preferred editor (in
$FCEDIT, defaults to
ex on my system) on the last (possibly multiline) command line that contained
while. And then run it after you save and exit in the editor.
zsh also has a
ksh emulation mode that makes it behave more closely to
ksh in case there are some
ksh behaviours you do rely on (entered with
emulate ksh or when invoking