13

If I want to format a few lines, should I choose equalprg (using = operator) or formatprg (using gq operator)?

Both of them are for some kind of formatting. But what are the differences.

1 Answer 1

9

From :help equalprg:

'equalprg' 'ep'               string  (default "")
                      global or local to buffer |global-local|
      External program to use for "=" command. ...

From :help =:

={motion}               Filter {motion} lines through the external program
                        given with the 'equalprg' option.  When the 'equalprg'
                        option is empty (this is the default), use the
                        internal formatting function |C-indenting| and
                        |'lisp'|.  But when 'indentexpr' is not empty, it will
                        be used instead |indent-expression|.  When Vim was
                        compiled without internal formatting then the "indent"
                        program is used as a last resort.

I agree that it would be nice if the "intent" of the motion was written more clearly, but it does seem relatively clear that the intent of = and thus equalprg is indentation.


Similarly we can find from :help formatprg and :help gq that this is a more general purpose formatting feature. It allows to use external programs or internal methods based on the formatexpr and formatprg settings. With internal formatting it typically just reflows text within a given textwidth, i.e. the textwidth setting.

3
  • for vim scripts, what does gq do? It results in weird text (lines joined)
    – doraemon
    Nov 21, 2018 at 7:50
  • 1
    @LiuSha if formatexpr and formatprg are empty, then Vim formats the lines as if they were normal text. So the lines are joined to make a paragraph (width subject to textwidth, etc.).
    – muru
    Nov 21, 2018 at 8:04
  • 1
    @LiuSha which, to add to muru’s comment, makes gq bad for code without a good formatexpr or formatprg
    – D. Ben Knoble
    Nov 21, 2018 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.