1
votes

I have read Vim indent xml file and Indenting entire file in Vim without leaving current cursor location, but I want to something slightly different:

  • Can I use vim to indent an entire XML file but without changing the file. The only thing that should change is the representation, but not the file itself.

For those interested: I work with books in xml format. If the file is changed, this is usually by adding tabs and/or spaces. This messes up my data (because there might actually be tabs or spaces in the text itself).

2
Can't you just indent it and not save the file afterwards?Zenadix
@Zenadix No because I actually want to make changes in the file whilst using this visual (but not real) indentation.Private

2 Answers

1
votes

You have some options:

Change tab width

If your XML has Tab-based indent, you can change the visual appearance of the indent by changing the 'tabstop' value. This doesn't affect the physical <Tab> characters in the file at all.

Pre-/Post-process

You can change the amount of indent after reading, and undo that shortly before the write. Here's an example that halves / doubles spaces:

:autocmd BufReadPost,BufWritePost  * %substitute/^\( \+\)\1/\1/e
:autocmd BufWritePre               * %substitute/^ \+/&&/e
1
votes

In Vim, all your editing is done on a buffer, the internal representation of your file. As long as you don't write your changes to disk, your file stays untouched.

But, if you really don't want to take that risk… work on a copy.