Unified C utility for converting between tabs and spaces in source code files while preserving proper indentation and formatting.
- tabconv: Unified utility with multiple conversion modes (tabs↔spaces)
- Default mode: Convert tabs to spaces (backward compatible with tospaces)
- Tab mode: Convert spaces to tabs with
--tabsflag (backward compatible with totabs)
- Overview
- Utilities
- Features
- Installation
- Usage
- How It Works
- Error Codes
- Testing
- Project Structure
- Limitations
- Contributing
- License
- Documentation
These utilities are designed to enforce consistent coding conventions by converting between tab characters and spaces. They maintain proper column alignment and handle edge cases like multiple consecutive tabs/spaces and mixed content.
A unified utility that can convert between tabs and spaces with configurable tab width. Maintains proper indentation by tracking column positions and calculating appropriate spacing.
Default mode (tabs→spaces): ./tabconv <tab-width> <file(s)>
Tab mode (spaces→tabs): ./tabconv --tabs <tab-width> <file(s)>
Help: ./tabconv --help
- Multiple conversion modes: Tabs to spaces (default), spaces to tabs (
--tabs), auto-normalise (--normalize) - Multiple file support: Process multiple files in a single command
- Backward compatibility: Default mode matches
tospacesbehaviour,--tabsflag matchestotabsbehaviour - Command-line options: Standard GNU-style long options (
--help,--verbose, etc.)
- Configurable Tab Width: Specify any number of spaces per tab (2, 4, 8, etc.)
- Column Tracking: Maintains current column position for proper spacing calculations
- Safe File Processing: Uses temporary files to avoid data loss
- Error Handling: Comprehensive error checking with descriptive messages
- Minimal Dependencies: Only uses standard C libraries (stdio.h, stdlib.h)
gcc -o tabconv tabconv.cThis will create an executable named tabconv in the current directory.
Alternatively, you can use the provided Makefile:
makeThis will compile the tabconv executable. Additional make targets:
make test- Build and run the test suitemake clean- Remove generated filesmake debug- Build with debug symbolsmake release- Build with optimisationsmake install- Install system-wide (requires sudo)
sudo cp tabconv /usr/local/bin/./tabconv [OPTIONS] <tab-width> <file(s)>- Default (tabs→spaces): Convert tabs to spaces (backward compatible with
tospaces) - Tab mode (
--tabs): Convert spaces to tabs (backward compatible withtotabs) - Normalise mode (
--normalize): Auto-detect and convert to specified style - Check mode (
--check): Verify indentation consistency without modifying files (not yet implemented)
-s, --spaces: Convert tabs to spaces (default)-t, --tabs: Convert spaces to tabs-n, --normalize: Auto-detect and convert (normalise)-c, --check: Check only, don't modify (future)-v, --verbose: Show detailed output-b, --backup: Create backup copies before modification-h, --help: Show help message
<tab-width>: Tab width in spaces (positive integer, e.g., 2, 4, 8)<file(s)>: One or more files to process (supports wildcards:*.c *.h)
# Convert tabs to 4 spaces (default mode)
./tabconv 4 file.c
# Convert spaces to tabs (4-space tab width)
./tabconv --tabs 4 file.c
# Convert tabs to 2 spaces
./tabconv 2 script.py
# Convert spaces to tabs with 8-space width
./tabconv --tabs 8 index.html# Process all C source files
./tabconv 4 *.c *.h
# Process all Python files with 2-space tabs
./tabconv 2 *.py
# Convert spaces to tabs in multiple files
./tabconv --tabs 4 src/**/*.java# Verbose output showing processing details
./tabconv --verbose 4 file.c
# Create backup before conversion (default mode)
./tabconv --backup 4 file.c
# Create backup with spaces-to-tabs conversion
./tabconv --tabs --backup 4 file.c
# Show help
./tabconv --help
# Default mode (equivalent to old tospaces)
./tabconv 4 file.c
# Tab mode (equivalent to old totabs)
./tabconv --tabs 4 file.cThe tool reads files character by character, tracking column positions to maintain proper indentation. It processes each character according to the selected conversion mode and writes the result to a temporary file before replacing the original.
Tabs to spaces: For each tab character, it calculates the spaces needed to reach the next tab stop based on the current column and tab width, then outputs the appropriate number of spaces.
Spaces to tabs: Consecutive spaces are grouped and replaced with tabs where possible, optimising indentation while preserving alignment.
Normalise mode: Analyses the first 100 lines to detect the existing indentation style (tabs, spaces, or mixed), then automatically converts to the specified tab width using the appropriate algorithm.
Temporary files follow the pattern filename.}}{{ and are cleaned up after successful processing.
The program returns the following exit codes:
0: Success1: Error (incorrect arguments, file cannot be opened, temporary file creation failed, etc.)
All errors return exit code 1 with a descriptive error message printed to stderr using perror().
The repository includes comprehensive testing for the unified utility:
test_suite.c: Complete unit test suite covering tabs→spaces, spaces→tabs, and normalise mode conversionstest_results.md: Test cases and verification results
To run all tests:
make testThis will compile the tabconv utility and run the full test suite.
Test tabs→spaces conversion:
# Create a test file with tabs
echo -e "Line1\tTab\nLine2\t\tDoubleTab" > test.txt
# Convert tabs to 4 spaces (default mode)
./tabconv 4 test.txt
# View the result
cat test.txtTest spaces→tabs conversion:
# Create a test file with spaces
echo -e "Line1 Tab\nLine2 DoubleTab" > test.txt
# Convert spaces to tabs (4-space tab width)
./tabconv --tabs 4 test.txt
# View the result
cat test.txt.
├── tabconv.c # Unified tab conversion utility source
├── tabconv # Compiled executable (tabconv)
├── Makefile # Build configuration and test runner
├── LICENSE.md # BSD 3-Clause License
├── FUTURE.md # Feature suggestions and roadmap
├── test_suite.c # Comprehensive unit test suite
├── test_results.md # Test cases and results
└── README.md # This file
- Processes one file at a time
- Maximum filename length: 1024 characters
This is a simple educational project. Feel free to fork and modify for your own needs. Potential improvements could include:
- Recursive directory processing
- Check mode implementation (
--checkflag) - Additional command-line options
This project is licensed under the BSD 3-Clause License. See the LICENSE.md file for details.
- Source Code:
tabconv.cincludes detailed Doxygen-style comments explaining the unified conversion algorithm - Test Suite:
test_suite.ccontains comprehensive unit tests covering all conversion modes - Future Plans: See FUTURE.md for feature suggestions and development roadmap
Vlad Shurupov - Creator and maintainer of the tab conversion utility.