I have turned off line wrap in Vim by adding this in my vimrc:
set nowrap
But, I would like to have line wrap turned on automatically when I am editing *.tex
files. So, I added this to my vimrc:
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex set wrap
augroup END
This should enable line wrap when the filetype is detected to be TeX. This works as expected for tex files, but if I open a non-tex file in the same Vim session, it has line wrap turned on!
Enabling and disabling word wrap automatically on different file extensions on Vim suggests using BufRead instead. But, even that has the same effect: if I first open a TeX file, followed by a non-TeX file, the non-TeX file has line wrap turned on.
How do I correctly enable line wrapping only for a certain filetype?
Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap. So using either: :set wrap!
You can use setlocal
for this. It'll only affect the current buffer.
augroup WrapLineInTeXFile
autocmd!
autocmd FileType tex setlocal wrap
augroup END
That'll be applied to every new TeX buffer.
The 'wrap'
option is local to window. When you use :set
, it will also apply to any newly opened windows. You want to use :setlocal
.
Also, though your :augroup WrapLineInTeXFile
works, it is cumbersome and doesn't scale to many settings. If you have :filetype plugin on
in your ~/.vimrc
, you can place filetype-specific settings (like :setlocal wrap
) in ~/.vim/after/ftplugin/tex.vim
(use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/tex.vim
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With