views:

2138

answers:

4

Let's speak of relative measures. My Vim looks like:

aaaaaaaaaaaaa 
bbbbbbbbbbbbb 
ccccccccccccc 
etc

I would like it to be smaller:

aaaaa
aaaaa
bbbbb
bbbbb
ccccc
ccccc
etc

How can I get it? And how can I manage setting the length of such a block?

+7  A: 

Using fold(1) is one possibility:

:%!fold -w5

Result:

aaaaa
aaaaa
aaa 
bbbbb
bbbbb
bbb 
ccccc
ccccc
ccc
fgm
This assumes a `fold` command is installed.
Swaroop C H
+5  A: 
:set textwidth=30
Swaroop C H
How does textwidth handle it when there are no spaces, like in the original poster's example? I've had a play and it only seems to format based on textwidth if there are spaces (or assumedly, if the breakat variable is set to something else).
Andy
@Andy `textwidth` will not handle a no-spaces situation. You can write your own `formatexpr` function to do that.
Swaroop C H
Thanks, I'll have a look at formatexpr
Andy
+6  A: 

You can actually do two things:

  1. Let vim format (i.e.change) your text to have shorter lines, by inserting linebreaks
  2. Leave lines as they are, but display them wrapped

Which do you want?

  1. would be achieved by setting textwidth (see Swaarop's answer). Then you can reformat your text by marking it (visual mode) and typing gq.

  2. can be toggled by ":set wrap" / ":set nowrap"

Both are independent.

sleske
+1 for the gq key command from visual mode, thats awsomely useful
Fire Crow
+4  A: 

Once you set 'textwidth', you can select text with visual mode and press gq to wrap it nicely (you can also use Q on some older/legacy configurations).

A few useful tips:

gqgq (wrap the current line)
gq{ (wrap this 'paragraph', i.e. until the next blank line)
:h hq
ddvlad