ansi-escapes

中文文档
7.3.0发布于 2026/02/05MITTypeScriptES Module近 30 天下载 5.2亿

ANSI escape codes for manipulating the terminal

原始使用文档
文档目录42 节
  1. Install
  2. Usage
  3. API
  4. cursorTo(x, y?)
  5. cursorMove(x, y?)
  6. cursorUp(count)
  7. cursorDown(count)
  8. cursorForward(count)
  9. cursorBackward(count)
  10. cursorLeft
  11. cursorSavePosition
  12. cursorRestorePosition
  13. cursorGetPosition
  14. cursorNextLine
  15. cursorPrevLine
  16. cursorHide
  17. cursorShow
  18. eraseLines(count)
  19. eraseEndLine
  20. eraseStartLine
  21. eraseLine
  22. eraseDown
  23. eraseUp
  24. eraseScreen
  25. scrollUp
  26. scrollDown
  27. clearViewport
  28. clearScreen
  29. clearTerminal
  30. enterAlternativeScreen
  31. exitAlternativeScreen
  32. beginSynchronizedOutput
  33. endSynchronizedOutput
  34. synchronizedOutput(text)
  35. beep
  36. link(text, url)
  37. image(filePath, options?)
  38. setCwd(path?)
  39. iTerm.setCwd(path?)
  40. ConEmu.setCwd(path?)
  41. iTerm.annotation(message, options?)
  42. Related

ansi-escapes

ANSI escape codes for manipulating the terminal

Install

npm install ansi-escapes

Usage

import ansiEscapes from 'ansi-escapes';

// Moves the cursor two rows up and to the left
process.stdout.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
//=> '\u001B[2A\u001B[1000D'

Or use named exports...

import {cursorUp, cursorLeft} from 'ansi-escapes';

// etc, as above...

You can also use it in the browser with Xterm.js:

import ansiEscapes from 'ansi-escapes';
import {Terminal} from 'xterm';
import 'xterm/css/xterm.css';

const terminal = new Terminal({…});

// Moves the cursor two rows up and to the left
terminal.write(ansiEscapes.cursorUp(2) + ansiEscapes.cursorLeft);
//=> '\u001B[2A\u001B[1000D'

API

cursorTo(x, y?)

Set the absolute position of the cursor. x0 y0 is the top left of the screen.

cursorMove(x, y?)

Set the position of the cursor relative to its current position.

cursorUp(count)

Move cursor up a specific amount of rows. Default is 1.

cursorDown(count)

Move cursor down a specific amount of rows. Default is 1.

cursorForward(count)

Move cursor forward a specific amount of columns. Default is 1.

cursorBackward(count)

Move cursor backward a specific amount of columns. Default is 1.

cursorLeft

Move cursor to the left side.

cursorSavePosition

Save cursor position.

cursorRestorePosition

Restore saved cursor position.

cursorGetPosition

Get cursor position.

cursorNextLine

Move cursor to the next line.

cursorPrevLine

Move cursor to the previous line.

cursorHide

Hide cursor.

cursorShow

Show cursor.

eraseLines(count)

Erase from the current cursor position up the specified amount of rows.

eraseEndLine

Erase from the current cursor position to the end of the current line.

eraseStartLine

Erase from the current cursor position to the start of the current line.

eraseLine

Erase the entire current line.

eraseDown

Erase the screen from the current line down to the bottom of the screen.

eraseUp

Erase the screen from the current line up to the top of the screen.

eraseScreen

Erase the screen and move the cursor the top left position.

scrollUp

Scroll display up one line.

scrollDown

Scroll display down one line.

clearViewport

Clear only the visible terminal screen (viewport) without affecting scrollback buffer or terminal state.

This is a safer alternative to clearScreen that works consistently across terminals.

clearScreen

Clear the terminal screen.

[!WARNING] This uses RIS (Reset to Initial State) which may also clear scrollback buffer in some terminals (xterm.js, VTE) and reset terminal modes. Consider using clearViewport() for safer viewport-only clearing.

clearTerminal

Clear the whole terminal, including scrollback buffer. (Not just the visible part of it)

enterAlternativeScreen

Enter the alternative screen.

exitAlternativeScreen

Exit the alternative screen, assuming enterAlternativeScreen was called before.

beginSynchronizedOutput

Begin synchronized output to reduce flicker during renders.

endSynchronizedOutput

End synchronized output.

synchronizedOutput(text)

Wrap output in synchronized output sequences to reduce flicker during renders.

beep

Output a beeping sound.

link(text, url)

Create a clickable link.

Supported terminals. Use supports-hyperlinks to detect link support.

image(filePath, options?)

Display an image.

See term-img for a higher-level module.

input

Type: Buffer

Buffer of an image. Usually read in with fs.readFile().

options

Type: object

width
height

Type: string | number

The width and height are given as a number followed by a unit, or the word "auto".

  • N: N character cells.
  • Npx: N pixels.
  • N%: N percent of the session's width or height.
  • auto: The image's inherent size will be used to determine an appropriate dimension.
preserveAspectRatio

Type: boolean
Default: true

setCwd(path?)

Type: string
Default: process.cwd()

Set the current working directory for both iTerm2 and ConEmu.

iTerm.setCwd(path?)

Type: string
Default: process.cwd()

Inform iTerm2 of the current directory to help semantic history and enable Cmd-clicking relative paths.

ConEmu.setCwd(path?)

Type: string
Default: process.cwd()

Inform ConEmu about shell current working directory.

iTerm.annotation(message, options?)

Creates an escape code to display an "annotation" in iTerm2.

An annotation looks like this when shown:

See the iTerm Proprietary Escape Codes documentation for more information.

message

Type: string

The message to display within the annotation.

The | character is disallowed and will be stripped.

options

Type: object

length

Type: number
Default: The remainder of the line

Nonzero number of columns to annotate.

x

Type: number
Default: Cursor position

Starting X coordinate.

Must be used with y and length.

y

Type: number
Default: Cursor position

Starting Y coordinate.

Must be used with x and length.

isHidden

Type: boolean
Default: false

Create a "hidden" annotation.

Annotations created this way can be shown using the "Show Annotations" iTerm command.

  • ansi-styles - ANSI escape codes for styling strings in the terminal

版本与发布记录

ansi-escapes 共发布 28 个版本,首次发布于 2015/08/14;过去 12 个月发布了 1 个版本。

版本发布日期距今
7.3.0latest2026/02/058 个月前
7.2.02025/11/041 年前
7.1.12025/09/231.1 年前
7.1.02025/09/101.2 年前
7.0.02024/05/032 年前
6.2.12024/03/242 年前

关于 ansi-escapes 的常见问题

ansi-escapes 是什么?

ANSI escape codes for manipulating the terminal

如何安装 ansi-escapes?

在项目目录运行 npm install ansi-escapes,或使用 pnpm add / yarn add / bun add。

ansi-escapes 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

ansi-escapes 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

ansi-escapes 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

ansi-escapes 可以商用吗?

ansi-escapes 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

ansi-escapes 的下载量有多少?

根据 npm 官方下载统计,ansi-escapes 在 2026/10/05 – 2026/11/03 这 30 天内被下载 524,105,237 次,日均约 17,470,175 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

ansi-escapes 还在维护吗?

最新版本 7.3.0 发布于 2026/02/05(8 个月前);至今共发布 28 个版本,其中过去 12 个月发布了 1 个;首次发布于 2015/08/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。