# slice-ansi

> Slice a string with ANSI escape codes

Latest version **9.0.0** (published 2026-04-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install slice-ansi
pnpm add slice-ansi
yarn add slice-ansi
bun add slice-ansi
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 9.0.0 |
| Published | 2026-04-04 |
| First published | 2015-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22 |
| Dependencies | 2 |
| Unpacked size | 31.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Maintainers | sindresorhus |
| Keywords | slice, string, ansi, styles, color, colour, colors, terminal, console, cli, tty, escape, formatting, rgb, 256, shell, xterm, log, logging, command-line, text |

## Links

- npm: https://www.npmjs.com/package/slice-ansi
- Repository: https://github.com/chalk/slice-ansi
- Homepage: https://github.com/chalk/slice-ansi#readme
- Issues: https://github.com/chalk/slice-ansi/issues
- Funding: https://github.com/chalk/slice-ansi?sponsor=1
- npm.io page: https://npm.io/package/slice-ansi

## Dependencies (2)

- [ansi-styles](https://npm.io/package/ansi-styles.md) ^6.2.3
- [is-fullwidth-code-point](https://npm.io/package/is-fullwidth-code-point.md) ^5.1.0

## Alternatives

- [postcss-color-hex-alpha](https://npm.io/package/postcss-color-hex-alpha.md) — 6.4M weekly downloads
- [randomcolor](https://npm.io/package/randomcolor.md) — 348.0K weekly downloads
- [bows](https://npm.io/package/bows.md) — 1.3K weekly downloads
- [ep_prefer_color_scheme](https://npm.io/package/ep_prefer_color_scheme.md) — 260 weekly downloads
- [coc-yank](https://npm.io/package/coc-yank.md) — 61 weekly downloads

## Recent versions

- 9.0.0 (latest) — 2026-04-04
- 8.0.0 — 2026-02-10
- 7.1.2 — 2025-09-08
- 7.1.0 — 2023-10-29
- 7.0.0 — 2023-10-28
- 6.0.0 — 2023-03-24
- 5.0.0 — 2021-04-16
- 4.0.0 — 2020-02-16
- 3.0.0 — 2019-09-17
- 2.1.0 — 2019-01-25
- 2.0.0 — 2018-10-16
- 1.0.0 — 2017-07-10
- 0.1.0 — 2017-06-21
- 0.0.4 — 2015-09-26
- 0.0.3 — 2015-09-26
- … 1 more at https://npm.io/package/slice-ansi/versions

## README

# slice-ansi [![XO: Linted](https://img.shields.io/badge/xo-linted-blue.svg)](https://github.com/xojs/xo)

> Slice a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles)

## Install

```sh
npm install slice-ansi
```

## Usage

```js
import chalk from 'chalk';
import sliceAnsi from 'slice-ansi';

const string = 'The quick brown ' + chalk.red('fox jumped over ') +
	'the lazy ' + chalk.green('dog and then ran away with the unicorn.');

console.log(sliceAnsi(string, 20, 30));
```

## API

### sliceAnsi(string, startSlice, endSlice?)

#### string

Type: `string`

String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk).

#### startSlice

Type: `number`

Zero-based visible-column index at which to start the slice. Grapheme clusters (for example, emoji sequences and combining marks) are kept intact.

#### endSlice

Type: `number`

Zero-based visible-column index at which to end the slice.
If a full grapheme cluster would cross `endSlice`, it is excluded.

## Related

- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
- [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal
- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right

## Maintainers

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)

---
_Source: https://npm.io/package/slice-ansi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
