Introduction
An INI file is a simple text format for configuration, made of named sections of
key=value lines. It became familiar on Windows, where versions up to 3.1
kept system settings in files such as WIN.INI and SYSTEM.INI,
and programs kept their own. Windows 95 and Windows NT moved settings to the Registry,
and the file functions remain only for compatibility, but the layout is still everywhere
in other software, including php.ini.
There is no formal standard, so each program’s reader is slightly different. Many
configuration files with other extensions, including .cfg and
.conf files, use the same layout, as Python’s setup.cfg does.
Others with the same extensions have a different layout entirely.
Structure
An INI file is plain text, read line by line. Each line is one of these:
- a section header: a name in square brackets, as in
[server], which applies to the lines after it - a key and value:
name=value - a comment, starting with
;, or with#in many readers
Sections do not nest. Beyond that, readers disagree on whether key names are case-sensitive, whether spaces around the equals sign count, whether values can be quoted or continued on a later line, whether a comment can follow a value, how repeated keys are handled and which text encodings are understood. Values have no types of their own; each program decides what a value means.
Several formats use the same shape with stricter rules of their own. A
Windows Registry file looks like an INI file, with
key paths in square brackets and "name"=value lines, but has its own
header and value syntax and is read by Registry Editor, not the INI functions.
Internet shortcuts and
desktop entries are also written in this
style.
; Settings for the example program
[server]
host=example.org
port=8080
[logging]
level=info
file=C:\Logs\example.log
The viewer reads the dialect from hints in the file and colours accordingly:
- Git config (a
[remote "origin"]header, or[core]): a;or#after a value starts a comment, anywhere outside double quotes; a value that ends in a backslash goes on at the next line; a key with no=is true; a key and value may follow the header on its line. - systemd units and desktop entries (
[Unit],[Service],[Desktop Entry]): comments only on lines of their own; a backslash continues a value. - php.ini (
extension=,[PHP]): a;after a value starts a comment;true,on,yes,false,off,no,none,nulland numbers are coloured as literals. - Python configparser (
setup.cfg,tox.ini,[DEFAULT],[tool:x],%(name)s,key: valuelines):=or:separates key and value, a;or#after a value is part of the value, a line indented deeper than its key continues the value, and every value is text. - Otherwise (the Windows
GetPrivateProfilestyle):=or:separates key and value; a;or#that follows white space after a value starts a comment (Windows itself keeps it in the value, so files meant for it should not rely on this); numbers and booleans are coloured.
In every dialect a line that starts with ; or # is a comment, a
comment may follow a [section] header, and a quoted value keeps any
; or # inside the quotes.
Not described
Known, not yet covered
- Reference: the page says that readers disagree, but it does not list how the common ones read the rules: the Windows
GetPrivateProfileStringfunctions, Python’sconfigparser(the:separator,%(name)sinterpolation, indented continuation lines), Git’s[section "subsection"]headers and escapes, PHP’sphp.inivalue syntax, and the systemd unit and.editorconfigvariations. Each one’s manual describes its rules. - Reference: encodings (UTF-16 files written by Windows, byte order marks) and the line-continuation, duplicate-key and case rules of each reader are not described, nor are the other file types that look like INI (
.cfg,.conf,.desktop,.url,.reg), except where they have a page of their own. - Viewer: the dialect is a guess from the hints in the file. A file with none is read in the Windows style, and one that mixes dialects (a git-style comment in a configparser file) is read in the first that matches. Systemd and desktop-entry values are not typed per key (a
yesis coloured as a boolean wherever it stands), and the escapes of git and PHP strings (\n,\") are not coloured. Variable interpolation (%(name)s,${name}) is not marked. - Viewer: the same rules are used for every INI-like file, including
.regfiles, wheredword:00000001andhex:01,02are coloured as strings, not as typed values. There is no outline of sections and keys, no check for repeated keys, and only the first 1 MiB is read and coloured.
Also used for
- Tyrian configuration – Tyrian's TYRIAN.CFG: 28 bytes of detail level, game speed, volumes, joystick buttons and keys.
Links
Software
- Notepad – the text editor built into Windows (Windows)
- Notepad++ – free text editor with INI syntax colouring (Windows)
- Visual Studio Code – free code editor with built-in INI support (Windows, macOS, Linux)
Developers
- configparser – Python’s built-in reader and writer for INI-style files (Python)
- inih – small INI parser (C and C++)
- GetPrivateProfileString – the Windows function that reads INI files (C)
Reference
- INI file – Wikipedia’s comparison of the common variations
- The configuration file – PHP’s manual on
php.ini - git-config – Git’s manual, describing its own INI-style configuration files