How can I replace tabs with spaces before compiling .tex file?

Use the fancyvrb package:

\documentclass{article}
\usepackage{fancyvrb}

\fvset{tabsize=4}

\begin{document}
I'm regular text
\begin{Verbatim}
I have no tab
	I have 1 tab
		I have 2 tabs

I have no space
    I have 4 spaces
        I have 8 spaces
\end{Verbatim}
\end{document}

enter image description here

The site is changing tabs to four spaces, so this might seem cheating, but in my file I have tabs.


As Mike Renfro points out in the comments, many text editors support converting between tabs and spaces; some will even make the change seamless and transparent by printing some number of blank spaces when you press the tab key. This is probably the easiest answer to your exact question.

Another solution if you happen to be a Linux (maybe OSX) user would be to use the expand command. On the command line if you run expand -t 4 file.tex > new_file.tex all of the tabs in file.tex will be converted to 4 spaces as the file is replicated in new_file.tex. More information is available on the expand command.

See also unexpand.