0

When I want to share my markdown file to others, I would like to join lines or format lines for all paragraphs in my file quickly.

File in formatted version:

# head 1

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

# head 2

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

# head 3

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

File with paragraph lines joined.

# head 1

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

# head 2

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

# head 3

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

I know that I can go to a paragraph and use vipJ to join lines or use gqip to format it. But it is quite cumbersome once there are a lot of paragraphs in this file.

Are there better ways to convert between these two formats quickly? Any suggestions are welcome.

3
  • What about macro?
    – Maxim Kim
    Commented Jun 1, 2020 at 6:28
  • @MaximKim I would like to use commands to convert between these two formats.
    – jdhao
    Commented Jun 1, 2020 at 6:38
  • then you have to define requirements (should you join/split code blocks? long headers?) and use vimscript to implement them
    – Maxim Kim
    Commented Jun 1, 2020 at 6:46

2 Answers 2

1

It would be really hard to do this for a real world markdown files with regards to all edge cases (tables, code blocks, list items, headers etc).

But for example given you can try following :h :global commands:

  1. :g/^\S.*\n\S/normal! vipJ
  2. :g/^\S/normal! gww

Join with :g/^\S.*\n\S/normal! vipJ

It searches for all "paragraphs" -- 2 consecutive lines that start with non whitespace char. And applies normal vim command vipJ

Split with :g/^\S/normal! gww

It searches for all lines that start with non whitespace char and applies normal vim command gww

0

You could use

:g/^$/+,/^$/-j
  • g/^$/: Search for an empty line
  • +,/^$/-: Range from the next line to the next empty line minus one
  • j: Join the lines

NOTE:

  • Requires a empty line at end of file
  • Will produce an error message, as the range can not be build from the last line.

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.