I would like to hard wrap XML files during editing. In every other filetype my set textwidth=80 works fine. But in XML something overrides the insertion of line breaks. I tried adding t to formatoptions but that doesn't make a difference. I believe there is an automatic formatting command being called from the default xml filetype plugin, which overrides all else.

How can I set up vim to do automatic line breaking in XML?


Yes, the xml filetype plugin disables auto wrapping text using textwidth with the following:

setlocal formatoptions-=t

This removes the option t (= "Auto-wrap text using textwidth") from formatoptions. See :help formatoptions and :help fo-table.

To overwrite do the following:

Create the file ~/.vim/after/ftplugin/xml.vim (or $HOME/vimfiles/after/ftplugin/xml.vim on Windows) with the following content:

" prevent multiple loading
if exists("b:did_my_ftplugin") | finish | endif
let b:did_my_ftplugin = 1

setlocal formatoptions+=t

The files in ~/.vim/after are loaded after the default plugins are loaded. So this can be used to overwrite settings done in the default ftplugin. See :help after-directory. Here the option t is readded to formatoptions.

  • I found I also needed setlocal formatoptions-=l and setlocal formatexpr=. This of course disables automatic formatting. It also seems to break auto indentation (disable with setlocal indentexpr=). Maybe the XML plugin needs patching? Or is there another solution? – tbrk Feb 25 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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