INI files are plain-text configuration files that store settings in a simple, human-readable format of sections, keys, and values. They work by letting a program read and write named parameters (like width=1920) grouped into sections (like [display]) at startup or on demand, making them one of the oldest and most portable ways to manage application settings.
Brief History and Purpose
INI (short for "initialization") files date back to early versions of Microsoft Windows, where they were used to store settings for the operating system and applications. The format was simple enough that users could edit settings with a text editor, and developers could parse them with minimal code. Although Windows later moved to the binary Registry for system settings, INI files remain widely used across platforms—especially in Linux, macOS, and open-source software—because they are lightweight, transparent, and easy to version-control.
Core Structure: Sections, Keys, and Values
An INI file is made of sections, each containing key-value pairs. The basic syntax looks like this:
[database]
host = localhost
port = 5432
user = admin
- Sections are declared on their own line, with the name in square brackets:
[section-name]. Everything after that line belongs to that section until the next section header. - Keys (also called properties or settings) are names on the left side of an equals sign (
=) or a colon (:). - Values are the text on the right side. They can be strings, numbers, booleans, or lists, depending on how the program interprets them.
Comments are supported with a semicolon (;) or a hash (#) at the start of a line. Whitespace around keys and values is usually ignored, and many parsers allow values to be quoted if they contain special characters.
How Programs Read and Write INI Files
When an application starts, it typically opens the INI file, reads it line by line, and builds an in-memory structure (like a dictionary or a nested map). The program then looks up the needed settings by section and key. For example, a game might read [video] resolution=1920x1080 to set the default screen size. If the file is missing, the program may create it with default values.
Writing is just as simple: the program modifies the in-memory structure and saves the file back to disk, preserving the original formatting as much as possible. Some libraries offer functions to read and write specific keys without rewriting the whole file, but most implementations simply overwrite the file when a change is made.
Common Variations and Extensions
Although the core format is simple, many implementations add features:
- Case sensitivity: Some parsers treat section and key names as case-sensitive, others do not. Windows INI files are traditionally case-insensitive, while many Unix tools are case-sensitive.
- Nested sections: Some formats allow dot notation (e.g.,
[database.connection]) to simulate hierarchy, but this is not part of the original spec. - Multi-line values: A few parsers support line continuation with a backslash or by indenting subsequent lines.
- Environment variables: Some programs expand variables like
${HOME}inside values, but that is application-specific.
How to Choose Between INI, JSON, YAML, and XML
INI is best for small, flat configuration sets that humans will edit directly. It has no built-in support for arrays, nested objects, or complex data types, so for those you might prefer:
- JSON: Supports nested structures and data types, but is less readable with many nested braces.
- YAML: More expressive with indentation and lists, but can be tricky with tabs and edge cases.
- XML: Powerful and extensible, but verbose and harder to hand-edit.
If you need a simple key-value store with sections, INI is often the most straightforward. If you need arrays or objects, consider JSON or YAML.
Common Problems and Pitfalls
INI files are easy, but they have a few gotchas:
- Duplicate keys: If a key appears twice in the same section, behavior varies—some parsers use the last value, others the first.
- Comment placement: Inline comments (after a value) are not universally supported; a semicolon in a value might be misinterpreted.
- Encoding: INI files are usually plain ASCII or UTF-8, but older Windows tools may expect ANSI. Always declare or stick to UTF-8.
- Trailing spaces: Some parsers trim values, others don't, which can cause subtle bugs.
A common misconception is that INI files can store booleans or numbers natively. In reality, everything is a string until the program converts it. So true and 1 are just text; the application decides what they mean.
Security and Safety Considerations
Because INI files are plain text, they can be edited by anyone with file access. This is both a strength and a risk:
- Never store passwords or API keys in INI files unless the file permissions are restricted (e.g.,
chmod 600on Unix). - Validate any value read from an INI file before using it in code—especially if the file can be user-modified. A malicious value could cause a crash or injection if not handled properly.
- Be careful with paths or commands read from INI files; they could point to unintended locations.
Frequently Asked Questions
What does INI stand for?
INI stands for "initialization." The name comes from the file extension .ini, which was used for initialization files in early Windows.
Can I use INI files on Linux or macOS?
Yes. INI files are platform-agnostic. Many Linux applications (e.g., systemd, some games) use INI-style configuration files, and you can parse them with libraries in any language.
How do I escape special characters in an INI file?
There is no universal escape character. Most parsers treat everything after the equals sign as the value, so you can include spaces and most symbols. If you need a literal semicolon or hash at the start of a value, you may need to quote the value (e.g., key="value; with semicolon") depending on the parser.
Are INI files still used today?
Yes. While newer formats like JSON and YAML are popular, INI files are still used in many open-source projects, game engines, and system tools because they are simple and easy to parse.
What is the difference between INI and .conf files?
Functionally, they are often the same. .conf is just a different extension; many Unix programs use .conf for INI-style configuration files, sometimes with slight syntax variations (like using spaces instead of equals signs).