# cli-truncate

> Truncate a string to a specific width in the terminal

Latest version **6.1.1** (published 2026-07-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install cli-truncate
pnpm add cli-truncate
yarn add cli-truncate
bun add cli-truncate
```

## Health

**Score 70/100 (B)** — status: active.

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.1.1 |
| Published | 2026-07-09 |
| First published | 2016-03-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=22 |
| Dependencies | 2 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 97 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | truncate, ellipsis, text, limit, slice, cli, terminal, term, shell, width, ansi, string |

## Links

- npm: https://www.npmjs.com/package/cli-truncate
- Repository: https://github.com/sindresorhus/cli-truncate
- Homepage: https://github.com/sindresorhus/cli-truncate#readme
- Issues: https://github.com/sindresorhus/cli-truncate/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/cli-truncate

## Dependencies (2)

- [slice-ansi](https://npm.io/package/slice-ansi.md) ^9.0.0
- [string-width](https://npm.io/package/string-width.md) ^8.2.0

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 6.1.1 (latest) — 2026-07-09
- 6.1.0 — 2026-06-28
- 6.0.1 — 2026-06-23
- 6.0.0 — 2026-04-04
- 5.2.0 — 2026-03-01
- 5.1.1 — 2025-10-23
- 5.1.0 — 2025-09-15
- 5.0.0 — 2025-08-31
- 4.0.0 — 2023-10-28
- 3.1.0 — 2021-10-09
- 3.0.0 — 2021-08-10
- 2.1.0 — 2019-12-06
- 2.0.0 — 2019-06-13
- 1.1.0 — 2017-07-23
- 1.0.0 — 2017-01-13
- … 3 more at https://npm.io/package/cli-truncate/versions

## README

# cli-truncate

> Truncate a string to a specific width in the terminal

Gracefully handles [ANSI escapes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles). Like a string styled with [`chalk`](https://github.com/chalk/chalk). It also supports Unicode surrogate pairs and fullwidth characters.

## Install

```sh
npm install cli-truncate
```

## Usage

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorn', 4);
//=> 'uni…'

// Truncate at different positions
cliTruncate('unicorn', 4, {position: 'start'});
//=> '…orn'

cliTruncate('unicorn', 4, {position: 'middle'});
//=> 'un…n'

cliTruncate('unicorns rainbow dragons', 6, {position: 'end'});
//=> 'unico…'

cliTruncate('\u001B[31municorn\u001B[39m', 4);
//=> '\u001B[31muni…\u001B[39m'

> [!NOTE]
> When truncating styled text (ANSI escapes), the truncation character inherits the style at the breaking point for `position: 'start'` and `position: 'end'`. This does not apply to `position: 'middle'`.

// Truncate Unicode surrogate pairs
cliTruncate('uni\uD83C\uDE00corn', 5);
//=> 'uni\uD83C\uDE00…'

// Truncate fullwidth characters
cliTruncate('안녕하세요', 3);
//=> '안…'

// Truncate the paragraph to the terminal width
const paragraph = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.';
cliTruncate(paragraph, process.stdout.columns ?? 80);
//=> 'Lorem ipsum dolor sit amet, consectetuer adipiscing…'
```

## API

### cliTruncate(text, columns, options?)

#### text

Type: `string`

The text to truncate.

#### columns

Type: `number`

The finite number of columns to occupy in the terminal.

#### options

Type: `object`

##### position

Type: `string`\
Default: `'end'`\
Values: `'start' | 'middle' | 'end'`

The position to truncate the string.

##### space

Type: `boolean`\
Default: `false`

Add a space between the text and the ellipsis.

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorns', 5, {space: false});
//=> 'unic…'

cliTruncate('unicorns', 5, {space: true});
//=> 'uni …'

cliTruncate('unicorns', 6, {position: 'start', space: true});
//=> '… orns'

cliTruncate('unicorns', 7, {position: 'middle', space: true});
//=> 'uni … s'
```

##### preferTruncationOnSpace

Type: `boolean`\
Default: `false`

Truncate the string from a whitespace if it is within 3 characters from the actual breaking point.

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorns rainbow dragons', 20, {position: 'start', preferTruncationOnSpace: true});
//=> '…rainbow dragons'

// Without preferTruncationOnSpace
cliTruncate('unicorns rainbow dragons', 20, {position: 'start'});
//=> '…rns rainbow dragons'

cliTruncate('unicorns rainbow dragons', 20, {position: 'middle', preferTruncationOnSpace: true});
//=> 'unicorns…dragons'

cliTruncate('unicorns rainbow dragons', 6, {position: 'end', preferTruncationOnSpace: true});
//=> 'unico…'

// preferTruncationOnSpace has no effect if space isn't found within 3 characters
cliTruncate('unicorns rainbow dragons', 6, {position: 'middle', preferTruncationOnSpace: true});
//=> 'uni…ns'
```

##### truncationCharacter

Type: `string`\
Default: `…`

The character to use at the breaking point.

```js
import cliTruncate from 'cli-truncate';

cliTruncate('unicorns', 5, {position: 'end'});
//=> 'unic…'

cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: '.'});
//=> 'unic.'

cliTruncate('unicorns', 5, {position: 'end', truncationCharacter: ''});
//=> 'unico'
```

## Related

- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes

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