| name | encoding-guard |
| description | Prevent garbled text (乱码) when editing code and comments. Use this skill whenever reading, writing, or modifying files to ensure proper UTF-8 encoding handling. Activates on any file edit operation, especially when working with Chinese characters, emoji, or non-ASCII content. |
Encoding Guard
Ensures all file operations use proper UTF-8 encoding to prevent garbled text (乱码).
Core Principles
- Always use UTF-8 — Read and write files with explicit UTF-8 encoding
- Validate before write — Check that content is valid UTF-8 before saving
- Preserve BOM awareness — Detect and handle BOM (Byte Order Mark) when present
- Handle mixed encoding — Convert legacy encodings (GBK, GB2312, Big5) to UTF-8
When Reading Files
cat -n file.tsx
file -I file.tsx
hexdump -C file.tsx | head -5
When Writing Files
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
Detection Patterns
Watch for these signs of encoding problems:
- Mojibake characters:
é, è, ü (UTF-8 read as Latin-1)
- Chinese garbled:
锟斤拷, 烫烫烫, 屯屯屯 (GBK/UTF-8 mismatch)
- Replacement character:
� (U+FFFD) — missing or invalid bytes
- Question marks:
??? where Chinese should be
Fix Strategies
Strategy 1: Re-read with correct encoding
If a file shows garbled text:
file -I problematic.tsx
iconv -f GBK -t UTF-8 problematic.tsx > fixed.tsx
mv fixed.tsx problematic.tsx
Strategy 2: Use Read tool with encoding awareness
The Read tool automatically handles encoding. If you see garbled output:
- Stop and check the file's actual encoding
- Convert if needed before editing
- Then proceed with normal Read/Edit operations
Strategy 3: Validate after write
After any Edit or Write operation:
python3 -c "
with open('file.tsx', 'r', encoding='utf-8') as f:
content = f.read()
print(f'Valid UTF-8: {len(content)} chars')
"
Common Scenarios
Scenario 1: Editing TypeScript/JSX with Chinese comments
Action: Always use UTF-8 locale. Set in shell:
export LANG=en_US.UTF-8
Scenario 2: Mixed encoding in project
If some files are GBK and others UTF-8:
find . -name "*.tsx" -exec file -I {} \; | grep -v utf-8
for f in $(find . -name "*.tsx" -exec file -I {} \; | grep gb2312 | cut -d: -f1); do
iconv -f GBK -t UTF-8 "$f" > "$f.utf8"
mv "$f.utf8" "$f"
done
Scenario 3: Git shows encoding warnings
git config core.quotepath false
git config i18n.commitencoding utf-8
git config i18n.logoutputencoding utf-8
Prevention Checklist
Before any file operation:
Quick Diagnostic
Run this to check your environment:
echo "Locale: $LANG"
echo "LC_ALL: $LC_ALL"
python3 -c "import sys; print(f'Default encoding: {sys.getdefaultencoding()}')"
python3 -c "import locale; print(f'Preferred encoding: {locale.getpreferredencoding()}')"
Expected output:
Locale: en_US.UTF-8
LC_ALL: en_US.UTF-8
Default encoding: utf-8
Preferred encoding: UTF-8
Integration with Tools
Read Tool
- Automatically detects and handles encoding
- If garbled, the file may need conversion first
Edit Tool
- Content parameter must be valid UTF-8
- Tool validates encoding before applying changes
Write Tool
- Content parameter must be valid UTF-8
- Overwrites file with UTF-8 encoding
Bash Tool
- Set locale before commands:
LANG=en_US.UTF-8 command
- Pipe through
iconv if needed: command | iconv -f GBK -t UTF-8