How do you format (rewrap) a visual selection to textwidth?
gq in visual mode
Select text with v or V, then press gq to format the selection according to textwidth.
16 results for "gq format text"
gq in visual mode
Select text with v or V, then press gq to format the selection according to textwidth.
:set formatprg=<cmd>
When formatprg is set, the gq operator pipes the selected text through that external program and replaces it with the program's output.
:set formatprg={program}
Vim's gq operator normally reflows text to fit textwidth, but by setting formatprg you can delegate formatting to any external tool — a language formatter, a
gq (visual mode)
Pressing gq on a visual selection reformats the selected lines to hard-wrap at textwidth columns.
:set formatexpr=MyFormat()
The formatexpr option lets you replace Vim's built-in line-breaking logic for the gq operator with your own Vimscript expression or function.
gw{motion}
The gw operator reformats text just like gq, but leaves the cursor exactly where it started.
gwip
The gw operator reformats text just like gq, but leaves the cursor in its original position after reformatting.
gq{motion}
The gq{motion} operator reformats text to fit within Vim's textwidth setting, inserting hard line breaks where lines are too long.
gqap
The gq operator reformats text by wrapping lines to fit within the textwidth setting.
vim.lsp.buf.format()
Neovim's built-in LSP client exposes vim.
gqip
The gqip command reformats the current paragraph to fit within the configured textwidth.
gw
The gw operator reformats text to fit within textwidth, just like gq — but with one key difference: gw leaves the cursor in its original position after format
:set formatoptions
The formatoptions setting is a string of single-character flags that governs Vim's automatic text formatting: when lines wrap, whether comment syntax continues
gwap
The gw operator reformats text to fit within 'textwidth' — identical in effect to gq, but with one key difference: the cursor returns to its original position
:set equalprg={program}
The equalprg option replaces Vim's built-in = indentation operator with any external formatting program.
:set formatoptions+=a
Vim's formatoptions setting controls how automatic text formatting works.