Using vim, is there a command to have pasted text automatically line-wrapped? Using vim, is there a command to have pasted text automatically line-wrapped? ruby ruby

Using vim, is there a command to have pasted text automatically line-wrapped?


first do a set textwidth

:set tw=80

then do gqq - for a single line

for the whole file

ggVGgqq


Sure you can do this with:

:set wrap

This will show the text as wrapped without altering the underlying structure or inserting line breaks. It's sometimes also helpful to:

:set linebreak

This causes vim to wrap without breaking words.

It's also possible to:

:set wrapmargin

Which sets how far on the right wrapping should start.


vi, vim, and gvim support the 'ex' level commands:

:set ws wm=10

which sets a wrap margin at 10 characters from the right border and enforces a "wrap scan" - automatic wrapping as you type. This won't work for pasting text, though. For that, the 'fmt' command exists, which is native to Unix/Linux and supplied on Cygwin and GnuWin32 (see How do I get fmt-like functionality for Vim in Windows?)..

The "fmt" command provides a filter for reformatting existing text with word breaks, and it accepts a numeric flag (e.g., "-80") to specify line width. You can invoke this from within the vim editor, after pasting in the long lines.

You do:

!!fmt

to reformat a long line (keyboard shortcut for ex command ":.!fmt")

Or, to rewrap a whole paragraph:

!}fmt

from the paragraph's first line.

This should save you some time.