avataralpha2phi

Summary

The webpage provides an in-depth guide on using Operating System Command (OSC) 52 in Neovim to facilitate clipboard operations between a local system and a remote SSH session.

Abstract

The article "Neovim 101 — OSC 52" delves into the use of OSC 52 within Neovim, a popular text editor. It explains how OSC sequences, initially defined by Xterm, can be utilized for various terminal operations, with a focus on manipulating the clipboard over an SSH session. The guide offers practical examples and illustrations to demonstrate how to configure Neovim to copy data from a remote session to the local clipboard, including the use of aliases, scripts from the Chromium project, and plugins like nvim-osc52 and smartyank.nvim. The article is part of the "Neovim 101" series and provides readers with the necessary configuration files and instructions to implement the discussed features, enhancing the efficiency of working with remote systems.

Opinions

  • The author emphasizes the utility of OSC 52 for developers who frequently work with remote systems, highlighting its ability to streamline the process of copying data to the local clipboard.
  • The article suggests that using OSC 52 can significantly improve the workflow for users of Neovim, making it more efficient and user-friendly.
  • The author provides a personal use case, mentioning daily work on AWS EC2 instances, which adds credibility to the practicality of the OSC 52 sequence in real-world scenarios.
  • The recommendation of the AI service ZAI.chat at the end of the article implies the author's belief in the value of cost-effective, high-performance AI tools for enhancing productivity.
  • By offering a repository with configuration files and showcasing the functionality through GIFs, the author demonstrates a commitment to providing comprehensive and accessible resources for Neovim users.

Neovim 101 — OSC 52

Let’s check out Operating System Command (OSC) and OSC 52.

Neovim 101 — OSC 52

In this article, we will learn the basics of OSC (Operating System Command), and how to use OSC 52 to transport data from a remote SSH session to the local system clipboard.

This article is part of the Neovim 101 series.

The Neovim configuration files are available in this repository.

Getting Started

In the previous article on terminal and shell, we talked about Control Sequence Introducer (CSI), and how to configure Alacritty, Kitty, and WezTerm to enable CSI u which uses control sequences starting with ESC [ or \x9B.

In this article, we will go through OSC which uses control sequences starting with ESC ] or \x9D.

OSC

OSC stands for Operating System Command. Originally OSC sequences were defined by Xterm, but many are also supported by other modern terminal emulators, e.g., Alacritty, Kitty, and WezTerm.

Let’s go through several examples.

The following command sets the style to the bold and red foreground.

  • \x1b[ — the escape sequence
  • 1 — the bold color/graphic mode. Refer to this table for details
  • 31 — the red color code. Refer to this table for details
print "\x1b[1;31mHello Alpha2Phi"
ANSI OSC Command

In general, the command looks like this

ESC[{graphic mode};{color code};{...}m

Let’s look at another example.

print "\x1b[3;30;44mHello Alpha2Phi"
ANSI OSC Command
  • For the graphic mode, we set it to italic (3)
  • For the text foreground color, we set it to black (30)
  • For the text background color, we set it to blue (44)

OSC 52

OSC can be used to configure the window title, text background/foreground color, cursor shape, clipboard operation, screen mode, keyboard settings, etc.

For Neovim, we will focus on OSC 52 sequence that can be used to manipulate the clipboard operations.

ESC]52;c;[base64 data]\a

OSC 52 is particularly useful when we want to copy selected data from a remote SSH session to the local system clipboard.

For example, from a remote SSH session, we can use the following alias to copy data from the remote host to the local system clipboard.

alias clip="base64 | xargs -0 printf '\e]52;c;%s\007'"

Let’s try this out using an AWS EC2 instance.

Scenario — AWS EC2 Instance

For my projects, I need to use an AWS EC2 instance daily.

In the screenshot below, we use Alacritty to ssh to the AWS EC2 instance using the following command.

ssh -i /path/key-pair-name.pem instance-user-name@instance-public-dns-name
SSH to AWS EC2 instance

Alacritty supports OSC 52, and we can use the OSC 52 control sequence to copy data from the AWS EC2 instance to the local computer clipboard.

Copy Data from SSH Session

In the screenshot above, in the left window, we copy the data using OSC 52 in the remote host and paste the copied data into the local console in the right window.

Chromium Project

The Chromium project provides useful scripts to test out OSC 52, including a Vim plugin.

For example, we can use the osc52.sh script to copy data from a remote host.

osc52.sh

Plugins

There are plugins available for Vim and Neovim that we can use to copy data from a remote host.

From the server, once we install the plugins, any yanked text from within Neovim is made available to the local host.

nvim-osc52

nvim-osc52 is a Neovim plugin to copy text to the system clipboard using the ANSI OSC 52 sequence.

We install this plugin using packer.nvim.

From the AWS EC2 instance, we clone the Neovim 101 repository and perform a :PackerSync.

Neovim 101 Repository

With Neovim and nvim-osc52 installed in the remote host, now we can copy any yanked text to the local system clipboard using Neovim.

nvim-osc52

smartyank.nvim

smartyank.nvim is an opinionated (yet customizable) yank. It utilizes the TextYankPost event to detect intentional yank operations (by testing vim.v.operator) and:

  • Highlight yanked text
  • Copy yanked text to the system clipboard (regardless of clipboard setting)
  • If tmux is available, copy to a tmux clipboard buffer (enables history)
  • If an SSH session is detected, use OSC 52 to copy to the terminal host clipboard

Once installed, we can seamlessly copy yanked text from the remote SSH session to the local system clipboard.

use({
  "ibhagwan/smartyank.nvim",
  config = function()
   require("smartyank").setup({})
  end,
 })

Check out the article Learn Neovim The Practical Way for all the Vim/Neovim articles!

If you are not a Medium member yet and want to become one, click here. You will gain unlimited access to all Medium articles and support my work directly.

References

Vim
Programming
Software Development
Coding
Software Engineering
Recommended from ReadMedium