Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
  • 한국어
GitHub
  • Introduction
  • Installation
    • Default Shell
  • Getting Started
    • Quick Tour
    • Moving Around the System
    • Thinking in Nu
    • Nushell Cheat Sheet
  • Nu Fundamentals
    • Types of Data
    • Loading Data
    • Data Formats
    • Working with SQLite
    • Pipelines
    • Working with Strings
    • Regular Expressions
    • Working with Binary Data
    • Working with Lists
    • Working with Records
    • Working with Numbers
    • Working with Tables
    • Working with Paths
    • Navigating and Accessing Structured Data
    • Special Variables
  • Programming in Nu
    • Custom Commands
    • Aliases
    • Operators
    • Variables
    • Control Flow
    • Scripts
    • Modules
      • Using Modules
      • Creating Modules
    • Overlays
    • Sorting
    • Testing your Nushell Code
    • Debugging Scripts
    • Language Server
    • Best Practices
  • Nu as a Shell
    • Configuration
    • Configuration Reference
    • Environment
    • Stdout, Stderr, and Exit Codes
    • Working with the Terminal
    • Running System (External) Commands
    • How to Configure 3rd Party Prompts
    • Directory Stack
    • Reedline, Nu's Line Editor
    • Custom Completions
    • Externs
    • Coloring and Theming in Nu
    • Hooks
    • Background Jobs
  • Coming to Nu
    • Coming from Bash
    • Coming from CMD.EXE
    • Coming from PowerShell
    • Nu map from other shells and domain specific languages
    • Nu Map from Imperative Languages
    • Nu Map from Functional Languages
    • Nushell operator map
  • Design Notes
    • How Nushell Code Gets Run
  • (Not So) Advanced
    • Standard Library
      • std/assert
      • std/bench
      • std/clip
      • std/config
      • std/dt
      • std/formats
      • std/help
      • std/input
      • std/iter
      • std/log
      • std/math
      • std/random
      • std/util
      • std/xml
      • std-rfc
    • Dataframes
      • Dataframe Operations
      • Column Selectors
      • Math Expressions
      • String Expressions
      • Dates and Times in Dataframes
      • Masks and Conditionals
      • Aggregations and Window Functions
    • Metadata
    • Creating Your Own Errors
    • Parallelism
    • Plugins
    • explore
    • Building TUIs with tui
    • MCP Server

std/clip

The std/clip module copies to and pastes from the system clipboard through the terminal. Its commands don't talk to the operating system. They send an OSC 52 escape sequence, which asks the terminal emulator to do the copying or pasting. That also works over SSH, and the clipboard they reach is the one on the machine where the terminal runs.

use std/clip

std/clip is the only standard library module that use std and use std * leave out, so there is no std clip copy52. Import it on its own, as above.

  • Terminal support
  • clip copy52
  • clip paste52
  • clip prefix
  • Native clipboard commands

Terminal support

Everything here depends on the terminal. It has to understand OSC 52, and many terminals have settings that turn copying or pasting through OSC 52 on or off. Terminal multiplexers such as tmux, screen and zellij sit between Nushell and the terminal and may block the sequence or need configuration to pass it on.

clip copy52

clip copy52 copies its input to the clipboard:

"Hello" | clip copy52

It returns nothing. It writes the escape sequence, with the text encoded as base64, to standard output, so it only reaches the terminal when stdout is the terminal. Redirect it to a file and the file gets the escape sequence instead.

A string is copied as it is. A table, record or list is first rendered with table -e, the way it would look on the screen, and that text is copied. The rendering has no colors unless you pass --ansi (-a), which keeps the ANSI color codes in the copied text. --ansi doesn't affect strings, so a string that already contains ANSI codes is copied with them. Remove them first with ansi strip if you don't want them.

clip paste52

clip paste52 asks the terminal for the clipboard's content and returns it as a string. It sends the OSC 52 query with term query and reads the terminal's reply from standard input.

If the terminal doesn't answer, clip paste52 waits until you press a key and then fails, because a key press isn't the reply it expects. When stdin isn't a terminal, it fails at once. The error is Terminal did not responds to OSC 52 paste request., with the hint Check if your terminal supports OSC 52.

clip prefix

clip prefix adds a prefix to the start of every line of its input and returns the result as a string. Like clip copy52, it first renders a table, record or list with table -e. The last line of a rendered table is empty, and it gets the prefix too:

[1 2 3] | clip prefix "# => "
# => # => ╭───┬───╮
# => # => │ 0 │ 1 │
# => # => │ 1 │ 2 │
# => # => │ 2 │ 3 │
# => # => ╰───┴───╯
# => # =>

At the prompt, where standard output is a terminal, the table that clip prefix renders contains ANSI color codes, and clip copy52 copies a string with its codes. To copy output for documentation written the way this book shows output, strip the codes before you copy:

ls | clip prefix '# => ' | ansi strip | clip copy52

Native clipboard commands

Nushell also has built-in clip copy and clip paste commands that use the operating system's clipboard directly instead of OSC 52. They are behind the native-clip experimental option and exist only when it is turned on, for example with:

nu --experimental-options '[native-clip]'

Without the option, clip copy isn't a command at all, and Nushell looks for an external program named clip. With it, the commands work on macOS, Windows and Linux. On other platforms they exist but fail with Clipboard not supported. Unlike the std/clip commands, they don't need a terminal, but they reach only the clipboard of the machine Nushell runs on, not the one at the other end of an SSH session. See help clip copy and help clip paste for their flags.

Edit this page on GitHub
Contributors: fdncred
Prev
std/bench
Next
std/config