I currently have a huge HTML file which doesn't have line breaks and just appears in a single line.
I want to format it in vim (macvim in particular). I tried the following options, but none of them has worked for me.
Is there a way to do this either using a Plugin or otherwise?
Thanks!
To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).
Press V then move the cursor to select a range of lines, then press = to indent the selection.
First, go to the start point of codes to be formatted, then press v to start selection. Second, go to the end point. Third, press = to format the codes that have been selected. All braces and indentations will be aligned.
One way to start it is to split all tags onto their own lines.
:s/<[^>]*>/\r&\r/g :g/^$/d
If you have floating <
or >
symbols (e.g. invalid HTML, JavaScript comparison operators, CSS direct descendant selector part), this won't work properly and you could switch to something like just doing end tags - <\/[^>]*>
. It provides a solid start, anyway.
Demonstration:
With a idealised document like this,
<!DOCTYPE html><html><head><title>hello</title></head><body>world</body></html>
This produces this:
<!DOCTYPE html> <html> <head> <title> hello </title> </head> <body> world </body> </html>
Then, =
will produce what you want:
<!DOCTYPE html> <html> <head> <title> hello </title> </head> <body> world </body> </html>
For a better result, you should use specialized external format programs.
You can integrate both tidy and html-beautify automatically by installing the plugin vim-autoformat. After that, you can execute whichever formatter is installed with a single keystroke.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With