1

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?

1 Answer 1

2

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.

4
  • 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, 2019 at 9:00
  • 1
    @tbrk xml filetype maintainer here. Why do you think you need patching? Apr 21, 2019 at 8:40
  • Hello @ChristianBrabandt, when I enabled hard line wrapping using the options given above, the automatic tag indentation started to behave strangely. However, I just found that there is a tag indentation problem on my Mac even before changing any options and that tag indentation + hard line wrapping works fine on my Linux box (fo+=t, fo-=l, formatexpr=; both using Vim 8.1). I take back my comment about the XML plugin needing paching. Sorry for the noise!
    – tbrk
    Apr 21, 2019 at 8:58
  • The fo+=t flag in the after/ftplugin file greatly improves the experience of editing xml files! Nov 10, 2020 at 12:11

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.