C Rose wrote:
Hi
Is it possible to format a paragraph (i.e. justify it) of text or
comments while in insert mode? The way I do it now is to visually
highlight the paragraph and then press "g" and then "q".
What I'd like is to be able to press Ctrl-q (or something) to format the
paragraph (and the cursor left in the place it was before the command,
rather than at the start of end or something inconvenient).
Thanks in advance,
C
To format a paragraph, use gqap in Normal mode. To execute one
Normal-mode command from Insert-mode, use Ctrl-O. So: Ctrl-O gqap will
format the paragraph, but the cursor will move to the end of that
paragraph. Formatting the paragraph without moving the cursor relative
to the text is possible but trickier: mark your place, format, then go back:
:imap <F5> <C-O>m'<C-O>gqap<C-O>``
or
:imap <F5> <Esc>m'gqap``a
(This is two backticks after gqap.) I'm showing it with F5 because F
keys are not used by Vim for its "standard" functions (except F1 for
Help and sometimes F10 for Menu). You may use any keystroke(s) that Vim
will see, but watch out for collisions.
see
:help gq
:help objects
:help object-motions
:help mark-motions
:help i_CTRL-O
Best regards,
Tony.