Vim: wrap text ("gq") without changing underlined headers

Is there a way to format text in Vim that relates to underlined headers?

Markdown has two ways of representing headers:

#Level 1 heading ##Level 2 heading ###Level 3 heading 

and only for levels 1 and 2:

 Level 1 heading =============== Level 2 heading --------------- 

I am fond of underlining, because, in my opinion, it reads better.

When I compose a markdown in Vim with, say :set textwidth=72 , I would like to be able to reformat the entire document with gggqG , but it treats these underlined headings as paragraphs and compresses them together on one line. Therefore, if I started with the following:

 Lorem ipsum =========== Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

After running gq all the way through, I get something like this:

 Lorem ipsum =========== Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 

Is there a way to prevent the formatting of selected Vim headers?

I suppose there should be a solution using formatexpr or formatprg . I studied the documentation for par , and although it is very powerful, it seems that this is not one of its functions. Therefore, I wonder if there is another external program that can be used with formatprg that understands markdowns, or if this can be achieved instead of using vimscript with the formatexpr parameter.

+6
vim formatting markdown text-formatting
source share
2 answers

One option that sorta does is to add underscores to the comments variable.

If your underline is a fixed size, you can only add those:

 :set comments+=:---------------,:=============== 

If they are variable sizes (more than one):

 :set comments+=n:--,n:== 

Using more than one allows a paragraph to start with one - or = and hold subsequent lines from the previous line of the comment.

Delete + above to add comments only to these lines and not add them.

There are cases when formatting will act unexpectedly (for example, underlining on consecutive lines). I'm sure there is a better way to do this, but hopefully this helps you get started.

 :h comments :h format-comments :h formatoptions :h fo-table 
+6
source share

At least you can set up a macro for it.

eg. place the text somehow in the first paragraph with the search for the first underline heading, then lower it down along the two lines, then visuallΓ­ select the area for the next underline, minus 3, and then format it:

 qa/^===========$/jjv/^===========$/-3<CR>gqq 

Now you can use the a macro.

NTN

0
source share

All Articles