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
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.
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
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
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.
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
.
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:
%s/</\r</g
gg=G
to auto indentI 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.