What does the Tabs to Spaces Converter do?
The Tabs to Spaces Converter transforms hard tab characters (\t) into standardized space sequences (2-space, 4-space, or 8-space stops) or collapses leading space indentation back into native tabs. It also normalizes cross-platform line endings (LF vs CRLF).
Core Concepts
Understanding tab-to-space conversion and indentation mechanics:
- Tab Stop Replacement: Replaces every single
\tcharacter with a fixed number of space characters (e.g. 2, 4, or 8 spaces). - Space-to-Tab Collapsing: Scans leading indentation and replaces consecutive space blocks with single tab (
\t) characters. - Line Ending Standardization: Converts Windows (
\r\n), classic Mac (\r), and Unix (\n) line terminators into uniformLForCRLFformats.
How to use the tool?
- Paste Code or Text: Enter or paste your source code or text payload into the editor or click Load Sample.
- Configure Conversion:
- Choose your Conversion Direction (Tabs to Spaces or Spaces to Tabs).
- Select your desired Tab Size (2, 4, or 8 spaces).
- Convert & Copy: Click Convert Indentation, then click Copy or Download to save your standardized code.
Related Developer Utilities
If you work with source code formatting, indentation, and whitespace normalization, explore these complementary tools:
- Text Whitespace Cleaner: Normalize multiple spaces, tabs, and blank lines.
- Trim Trailing Spaces: Strip extraneous whitespace from line endings.
- Remove Empty Lines: Remove blank and whitespace-only lines from text.
- Text Diff Tool: Compare text and code documents with side-by-side highlighting.
REST API Integration
Blueutils provides a free REST API endpoint (POST https://blueutils.com/api/text/tabs-to-spaces) to programmatically convert tab characters (\t) to spaces or collapse spaces to tabs with customizable 2-space, 4-space, or 8-space indentation in source code and text documents.
API Request Parameters
| Name | Type | Description | Example |
|---|---|---|---|
rawText |
String | Input multiline raw text or source code payload. | "\tconst a = 1;" |
mode |
String | Optional. Conversion mode: "tabsToSpaces" or "spacesToTabs". Default: "tabsToSpaces". |
"tabsToSpaces" |
tabSize |
Number | Optional. Number of spaces per tab (range: 1-16). Default: 4. |
4 |
lineEndings |
String | Optional. Line ending normalization: "lf" or "crlf". |
"lf" |
API Request Payload Examples
cURL
curl -X POST https://blueutils.com/api/text/tabs-to-spaces \
-H "Content-Type: application/json" \
-d '{
"rawText": "\tconst a = 1;",
"mode": "tabsToSpaces",
"tabSize": 4
}'Python
import requests
url = "https://blueutils.com/api/text/tabs-to-spaces"
payload = {
"rawText": "\tconst a = 1;",
"mode": "tabsToSpaces",
"tabSize": 4
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())Java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String jsonPayload = """
{
"rawText": "\\tconst a = 1;",
"mode": "tabsToSpaces",
"tabSize": 4
}
""";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://blueutils.com/api/text/tabs-to-spaces"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}API Response Parameters
| Name | Type | Description | Example |
|---|---|---|---|
isValid |
Boolean | Indicates whether conversion succeeded. | true |
result |
String | Converted text payload with standardized indentation. | " const a = 1;" |
API Response Payload Examples
Success Response (HTTP 200 OK)
{
"isValid": true,
"result": " const a = 1;"
}Validation Failure Response (HTTP 400 Bad Request)
{
"isValid": false,
"error": "Input text payload cannot be empty."
}Rate Limit Exceeded Response (HTTP 429 Too Many Requests)
{
"error": "API rate limit exceeded. Please wait or contact support@blueutils.com."
}Why use an API to convert tabs to spaces?
Integrating the Tabs to Spaces API into CI/CD linters, git pre-commit hooks, or AI agent tool calling provides key benefits:
- Rapid Script Validation: Standardizes indentation before running strict style checkers (PEP 8, ESLint, Google Style Guide).
- Optimized Token Efficiency for AI Agents: LLMs frequently produce mixed tabs and spaces that break YAML and Python parsing. Invoking the API normalizes indentation deterministically without consuming reasoning tokens.
- Deterministic Accuracy Without Hallucinations: Ensures 100% predictable character replacement and uniform tab stop spacing.
Native Usage
How to convert tabs to spaces locally in terminal environments or scripts:
Windows (CMD / PowerShell)
# Convert tabs to 4 spaces in PowerShell
(Get-Content input.txt) | ForEach-Object { $_ -replace "`t", " " } | Set-Content cleaned.txtLinux / Unix (Bash)
# Convert tabs to 4 spaces using expand in Linux
expand -t 4 input.txt > cleaned.txt
# Convert spaces to tabs using unexpand
unexpand -t 4 input.txt > tabs.txtPython
Using Python expandtabs:
with open("input.txt", "r", encoding="utf-8") as f:
text = f.read().expandtabs(4)
with open("cleaned.txt", "w", encoding="utf-8") as f:
f.write(text)
print("Tabs converted to spaces successfully.")Java
Using Java String.replace:
import java.nio.file.*;
public class TabsToSpacesExample {
public static void main(String[] args) throws Exception {
String content = Files.readString(Paths.get("input.txt"));
String converted = content.replace("\t", " ");
Files.writeString(Paths.get("cleaned.txt"), converted);
System.out.println("Tabs converted successfully.");
}
}