TXT (Plain text)
| Full name | Plain text |
|---|---|
| Extensions | .txt, also .log, .nfo, .diz, .text, .tcr (Psion compressed text) |
| MIME type | text/plain (with a charset parameter, e.g. text/plain; charset=utf-8) |
| Type | text, no structure or formatting |
| Starts with | nothing fixed; optional byte order mark EF BB BF (UTF-8), FF FE (UTF-16 LE), FE FF (UTF-16 BE) |
| Specification | RFC 2046 (text/plain), RFC 3629 (UTF-8), The Unicode Standard |
| Maintained by | IETF, Unicode Consortium, WHATWG (Encoding Standard) |
| License | open |
A plain text file is a sequence of characters with no formatting. The bytes on disk depend on the character encoding: the same text can be stored as UTF-8, UTF-16, Windows-1252 or Shift_JIS, and a text file doesn’t record which one it uses. Most problems with text files come from reading them with the wrong encoding (garbled “mojibake” like é instead of é) or with unexpected line endings.
Today the default is UTF-8 without a BOM and \n line endings. Windows tools still produce UTF-16 (PowerShell 5 redirection, some Microsoft exports) and legacy 8-bit code pages, and old files from DOS, Windows 9x and other systems use whatever code page the author’s system had.
When to use TXT#
- Use for notes, logs, README files, data that people read and edit, and as the simplest exchange format.
- Use UTF-8 for anything new. It covers all of Unicode, it’s ASCII-compatible and it’s the default for the web, Linux, macOS and modern Windows tools.
- Avoid when you need structure: use CSV, JSON or XML. For formatting use Markdown or RTF.
- Avoid legacy 8-bit encodings for new files: they can’t mix scripts and the reader has to guess them.
File structure#
A text file has no header. It’s a stream of encoded characters, split into lines by line-ending characters, with an optional byte order mark at the start.
Encodings#
| Encoding | Bytes per character | Notes |
|---|---|---|
| ASCII | 1 | 128 characters (bytes 00-7F); a subset of UTF-8 and most code pages |
| UTF-8 | 1-4 | all of Unicode; ASCII stays 1 byte; the default everywhere today |
| UTF-16 LE / BE | 2 or 4 | all of Unicode; Windows’ internal encoding; LE is common in Windows files |
| UTF-32 LE / BE | 4 | rare in files |
| Windows-1252 | 1 | Western European Windows; often mislabeled ISO-8859-1 (Latin-1) |
| ISO-8859-x | 1 | Latin-1 to Latin-10, Cyrillic (-5), Greek (-7), … |
| Windows-125x, KOI8-R | 1 | Central European, Cyrillic, Greek, Turkish, … |
| CP437, CP850 | 1 | DOS code pages; box-drawing characters in .nfo art |
| Shift_JIS, EUC-JP | 1-2 | Japanese |
| GBK, GB18030 | 1-4 | Chinese (simplified); GB18030 covers all of Unicode |
| Big5 | 1-2 | Chinese (traditional) |
| EUC-KR | 1-2 | Korean |
UTF-8 encodes a code point in 1 to 4 bytes:
| Code points | Bytes |
|---|---|
| U+0000-U+007F | 0xxxxxxx |
| U+0080-U+07FF | 110xxxxx 10xxxxxx |
| U+0800-U+FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
| U+10000-U+10FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
48 C3 A9 6C 6C 6F 0A "Héllo\n" in UTF-8 (é = C3 A9)
48 E9 6C 6C 6F 0D 0A "Héllo\r\n" in Windows-1252 (é = E9)
FF FE 48 00 E9 00 6C 00 ... "Hél..." in UTF-16 LE with BOM
Byte order mark (BOM)#
U+FEFF at the start of a file identifies the Unicode encoding and byte order:
| Bytes | Encoding |
|---|---|
EF BB BF |
UTF-8 |
FF FE |
UTF-16 LE |
FE FF |
UTF-16 BE |
FF FE 00 00 |
UTF-32 LE (check before UTF-16 LE) |
00 00 FE FF |
UTF-32 BE |
UTF-8 doesn’t need a BOM. Windows Notepad used to add one; many Unix tools, shells (#! scripts), JSON parsers and CSV readers treat it as garbage at the start of the file. Write UTF-8 without a BOM unless a consumer requires it (old Excel versions do for CSV). When reading, accept and strip it.
Line endings#
| Name | Bytes | Used by |
|---|---|---|
| LF | 0A (\n) |
Linux, macOS, Unix, most modern tools |
| CRLF | 0D 0A (\r\n) |
Windows, DOS, many network protocols (HTTP, SMTP) |
| CR | 0D (\r) |
classic Mac OS (before OS X) |
Modern editors, including Windows Notepad since 2018, read all three. Git can convert line endings on checkout (core.autocrlf, .gitattributes with text=auto or eol=lf). The last line may or may not end with a line ending; POSIX tools expect one.
Detecting the encoding#
A text file doesn’t say how it’s encoded, so programs check in this order:
- A BOM: if present, it’s reliable.
- Valid UTF-8: non-ASCII text in a legacy encoding is very rarely valid UTF-8 by accident, so if all bytes decode as UTF-8, treat the file as UTF-8.
- Many zero bytes, alternating with ASCII: UTF-16 without a BOM.
- Statistical detection for legacy encodings: libraries like uchardet, ICU and charset-normalizer compare byte and character frequencies against language models. Short files give unreliable results; tell the user and let them override.
- A default: Windows-1252 for Western text is the most common fallback.
Related plain text extensions#
| Extension | What it is |
|---|---|
.log |
program output, usually UTF-8 or the system code page |
.nfo |
release info files, traditionally CP437 with ASCII art; view with a DOS font |
.diz |
FILE_ID.DIZ, a short description inside archives, CP437 |
.tcr |
Psion “Text Compression for Reader”: compressed plain text eBooks from Psion handhelds |
.text, .me, README |
plain text without a .txt extension |
Converting encodings#
# show the detected encoding (file command, uchardet)
file -i notes.txt
uchardet notes.txt
# convert Windows-1252 to UTF-8
iconv -f WINDOWS-1252 -t UTF-8 old.txt > new.txt
# convert UTF-16 (with BOM) to UTF-8
iconv -f UTF-16 -t UTF-8 export.txt > export-utf8.txt
# CRLF to LF and back
dos2unix file.txt
unix2dos file.txt
In PowerShell 7, Get-Content -Encoding and Set-Content -Encoding utf8NoBOM convert files; Windows PowerShell 5.1 writes UTF-16 LE by default with >.
Specifications and documentation#
- RFC 2046, section 4.1: the
text/plainmedia type and thecharsetparameter - RFC 3629: UTF-8
- RFC 2781: UTF-16, byte order and BOM
- The Unicode Standard: characters, encoding forms, BOM (chapter 2 and 3)
- WHATWG Encoding Standard: encoding names and labels as browsers implement them; the reference for decoding legacy encodings
- IANA character set registry: official charset names
- Unicode FAQ: UTF-8, UTF-16, UTF-32 and BOM
Software that opens TXT files#
| Software | Windows | macOS | Linux | Web | Can | Notes |
|---|---|---|---|---|---|---|
| SumatraPDF | ✓ | view | free, open source; .txt, .log, .nfo, .tcr shown as a reflowable document | |||
| Notepad | ✓ | view, edit, create | built into Windows; UTF-8, UTF-16 and ANSI, all line endings | |||
| TextEdit | ✓ | view, edit, create | built into macOS; choose the encoding in Open and Save | |||
| Visual Studio Code | ✓ | ✓ | ✓ | ✓ | view, edit, create, convert | free; shows and changes encoding and line endings in the status bar |
| Notepad++ | ✓ | view, edit, create, convert | free, open source; encoding conversion menu | |||
| Sublime Text | ✓ | ✓ | ✓ | view, edit, create, convert | commercial, free evaluation | |
| Vim | ✓ | ✓ | ✓ | view, edit, create, convert | free, open source; :set fileencoding, :set fileformat | |
| GNU Emacs | ✓ | ✓ | ✓ | view, edit, create, convert | free, open source | |
| Kate | ✓ | ✓ | ✓ | view, edit, create, convert | free, open source | |
| BBEdit | ✓ | view, edit, create, convert | commercial, free mode without some features | |||
| EmEditor | ✓ | view, edit, create, convert | commercial; opens very large files, encoding detection | |||
| iconv | ✓ | ✓ | ✓ | convert | command line encoding converter, built into Linux and macOS | |
| uchardet | ✓ | ✓ | ✓ | view | command line encoding detector |
Libraries#
Every language reads and writes text; the libraries below detect and convert character encodings. See all text encoding libraries.