hard. True ~
story. ~
Note: there is a way to do automatic formatting for specific types of text
layouts, see |auto-format|.
Since "gq" is an operator, you can use one of the three ways to select the
text it works on: With Visual mode, with a movement and with a text object.
The example above could also be done with "gq4j". That's less typing, but
you have to know the line count. A more useful motion command is "}". This
moves to the end of a paragraph. Thus "gq}" formats from the cursor to the
end of the current paragraph.
A very useful text object to use with "gq" is the paragraph. Try this: >
gqap
"ap" stands for "a-paragraph". This formats the text of one paragraph
(separated by empty lines). Also the part before the cursor.
If you have your paragraphs separated by empty lines, you can format the
whole file by typing this: >
gggqG
"gg" to move to the first line, "gqG" to format until the last line.
Warning: If your paragraphs are not properly separated, they will be joined
together. A common mistake is to have a line with a space or tab. That's a
blank line, but not an empty line.
Vim is able to format more than just plain text. See |fo-table| for how to
change this. See the 'joinspaces' option to change the number of spaces used
after a full stop.
It is possible to use an external program for formatting. This is useful
if your text can't be properly formatted with Vim's builtin command. See the
'formatprg' option.
==============================================================================
*25.2* Aligning text
To center a range of lines, use the following command: >
:{range}center [width]
{range} is the usual command-line range. [width] is an optional line width to
use for centering. If [width] is not specified, it defaults to the value of
'textwidth'. (If 'textwidth' is 0, the default is 80.)
For example: >
:1,5center 40
results in the following:
I taught for a while. One ~
time, I was stopped by the ~
Fort Worth police, because my ~
homework was too hard. True ~
story. ~
RIGHT ALIGNMENT
Similarly, the ":right" command right-justifies the text: >
:1,5right 37
gives this result:
I taught for a while. One ~
time, I was stopped by the ~
Fort Worth police, because my ~
homework was too hard. True ~
story. ~
LEFT ALIGNMENT
Finally there is this command: >
:{range}left [margin]
Unlike ":center" and ":right", however, the argument to ":left" is not the
length of the line. Instead it is the left margin. If it is omitted, the
text will be put against the left side of the screen (using a zero margin
would do the same). If it is 5, the text will be indented 5 spaces. For
example, use these commands: >
:1left 5
:2,5left
This results in the following:
I taught for a while. One ~
time, I was stopped by the ~
Fort Worth police, because my ~
homework was too hard. True ~
story. ~
JUSTIFYING TEXT *justify* *:Justify* *Justify()* *package-justify*
Vim has no built-in way of justifying text. However, there is a neat macro
package that does the job. To use this package, execute the following
command: >vim
:packadd justify
Or put this line in your |vimrc|: >vim
packadd! justify
This Vim script file defines a new visual command "_j". To justify a block of
text, highlight the text in Visual mode and then execute "_j".
Look in the file for more explanations. To go there, do "gf" on this name:
$VIMRUNTIME/pack/dist/opt/justify/plugin/justify.vim.
An alternative is to filter the text through an external program. Example: >
:%!fmt
==============================================================================
*25.3* Indents and tabs
Indents can be used to make text stand out from the rest. The example texts
in this manual, for example, are indented by eight spaces or a tab. You would
normally enter this by typing a tab at the start of each line. Take this
text:
the first line ~
the second