How to enable line wrapping only for certain file types in Vim?

I turned off line wrapping in Vim by adding this to my vimrc :

set nowrap 

But I would like the automatic line feed to be turned on automatically when editing *.tex files. So, I added this to my vimrc:

 augroup WrapLineInTeXFile autocmd! autocmd FileType tex set wrap augroup END 

This should include line wrapping when the file type is defined as TeX. This works as expected for tex files, but if I open the non-tex file in the same Vim session, it will include line termination!

Enabling and disabling word wrap automatically in different file extensions on Vim involves the use of BufRead. But even this has the same effect: if I open the TeX file first and then the file without TeX, the file without TeX will include a wrapper.

How to enable line wrapping only for a certain type of file?

+6
source share
2 answers

You can use setlocal for this. This will only affect the current buffer.

 augroup WrapLineInTeXFile autocmd! autocmd FileType tex setlocal wrap augroup END 

This will apply to all new TeX buffers.

+5
source

The 'wrap' parameter is local to the window. When you use :set , it also applies to any newly opened windows. You want to use :setlocal .

Also, although yours :augroup WrapLineInTeXFile works, it is bulky and does not scale for many settings. If you have :filetype plugin on in ~/.vimrc , you can set file-specific parameters (for example :setlocal wrap ) to ~/.vim/after/ftplugin/tex.vim (using the after directory allows you to override any settings default file types made by $VIMRUNTIME/ftplugin/tex.vim ).

+3
source

All Articles