2013-11-14

Unwrap the text of an email message

This is a challenge that's only hard if you don't know the right command. If you do know the right command, it's trivial.

:se tw=999<CR>gqGZZ

:se tw=999^MgqGZZ for 16.

The email quotes (> at the front of the line) look like they'll get in the way, but gq is can handle comment text. Just crank textwidth up to 999 (or 298 if you're a minimalist), and you can finish the challenge with one command.

Too easy? Let's change the rules. What score can you get without gq (or its less popular cousin gw)? I can get 18:

:%s/\n\A*\ze\l/ ^MZZ

This solution uses the regex \n\A*\ze\l to match the newline and non-letters before a lowercase letter, but not the lowercase letter itself. \ze forces the regex to match the text that comes after it, without it being part of the match to be replaced. It's often simpler and cheaper than using submatches.

Read the manual

  • :help gq is mostly irrelevant and confusing, but here it is. gw does almost exactly the same thing, but no one likes it for some reason.
  • :help 'textwidth' will tell you little you didn't already know.
  • :help fo-table has interesting information about how to customize text formatting with :set formatoptions. Not sure you'll get any use out of it though.
  • :help /character-classes is a VimGolfer's best friend.
  • :help /\zs for more on \zs and \ze in regexes. They don't show up all that often in VimGolf, but they're a quick, intuitive way to get things done.

Similar challenges

No comments:

Post a Comment