Saturday, January 10, 2009

Reformat a Paragraph

Say you have the following text:

> someone said
> > someone else said
> > > long quoted message
> > blah blah
> blah

Place the cursor on the first line and do `gqip'. I often use this combo to conveniently reformat paragraphs.

13 comments:

Anonymous said...

Didn't work with your posted example.

Sankar said...

I got gqip command not found error.

Ben said...

(the example worked for me.)

I know gq"movement" will re-justify text for me, what's the breakdown of what gqip command is saying?

Stefan said...

I don't understand. What should happens?
This example does nothing for me:
> someone said
> > someone else said
> > > long quoted message
> > blah blah
> blah
ESC gqip

Anonymous said...

The command works, but the example text is misleading: You really have to have a longer quotes message, i.e. several lines. Then 'gqip' transforms

> someone said
> > someone else said
> > someone else said
> > someone else said
> > > long quoted message
> > > long quoted message
> > > long quoted message
> > > long quoted message
> > someone else said
> > someone else said
> > someone else said
> blah

(12 lines)

into

> someone said
> > someone else said someone else said someone else said
> > > long quoted message long quoted message long quoted message long quoted
> > > message
> > someone else said someone else said someone else said
> blah

(6 lines of text in vim; additional linebreaks inserted by blogger.com)

Still interested in why the command does that, though.

Stefan said...

AHA

> one
>> two
>> two
>> two
>>> three
>>> three
>>> three
>> two
>> two
> one
> one

ESC gqip

> one
>> two two two
>>> three three three
>> two two
> one one

Nice, thanks

Douglas said...

I use gqap ("a paragraph") instead of gqip, as this also moves the cursor down to the blank line after the paragraph. I can then use the dot to repeat the command, easily reformatting a series of paragraphs.

dominiko said...

For a more powerful paragraph formatter, check-out 'par':

- http://www.nicemice.net/par/par-doc.var

- http://vim.wikia.com/wiki/Par_text_reformatter

Anonymous said...

None of the examples make any sense to me. What is the benefit of this?

Thanks.

Travis Whitton said...

For those of you that are confused. The command given in the tip is meant to be typed in normal mode and is not an ex (colon) command. What it's intended to do is invoke gq (format motion) on ip (inner paragraph). Vim provides a number of text-object motions, which allow a command to be applied to a given text entity. If you're not familiar with text-objects, I highly recommend doing a :help text-objects.

Anonymous said...

But the result of the command is a single paragraph as if you had written jagged text such as:

> a
> > b
> > b
> > > c

which becomes:
> a > > b > > b > > > c

That is, the text is simply rejustified, and nothing magic happens due to the presence or number of greater-than '>' character.

Broofa said...

Also note that "gq" is the reformat action. "ip" simply specifies what to format.

You could also do a mouse selection and type "gq" (in normal mode) to reformat that.

Finally, note that "gq" works on more than just email quoted text. I often use it for reformatting source code comments. For example:

// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum
// lorem ipsum

... select all of the above and hit "gq" gives you ...

// lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum
// lorem ipsum lorem ipsum lorem ipsum

Rizzabelle said...
This comment has been removed by the author.