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?

1 Answer 1


I like this one

augroup comment_textwidth
    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


For more inspiration look here.

  • 2
    Funny thing is, I never thought of that solution as a serious one :)
    – VanLaser
    Sep 22, 2015 at 9:08
  • If it works, it works :)
    – raine
    Mar 30, 2022 at 12:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.