Home Explore Blog CI



neovim

20th chunk of `runtime/doc/change.txt`
892aabacfb38c57716fc1aa194b493cc9b490f42334bf5e40000000100000fa0
	Format the highlighted text as with "gw".  (for
			{Visual} see |Visual-mode|).

Example: To format the current paragraph use:			*gqap*  >
	gqap

The "gq" command leaves the cursor in the line where the motion command takes
the cursor.  This allows you to repeat formatting repeated with ".".  This
works well with "gqj" (format current and next line) and "gq}" (format until
end of paragraph).  Note: When 'formatprg' is set, "gq" leaves the cursor on
the first formatted line (as with using a filter command).

If you want to format the current paragraph and continue where you were, use: >
	gwap
If you always want to keep paragraphs formatted you may want to add the 'a'
flag to 'formatoptions'.  See |auto-format|.

If the 'autoindent' option is on, Vim uses the indent of the first line for
the following lines.

Formatting does not change empty lines (but it does change lines with only
white space!).

The 'joinspaces' option is used when lines are joined together.

You can set the 'formatexpr' option to an expression or the 'formatprg' option
to the name of an external program for Vim to use for text formatting.  The
'textwidth' and other options have no effect on formatting by an external
program.

                                                        *format-formatexpr*
The 'formatexpr' option can be set to a Vim script function that performs
reformatting of the buffer.  This should usually happen in an |ftplugin|,
since formatting is highly dependent on the type of file.  It makes
sense to use an |autoload| script, so the corresponding script is only loaded
when actually needed and the script should be called <filetype>format.vim.

For example, the XML filetype plugin distributed with Vim in the
$VIMRUNTIME/ftplugin directory, sets the 'formatexpr' option to: >

   setlocal formatexpr=xmlformat#Format()

That means, you will find the corresponding script, defining the
xmlformat#Format() function, in the file `$VIMRUNTIME/autoload/xmlformat.vim`

Here is an example script that removes trailing whitespace from the selected
text.  Put it in your autoload directory, e.g. ~/.vim/autoload/format.vim:
>vim
  func! format#Format()
    " only reformat on explicit gq command
    if mode() != 'n'
      " fall back to Vim's internal reformatting
      return 1
    endif
    let lines = getline(v:lnum, v:lnum + v:count - 1)
    call map(lines, {key, val -> substitute(val, '\s\+$', '', 'g')})
    call setline('.', lines)

    " do not run internal formatter!
    return 0
  endfunc

You can then enable the formatting by executing: >
  setlocal formatexpr=format#Format()

Note: this function explicitly returns non-zero when called from insert mode
(which basically means, text is inserted beyond the 'textwidth' limit).  This
causes Vim to fall back to reformat the text by using the internal formatter.

However, if the |gq| command is used to reformat the text, the function
will receive the selected lines, trim trailing whitespace from those lines and
put them back in place.  If you are going to split single lines into multiple
lines, be careful not to overwrite anything.

If you want to allow reformatting of text from insert or replace mode, one has
to be very careful, because the function might be called recursively.  For
debugging it helps to set the 'debug' option.

							*right-justify*
There is no command in Vim to right justify text.  You can do it with
an external command, like "par" (e.g.: `:.,}!par` to format until the end of the
paragraph) or set 'formatprg' to "par".

							*format-comments*
An overview of comment formatting is in section |30.6| of the user manual.

Vim can automatically insert and format comments in a special way.  Vim
recognizes a comment by a specific string at the start of the line (ignoring
white space).  Three types of comments can be used:

- A comment string that repeats at the start of each line.  An example is the
  type of comment used in shell scripts, starting with "#".
- A comment string that occurs

Title: Vim: Formatting Paragraphs, `formatexpr` Option, and Comment Formatting
Summary
This section explains how to format paragraphs in Vim using commands like `gqap` and `gwap`. It details how the cursor position is affected by these commands and how the 'formatoptions' setting can be used for automatic formatting. The section also covers the `formatexpr` option, which allows users to specify a Vim script function for custom reformatting, and provides an example script for removing trailing whitespace. Additionally, it touches upon right-justifying text and formatting comments, including how Vim recognizes comments through specific start-of-line strings.