I have the markdown file that uses #
for headlines, like this one.
#H1
Text one one
##h2
more Text
#more H1s
Even more text
No I have the problem that some interpreters of markdown don't accept a headline when it isn't surrounded by empty lines. So the upper file needs to look like this.
#H1
Text one one
##h2
more Text
#more H1s
Even more text
I thought I could write a function that gets executed when I write my changes to the file and I came up with this two functions
function! <SID>SurroundingNewLines()
execute "normal! O\<esc>jo\<esc>"
endfunc
function! <SID>AddEmptyLines()
1global/^#/execute "normal! o\<esc>"
2,$global/^#/call <SID>SurroundingNewLines()
%substitute/\n\{3,}/\r\r/e
endfunc
Please note that this is my first experience with VimScript.
The function AddEmptyLines
searches for lines that start with a #
and surrounds those with new lines and then the substitute
command removes all duplicate empty lines.
This works but it is rather inefficient since it always adds the lines and then removes all unnecessarily added lines.
So is there a better way of doing this, for example find all headers that are not surrounded by empty lines?