This was not supposed to be my first post here. But I was writing that one and ran into a problem.
I write markdown in vim, and I hard wrap lines at 80 characters.
In my local preview everything was looking fine, but when pasting my post into the dev.to editor, I got greeted by a bunch of broken lines:
After some googling, I found out that there is an open issue with markdown linebreaks.
Feature Request or Task
User Story / Description
I'm writing my articles in a code editor and line-break them manually at 80 chars, so I could read them anywhere without worrying about word-wrap and stuff. Markdown does not count these line-breaks as line-breaks and every markdown parser I saw join them automatically, so
Dev.to, however, handle them as line-breaks.
Definition of Done
in a dev.to article renders as
What to do? I could switch to soft linebreaks, but navigation gets cumbersome. Maybe I could try to use the dev.to API to post and preprocess the markdown before... sounds like more work than I'd like to do. What's a
lazy good enough approach?
Have vim do it!
Can I edit the text with my usual setup but copy it as if it didn't have linebreaks? You can guess that was a yes. Here's the little monster I came up with
:map ,r :let oldtw=&textwidth <cr> \|:set textwidth=10000 <cr> \| ggVGgqgvy \| :let &textwidth=oldtw <cr> \| :set textwidth? <cr> \| gvgq
Let's break it down (omitting
\| which is a command separator and
<cr> which just means press enter after typing the command):
on pressing the ,r shortcut...
store whatever your current
textwidth is (in my case 80)
textwidth to some ridiculously large value so that you get no line breaks on long paragraphs
gg: go to the top of the file,
v: enable visual mode,
G: go to the bottom of the file (since you're in visual mode this will select everything)
gq: reformat the paragraph (since
textwidthis now 10000 all your paragraphs will be without newlines)
textwidth property to this old values (warning! this is not enough!, see options as values in vimscript)
Value is now fully restored
gv: start visual mode with the last selection (so whole document)
gq: reformat the paragraph again with your old
And that's it! Your post will be on your clipboard with uninterrupted paragraphs, and your editor will still display your nicely wrapped text.
Admittedly it has a few corner cases and yes, I should turn it into a function but solving problems with this uber pragmatic approach is a lot of fun. And fast. And I wanted to get something posted already!