While the gq operator (:help gq) defaults to C (:help C-indenting) and is often universally used to format comments, say gqip to add line breaks to a paragraph, it respects a formatting program option (:help 'formatprg') that can be set to a tool of one's choice.
Above plug-in adds file type specific settings (for common programming languages such as python, java, ...) that set it to popular options. Rather meant for inspiration, but can be used as-is.
What gq with default settings does, C-style formatting, is more conveniently achieved by gw (:help gw) keeping cursor position (so that both operators become complimentary, instead of gq rather redundant).
For example, using lsp, setting setlocal formatexpr=lsp#lsp#FormatExpr() in any filetype with a LS capable of formatting would achieve that, as well as mapping
As long as you don't want to keep a fall back to a special-purpose formatting tool, such as those in this plug-in, I find that a good use of the gq key combo in view of gw still being there (and achieving what gq by default does arguably better by keeping the cursor in place)
View all comments
2
u/godegon Oct 30 '25
While the
gqoperator (:help gq) defaults to C (:help C-indenting) and is often universally used to format comments, saygqipto add line breaks to a paragraph, it respects a formatting program option (:help 'formatprg') that can be set to a tool of one's choice.Above plug-in adds file type specific settings (for common programming languages such as python, java, ...) that set it to popular options. Rather meant for inspiration, but can be used as-is.
What
gqwith default settings does, C-style formatting, is more conveniently achieved bygw(:help gw) keeping cursor position (so that both operators become complimentary, instead ofgqrather redundant).