add newlines before closing tags

While I still hate how VS decides to format tags like p (paragraph) and h1 through h6 (and check the comments on the blog post – AFAICT no one using the product wants the current behavior), I do have an ugly workaround – get some whitespace in front of the closing tags (typically the </p> closing tag is the issue).  Since I want it to end up on its own line anyway, I figured the search-and-replace might as well do it for me.

I know ‘normal’ (perl/.net/etc family) regular expressions fine but VS has its own little quirks (like how it ‘tags’ via curly brackets, instead of ‘grouping’ via parentheses), so I figured I’d put this in a post so I can find it a little easier later on.

The ‘find’ expression:

{[^ ]}\</p\>

Going somewhat backwards, the ending is the </p> tag (had to escape the brackets), the part preceding it is a tagged expression (the curly brackets for that), and the expression being tagged is ‘any character that’s not a space’.

The ‘replace’ expression:

\1\n</p>

Going forward this time, it’s ‘first tagged expression’ (the \1), then a newline, then the closing </p> tag.  It doesn’t indent the tag the right amount, but once it’s on the newline, format-document will align it fine for me.

Under ‘Find options’, make sure you have ‘Use’ checked with ‘Regular expressions’ selected in the drop-down. 

The result should look like this:

image

Advertisements