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 missing an easy way of handling such codeblocks in markdown files?

I also recorded this asciinema video to illustrate the problem.


  • Entering paste mode should stop autoformatting as well, though that's just swapping out one manual operation for another. One option might be to set a key mapping in insert mode to insert the three backticks and toggle paste mode or the a formatoption. I don't hard wrap markdown, personally, so this has never really occurred to me before, but I can't imagine there's a terribly elegant solution. – brhfl Jun 1 '18 at 18:00
  • I have this problem too... Have not yet found a solution besides implementing the format myself in vimscript. – bobismijnnaam Apr 17 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.