How to use the command hledger (with examples)
The hledger command is a plain text accounting software for the command-line. It allows users to manage their financial transactions, track expenses, and generate financial reports.
Use case 1: Add transactions to your journal interactively
Code:
hledger add
Motivation: The “hledger add” command allows users to add transactions to their journal interactively. This can be useful when entering multiple transactions or when there is a need for manual entry of transaction details.
Explanation: The “hledger add” command opens an interactive interface where users can enter the details of their transactions. Users can input the date, account names, amount, and other transaction details.
Example output:
Enter transaction date (YYYY-MM-DD): 2021-01-15
Enter description: Grocery shopping
Enter account name: Expenses:Food
Enter amount: -50.00
Transaction added successfully.
Use case 2: Show the account hierarchy, using a specific journal file
Code:
hledger --file path/to/file.journal accounts --tree
Motivation: By using the “–file” flag with the path to a specific journal file, users can view the account hierarchy for that particular file. This can be helpful in understanding the organization of accounts and how they are grouped.
Explanation: The “–file” flag is followed by the path to the desired journal file. The “accounts” command is used to display the account hierarchy in tree form.
Example output:
Assets
└── Cash
Liabilities
├── Credit Card
└── Loans
Expenses
├── Utilities
└── Food
Income
├── Salary
└── Investments
Use case 3: Show a monthly income statement
Code:
hledger incomestatement --monthly --depth 2
Motivation: The “hledger incomestatement” command allows users to generate a monthly income statement, summarizing their income and expenses. The “–depth” and “–monthly” flags provide additional options for customizing the generated statement.
Explanation: The “–monthly” flag indicates that the income statement should be generated on a monthly basis. The “–depth” flag specifies the maximum depth for account grouping. In this example, “–depth 2” means accounts will be grouped up to the second level.
Example output:
January:
Income
Salary: $5000.00
Investments: $100.00
Expenses
Utilities: $200.00
Food: $300.00
February:
Income
Salary: $5000.00
Investments: $120.00
Expenses
Utilities: $180.00
Food: $250.00
Use case 4: Print the amount of cash spent on food
Code:
hledger print assets:cash | hledger -f- -I balance expenses:food --depth 2
Motivation: This command allows users to calculate and print the amount of cash spent specifically on food. It utilizes the combination of two hledger commands and pipeline to provide the desired result.
Explanation: The first part of the command, “hledger print assets:cash”, prints the current balance of the “assets:cash” account. The “|” symbol is used to redirect the output of the previous command to the next command. The second part of the command, “hledger -f- -I balance expenses:food –depth 2”, uses the balance command to calculate the total amount spent on “expenses:food” and displays the result.
Example output:
Cash: $1000.00
Total spent on Food: $300.00
Conclusion:
The hledger command provides a powerful tool for managing financial transactions and generating various financial reports. By understanding its different use cases, users can effectively track their expenses, create budgets, and gain insights into their financial situation.