INI Cheatsheet

Sections, keys, values & configuration file format

Data Format
Contents
📝

Basics

; INI = Initialization file format
; Simple key=value pairs organized in sections
; Comments with ; or #

; Key-value pairs
key = value
name = Alice
port = 8080

; Comments
; This is a comment
# This is also a comment
📂

Sections

; Sections group related settings
[general]
app_name = MyApp
version = 1.0

[server]
host = localhost
port = 8080

[database]
host = localhost
port = 5432
name = mydb
user = admin
password = secret

[logging]
level = INFO
file = /var/log/app.log
💎

Value Types

; All values are strings
; Application interprets types

; String
name = Alice
path = /usr/local/bin

; Number (as string)
port = 8080
timeout = 30.5

; Boolean (conventions)
enabled = true
debug = false
verbose = yes
; Also: on/off, 1/0

; Empty value
optional =

; Values with spaces
message = Hello World
; Quotes are optional in most parsers
quoted = "Hello World"
🔧

Features

; Multi-line (continuation with indent)
description = This is a long value
    that spans multiple lines

; Interpolation (Python configparser)
[paths]
home = /home/user
data = %(home)s/data
logs = %(home)s/logs

; DEFAULT section (inherited by all)
[DEFAULT]
timeout = 30

[server]
host = localhost
; timeout = 30  (inherited)
🐍

Python configparser

import configparser

config = configparser.ConfigParser()
config.read('config.ini')

# Read
config['server']['host']
config.get('server', 'host')
config.getint('server', 'port')
config.getboolean('server', 'debug')
config.getfloat('server', 'timeout')

# Write
config['new_section'] = {'key': 'value'}
with open('config.ini', 'w') as f:
    config.write(f)

# Check
config.has_section('server')
config.has_option('server', 'host')
config.sections()
📋

Common Patterns

; Git config (~/.gitconfig)
[user]
name = Alice
email = alice@example.com

[core]
autocrlf = input
editor = vim

[alias]
co = checkout
br = branch
ci = commit

; PHP config (php.ini)
[PHP]
memory_limit = 256M
upload_max_filesize = 10M
display_errors = Off
date.timezone = UTC