Reputation: 2830
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
Upvotes: 0
Views: 322
Reputation: 136
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
.
Upvotes: 1
Reputation: 172570
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.
Upvotes: 2
Reputation: 195059
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:
Upvotes: 1