steve_gallagher
steve_gallagher

Reputation: 3898

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

CONTEXT: Part of a job I'm doing involves pasting paragraphs of text from a word doc into a ruby file.

PROBLEM: These paragraphs are getting pasted in as a single very long line of text and I have to manually insert newlines to make the lines of reasonable length.

SOLUTION: Is there a way to make the pasting function "aware" of reasonable margin limits and wrap the text when I paste it in?

Upvotes: 11

Views: 1753

Answers (4)

user2093625
user2093625

Reputation: 31

I typically have a need to import text and then have to wrap the whole document: I use:

:g/./normal gqq

Hope that helps.

Upvotes: 3

blackcatweb
blackcatweb

Reputation: 1013

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.

Upvotes: 4

srini.venigalla
srini.venigalla

Reputation: 5145

first do a set textwidth

:set tw=80

then do gqq - for a single line

for the whole file

ggVGgqq

Upvotes: 15

Benj
Benj

Reputation: 32398

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.

Upvotes: 4

Related Questions