0

I am editing my markdown files which contain some code examples (like here). Is there any way to tell vim, when it is editing markdown files, to set textwidth=80 to everything except where I keep my code syntax? So for example:

Here is a text within a markdown file // textwidth=80

    puts 'Hello World' // textwidth is not specified
2
  • puts 'Hello World' is code in Markdown? Commented May 21, 2013 at 21:12
  • yup, but it is indented 4 spaces, and markdown recognises that as code so it uses special syntax highlighting. Commented May 21, 2013 at 21:13

3 Answers 3

2

You can change the 'textwidth' setting dynamically with an :autocmd:

:autocmd CursorMoved,CursorMovedI <buffer> let &textwidth = (getline('.') =~# '^    ' ? 0 : 80)

This checks for Markdown code (indented by 4 spaces), and then clears the textwidth.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know how to simply set tw option to meet your requirement. However I came up with a function, it could do what you want:

function! WrapMD()
   let x=&tw
   let &tw=80
   normal! gqq
   let &tw=x
endfunction

this function just does format with tw=80 on current line, after that restores your original tw setting.

You can source the function (or put it in your vimrc), and then do:

:v/\v^( {4}|\t)/call WrapMD()

at any time when you want to format your MD text.

You could also create a mapping for that or put it in a autocmd on event BufWritePre.

Here I made a gif when I test the function:

enter image description here

Comments

1

I frequently use pandoc to tidy up markdown: pandoc -t markdown will wrap markdown, but not code blocks. It also nicely tidies up lists and block quotes. The vim-pandoc plugin sets 'equalprg' to pandoc -t markdown --reference-links.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.