8

Vim has the excellent command set tw=79 which will automatically break your lines at 79 characters, however I like (just) my comments broken at 72 characters automatically.

Is there any good way to do this in Vim?

user530873
  • 648
  • 4
  • 8

1 Answers1

9

I like this one

augroup comment_textwidth
    autocmd!
    autocmd TextChanged,TextChangedI * :call AdjustTextWidth()
augroup END

function! AdjustTextWidth()
    let syn_element = synIDattr(synID(line("."), col(".") - 1, 1), "name")
    let &textwidth = syn_element =~? 'comment' ? 72 : 79
    echo "tw = " . &textwidth
endfunction

Source

For more inspiration look here.

ryuichiro
  • 1,171
  • 10
  • 8