Last Updated: December 10, 2020
·
23.5K
· Chip Castle

vim paragraph formatting to wrap at 80 characters

Forcing selected text to be limited to 80 characters within vim is fairly straightforward.

Just do this:

  1. Set the text width.
  2. Select desired lines.
  3. Reformat selected text.

Here's the breakdown in more detail.

From within vim, first set the text width setting so that vim knows how to format the selected text:

:set textwidth=80

Selecting the text can be done a number of ways, so for this example, let's say my cursor was on the first line of a paragraph that I wanted to format. The cursor can be on any column of that line for this to work. I would then do this to select that paragraph:

Shift-v }

Then I would reformat that paragraph, by just typing this:

gq

For more UNIX tips, please check out
Learning the UNIX Command Line.

Enjoy,
Chip

Related protips:

Basic Vim commands - For getting started

1 Response
Add your response

Or if the paragraph is separated with newlines simply "gqad" will do it without selecting anything.

But thanks for the Shift-v way, I was looking for that, since often it arent actually well separated paragraphs due to HTML or LaTeX encodings around.

over 1 year ago ·