0

How can I convert a column of words (one word per line) to a paragraph using vim?

For example, before:

how
to
convert

After:

how to convert
6
  • 1
    Try this: VipJ.
    – 3N4N
    Commented Oct 10, 2022 at 5:18
  • @kadekai This is (IMHO) THE answer, i.e., I doubt there's a more elegant way to accomplish the task. If you move it to an answer and flesh it out a little, it would get the cred it it deserves.
    – MDeBusk
    Commented Oct 10, 2022 at 5:34
  • 1
    @kadekai as pointed out, please don’t answer in the comments, which can’t be appropriately voted on or edited.
    – D. Ben Knoble
    Commented Oct 10, 2022 at 22:39
  • @D.BenKnoble noted. For relatively simple questions, I prefer commenting instead of answering because the reward (reps) against the work (explanation of the answer) falls short most of the time. I was gonna give as example my recent answers which took time and effort to investigate but got maybe one upvote and no green tick, but somehow all three of those answers got upvoted and ticked last night (I have proof, if anyone thinks I'm spinning a tale), so I can't even back my argument up with example. . . . But anyway, noted.
    – 3N4N
    Commented Oct 11, 2022 at 5:51
  • That's a problem with the voting system in general: if an answer takes a long time to write, that generally will mean it takes a long time to check if it's correct, and upvoters tend not to make that effort. Nevertheless, (I feel) answers should be written as answers! If you don't want to write an explanation, just don't write one: you might not get upvotes then, but you don't get them for a comment either!
    – Rich
    Commented Oct 11, 2022 at 9:43

3 Answers 3

3

With fairly default settings, gqip or gwip should do the trick.

This applies the gq or gw formatting/wrapping operators to the ip in-paragraph text object.

1

Visually select a range of words and press J to join them:

  1. select 3 lines and Join them
VjjJ
  1. select a paragraph (text separated by an empty line(s)) and Join them
vipJ

Or for the whole buffer you can simply :%join or :%j to join all lines.

0

For one paragraph, with cursor on the first line somewhere:

v}J

v = start visual selection; } = jump to next blank line; J = join the lines (note it's a capital J)

In your example, with cursor on the first line, 3J also would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.