Vim indent xml file

后端 未结 9 1762
北恋
北恋 2020-12-07 08:05

I am learning Vim but I thought this was a simple task but I cannot get it to work. I have browser SO but the solutions are not working for me.

I am trying to correc

相关标签:
9条回答
  • 2020-12-07 08:09

    This is easy to achieve once ~/.vimrc is setup properly.

    Set the following options in ~/.vimrc:

    filetype indent on
    set smartindent
    

    This allows you to move the cursor to the top of the file and indent to the end: gg=G

    You can also select the desired text with visual mode and hit = to indent it.

    0 讨论(0)
  • 2020-12-07 08:14

    for those one which is using coc.nvim plugin, you can install coc-xml by :CocInstall coc-xml , then mapping format key in your config file: nmap <silent> fm <Plug>(coc-format). From now on, you can format not only xml file but other file very easy

    0 讨论(0)
  • 2020-12-07 08:18

    In VIM, I auto-indent the whole file (using hard tabs) using the following command:

    :%!XMLLINT_INDENT="^I" xmllint --format -
    

    The ^I is a single character you generate via: CTRL+v,CTRL+i

    0 讨论(0)
  • 2020-12-07 08:24

    I like Berei's answer. However, I think the following is a little more flexible in that you don't have to alter your vimrc file. Plus it is easier to format select portions of the XML file (something I happen to do a lot).

    First, highlight the XML you want to format.

    Then, in normal mode, type ! xmllint --format -

    Your command-line at the bottom will look like this:

    :'<,'>!xmllint --format -

    Then hit enter.

    Technical Explanation

    The selected text is sent to the xmllint command, then --format'ed, and the results of xmllint are placed over your selected text in vim. The - at the end of the command is for receiving standard input - which, in this case, is the selected text that vim sends to xmllint.

    0 讨论(0)
  • 2020-12-07 08:26

    A simple solution that I like which doesn't require any 3rd party tool is to insert a newline before each opening tag '<...>'. Then you can use standard vim auto-indentation. In short:

    1. %s/</\r</g
    2. gg=G to auto indent
    0 讨论(0)
  • 2020-12-07 08:27

    I had the same problem. It turned out, that the line

    set viewdir=~\.vim\views\
    

    in my .vimrc caused the problem. Just make sure, you don't have it.

    0 讨论(0)
提交回复
热议问题