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.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityFrom :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.
gq
do? It results in weird text (lines joined)
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.).
gq
bad for code without a good formatexpr
or formatprg