I have some html files that I'd like to retab that look like this:
<header>
<div class="wrapper">
<img src="images/logo.png">
<div class="userbox">
<div class="welcome">Welcome Andy!</div>
<div class="blackbox">
<ul>
<li><a href="#">Invite Friends</a></li>
<li><a href="#">My Account</a></li>
<li><a href="#">Cart</a></li>
<li><a href="#">Sign Out</a></li>
</ul>
</div>
</div>
</div>
</header>
And I want them to look something like this:
<header>
<div class="wrapper">
<img src="images/logo.png">
<div class="userbox">
<div class="welcome">Welcome Andy!</div>
<div class="blackbox">
<ul>
<li><a href="#">Invite Friends</a></li>
<li><a href="#">My Account</a></li>
<li><a href="#">Cart</a></li>
<li><a href="#">Sign Out</a></li>
</ul>
</div>
</div>
</div>
</header>
Or some sane default. What's the easiest way to go about doing this from the terminal in ubuntu for all of the html files in the current directory?
In
vim
:EDIT: Explainations:
gg
: top line=
: indent until...G
: ...endNote: this probably wont play nice with inline-php. Play around with
tidy
(without the-m
argument) to see how it works.I used Stéphane Chazelas' solution for reindenting a file with sed along with dogbane's solution on how to repeat a string 'n' times to compose this invocation that will change the indentation of a file that uses spaces for indentation:
Add these functions to your
.bashrc
or.zshrc
, and then just run:If the indentation is imperfect to begin with, I would first run the file through a "pretty print" formatting utility such as tidy for HTML or jq for JSON.
Are those tabs in the first example and spaces in the next? Here's a shell script that could change tabs to two spaces. Change working directory to location of HTML files.
UPDATE
I just had need to do something similar. I went strait for Komodo Edit's replace-in-files feature. It's very nice if you have GUI access to the files.