3

I'm learning Vimscript in Learn Vimscript the Hard way and trying an exercise. Here is this:

The lines "underlined" with = characters are treated as headings by Markdown.

:onoremap ih :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rkvg_"<cr>

By this, Vim will delete the heading of whatever section you're in and put you in insert mode ("change inside heading").

Markdown can also have headings delimited with lines of - characters. Adjust the regex in these mappings to work for either type of heading.

So I changed the regex(?^==\\+$) to this:

?^\(==\|--\)\\+$

Then replaced it. However, this regex can't find neither '-' headings nor '=' them.

What is the correct regex I want in this case?

6

The original regex is

?^==\\+$

which means, searching backwards (?) for start-of-line (^), followed by =, followed by another = which can be repeated several times (\+ qualifier, followed by end-of-line ($).

The backslash needs to be doubled, because the regex is embedded into a double-quoted string, read :h expr-quote why this is needed).

If you want to allow either - or = to find the headers, you would change the regex to be somthing like this:

 ^[=-]\+$

which comes down to, search start of line (^), any of the chars - or = at least once (\+) and end of line ($).

Note, that this would also find e.g. lines consisting of: =-=-=- If you would like to prevent this, you would need to capture the first character and make sure it comes several times:

^\([=-]\)\1\+$

Note: remember to double the backslashes, if you put this into a double quoted string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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