---
title: "Write an ANSI colored terminal diff in C#"
url: https://recursiveai.org/onediff/cookbook/write-colored-terminal-diff/
description: "Render a line diff with ANSI colors in C#, then write a plain version for logs. Includes a runnable .NET example with OneDiff."
version: "1.0.1"
---

# Write an ANSI colored terminal diff in C#

**OneDiff** is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/write-colored-terminal-diff/write-colored-terminal-diff.zip)

Show deleted lines in red and inserted lines in green, or write the same diff without color for logs.

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneDiff;

var diff = Diff.Lines("red\n", "green\n");
Console.Write(Diff.ToTerminal(diff));
```

A hunk is a group of changes plus any surrounding unchanged lines. The terminal shows this hunk with the deletion red and insertion green:

```text
@@ -1 +1 @@
-red
+green
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/write-colored-terminal-diff/write-colored-terminal-diff.zip), then `dotnet run`.

## The full program

The download prints the ANSI string with its escape characters made visible, then the plain version. All inputs are the two strings shown in the code.

```csharp
using OneDiff;

var diff = Diff.Lines("red\n", "green\n");
string ansi = Diff.ToTerminal(diff);
Console.WriteLine("ANSI (ESC shown as <ESC>):");
Console.Write(ansi.Replace("\u001b", "<ESC>"));
Console.WriteLine("Plain log:");
Console.Write(Diff.ToTerminal(diff, color: false));
```

```text
ANSI (ESC shown as <ESC>):
<ESC>[36m@@ -1 +1 @@<ESC>[0m
<ESC>[31m-red<ESC>[0m
<ESC>[32m+green<ESC>[0m
Plain log:
@@ -1 +1 @@
-red
+green
```

A word-token diff compares words rather than whole lines; `Diff.Words` makes one. Passing it to the terminal renderer throws `ArgumentException`; use `Diff.Lines`.

## Variations

### Named files without context or color

This standalone snippet labels the inputs and removes surrounding equal lines.

```csharp
using OneDiff;
using OneDiff.Output;

var diff = Diff.Lines("keep\nred\n", "keep\ngreen\n");
var renderer = new TerminalRenderer
{
    Color = false,
    ContextLines = 0,
    OldName = "before.txt",
    NewName = "after.txt"
};
Console.Write(renderer.Render(diff));
```

```text
--- before.txt
+++ after.txt
@@ -2 +2 @@
-red
+green
```

## Related recipes

- [Diff two lists with a custom comparer in C#](https://recursiveai.org/onediff/cookbook/diff-typed-sequences/)
- [Limit diff algorithm work in C#](https://recursiveai.org/onediff/cookbook/bound-diff-work/)
- [Compare text in C#](https://recursiveai.org/onediff/cookbook/compare-text/)

## The short answer

Create a line diff with `Diff.Lines`, then pass it to `Diff.ToTerminal`. ANSI color is on by default. Pass `color: false` for the plain form. Both forms keep the deletion and insertion prefixes, so the log remains readable without its colors.

The terminal renderer takes a line-token diff, not a word-token diff. It provides its own word highlighting inside replaced lines; you don’t need to change the initial tokenization to get that view. The [terminal output documentation](https://recursiveai.org/onediff/docs/text-output/) describes the emitted format.

## What is ANSI color in a terminal?

ANSI colors are control sequences in the output string, interpreted by a supporting terminal rather than printed as ordinary letters. OneDiff emits red for deleted lines, green for inserted lines and cyan for hunk headers. Each styled line ends with a reset sequence, so that style doesn’t carry into the next prompt. File headers use bold text.

The example makes the escape character visible as `<ESC>` so you can see the bytes’ structure in a browser. That replacement is only for displaying the example output. Send the original string to the console for actual color. Sending the replaced string prints the label literally instead of changing colors.

## How do I write an ANSI colored diff in C#?

Use the snippet at the top for the default view. Use `TerminalRenderer` when you need file names, a different number of context lines or control over word highlighting. The variation above produces a named hunk with no surrounding unchanged lines.

The `@@` header contains the old and new starting line numbers and their counts. Line numbers there are one-based. A count of one is omitted, which is why the example reads `-1 +1` rather than `-1,1 +1,1`. A minus prefix means a line exists only in the old text; a plus prefix means it exists only in the new text. Context lines start with a space.

An identical pair renders as an empty string. A shown line without its final newline gets a separate no-newline note. Output lines themselves always end with `\n`, regardless of the input platform.

## How do I write a terminal diff without ANSI color codes?

Pass `color: false`, as the full program does for its “Plain log” output. This emits the same hunk text without styling escape sequences. It also disables word highlighting, which uses reverse video and therefore needs color mode. Color selection is explicit; the renderer’s default doesn’t inspect whether your output is redirected. Choose the plain form when you want a file with no renderer-generated styling codes.

With color on, word highlighting applies only to replaced blocks with both old and new lines. The renderer skips it for large blocks or rewrites with little shared content; it still shows the line deletions and insertions. You can also disable it through `WordLevel` while retaining line colors. These are presentation choices, not changes to the underlying edit script.
