Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

In vim:

:set softtabstop=0
:set expandtab
:set smarttab
:set shiftwidth=2
gg=G
:retab

EDIT: Explainations:

  • lines 1-3: sane default
  • line 4: indent with 2 spaces
  • line 5:
    • gg: top line
    • =: indent until...
    • G: ...end
  • line 6: insure that all tabs are converted to spaces
share|improve this answer
    
You probably should mention that those are vi commands. –  psusi Jan 10 '11 at 20:37
    
Indeed, I should, (the question appeared in my 'vim' category) –  shellholic Jan 10 '11 at 20:49
sudo apt-get install tidy
cd whatever_dir_you_want
find . -name '*.html' -exec tidy -m {} \;

Note: this probably wont play nice with inline-php. Play around with tidy (without the -m argument) to see how it works.

share|improve this answer

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.

#!/bin/sh
for i in *.html
do
    sed 's/\t/  /g' "${i}" > filename.notabs && mv filename.notabs "${i}"
done

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.

alt text

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.