How can I set different textwidths for specific file extensions in .vimrc?

vim

Solution

use `setlocal`

autocmd bufreadpre *.txt setlocal textwidth=0

instead of `set`.

With `setlocal` you make sure that the value you're setting is set in the current buffer, not for all buffers.

Problem

I would like to have a default textwidth of 80 characters except for a few file extensions like `txt`. The following lines appear to work, except for the first time when I edit (and create) a `txt` file. ``` setlocal textwidth=80 autocmd bufreadpre *.txt set textwidth=0 ``` What is the correct way to do this?

Original source