How to stop vim from reformatting lines inside a codeblock in a markdown file?

2018-06-01 17:58:06

I keep the a formatoption in vim when I'm modifying markdown files, so my lines are automatically kept to textwidth characters long, with automatic reformatting when I modify text.

How can I make vim keep this behavior, but not reformat/join lines that are inside a triple-backtick code block? For example, consider the following file:

# Title

Here's some content that vim is automatically wrapping to 60

characters long, because I have run :set textwidth=60 and

:set formatoptions+=a. However, when I'm in the codeblock

below, I would like vim to stop the automatic wrapping as

well as the line joining. As it stands, this file would be

impossible to reproduce in vim with my current settings.

```

def hello():

print("Hello world")

```

Trying to type that codeblock at the end gives me:

``` def hello(): print("Hello world") ```

Is my style of markdown abnormal? Do people just rely on manually turning off the a formatoption when they're modifying a codeblock? Or am I