All formats

TXT (Plain text)

Full namePlain text
Extensions.txt, also .log, .nfo, .diz, .text, .tcr (Psion compressed text)
MIME typetext/plain (with a charset parameter, e.g. text/plain; charset=utf-8)
Typetext, no structure or formatting
Starts withnothing fixed; optional byte order mark EF BB BF (UTF-8), FF FE (UTF-16 LE), FE FF (UTF-16 BE)
SpecificationRFC 2046 (text/plain), RFC 3629 (UTF-8), The Unicode Standard
Maintained byIETF, Unicode Consortium, WHATWG (Encoding Standard)
Licenseopen

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#

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:

  1. A BOM: if present, it’s reliable.
  2. 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.
  3. Many zero bytes, alternating with ASCII: UTF-16 without a BOM.
  4. 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.
  5. A default: Windows-1252 for Western text is the most common fallback.
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#

Shell
# 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#

Software that opens TXT files#

SoftwareWindowsmacOSLinuxWebCanNotes
SumatraPDF✓viewfree, open source; .txt, .log, .nfo, .tcr shown as a reflowable document
Notepad✓view, edit, createbuilt into Windows; UTF-8, UTF-16 and ANSI, all line endings
TextEdit✓view, edit, createbuilt into macOS; choose the encoding in Open and Save
Visual Studio Code✓✓✓✓view, edit, create, convertfree; shows and changes encoding and line endings in the status bar
Notepad++✓view, edit, create, convertfree, open source; encoding conversion menu
Sublime Text✓✓✓view, edit, create, convertcommercial, free evaluation
Vim✓✓✓view, edit, create, convertfree, open source; :set fileencoding, :set fileformat
GNU Emacs✓✓✓view, edit, create, convertfree, open source
Kate✓✓✓view, edit, create, convertfree, open source
BBEdit✓view, edit, create, convertcommercial, free mode without some features
EmEditor✓view, edit, create, convertcommercial; opens very large files, encoding detection
iconv✓✓✓convertcommand line encoding converter, built into Linux and macOS
uchardet✓✓✓viewcommand line encoding detector

Libraries#

Every language reads and writes text; the libraries below detect and convert character encodings. See all text encoding libraries.

C/C++iconv, ICU, uchardet
C#System.Text.Encoding, UTF.Unknown
JavaICU4J, java.nio.charset, juniversalchardet
Gochardet, x/text/encoding
Pythonchardet, charset-normalizer
Rubycharlock_holmes, rchardet, String#encode
Rustchardetng, encoding_rs
PHPmbstring