I created a rather large file marked up with Markdown (heh) & then, a few hours later, stupidly opened it in a text editor that I had evidently set (some time ago) to strip extra spaces off the ends of files. I made a change, not realizing what I’d just done, & then, a day later, I opened the file in Sublime Text.

At that point, I discovered to my horror that none of my Markdown breaks—inserted by placing two spaces at the end of a line1—were working. This was a really long file2, so I didn’t want to manually replace the many breaks. Instead, I decided to use a regex search & replace in Sublime Text. Here’s what I did.

Search for this:

([a-zA-Z0-9:,);`*])$\n([a-zA-Z0-9/&(*`>])

Replace with:

\1  \n\2

The regex looks for any of these characters at the end of a line:

a-z
A-Z
0-9
:
,
)
;
`
*

Followed by a return, & then a line immediately following that starts with any of these:

a-z
A-Z
0-9
/
&
(
*
`
>

This covers everything I needed. I created both lists iteratively, by searching & replacing, looking through the file for things I’d missed, adding them, searching & replacing again, repeat.

There’s one downside to my method: you can’t just set up the search & replace & let ’er rip. Instead, you have to search & replace by hand (this actually goes very quickly, even in a large file: just press Command-G for the next match; if it matches, press Command-Alt-E; if it doesn’t, press Command-G to move on). Why? Well, the search matches this, which you want to catch:

foo
1968

But also this, which you do not want it to match:

1. foo
2. bar

Another reason to search & replace by hand is that the regex will match this, which you want to fix:

foo
*bar*

But also this, which you want to leave alone:

* foo
* bar

It’s nice to have this search & replace documented, in case I ever accidentally strip the double spaces from my Markdown files again.

  1. I possess a deep & abiding love for Markdown, but the “two spaces at the end of a line make a break” thing is easily the worst design decision in the language. That said, I don’t know what, other than an ugly backslash at the end of the line, could replace it. 

  2. It was actually a presentation about Linux that I created in reveal.js, an awesome browser-based presentation tool that I’m going to write about soon.