Skip to content

Instantly share code, notes, and snippets.

@namklabs
Created March 28, 2012 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namklabs/2228933 to your computer and use it in GitHub Desktop.
Save namklabs/2228933 to your computer and use it in GitHub Desktop.
Find all closing HTML tags and add a linebreak after each group
" Find all closing tags, add a linebreak after each grouping.
" This is useful when your whole HTML file has had the whitespace removed by a rogue WYSIWYG editor.
" NOTE: I don't think this .vim file would actually work in vim. I just gave it that extension for Gist's sake.
:%s@\(<\/\w\{1,10\}>\)\+@&\r@gc
" command explanation below
" : begin command
" % use all lines in file
" s search and replace
" @ begin what-to-match for search
" \( begin grouping
" < first character of closing html tag
" \ escape closing html tag slash
" / closing html tag slash
" \w match a letter
" \{1,10\} match 1 to 10 letters in a row. I don't think any HTML elements are more than 10 chars long
" > end closing html tag
" \) end grouping
" \+ find more than one closing tag in a row
" @ end what-to-match, begin what-to-replace
" & insert what was matched in what-to-match
" \r insert line break
" @ end what-to-replace
" g this search can happen more than once per line
" c confirm each replacement
@zach-is-my-name
Copy link

zach-is-my-name commented Oct 18, 2021

Thank you! This ranked high in the search results. (I really like the step-by-step breakdown of the regex... great way to reduce the intimidation factor!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment