• Stars
    star
    136
  • Rank 266,385 (Top 6 %)
  • Language
    Shell
  • License
    GNU General Publi...
  • Created almost 7 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

System clipboard key bindings for Zsh Line Editor with vi mode. It is similar to what `set clipboard=unnamed` does for vim.

Logo

zsh-system-clipboard

Zsh plugin that adds key bindings support for ZLE (Zsh Line Editor) clipboard operations for vi emulation keymaps. It works under Linux, macOS and Android (via Termux).

demonstration-gif

By default, ZLE has its own clipboard buffer. So, using keys like y inside ZLE's normal mode for yanking operations will not send that yanked text to system clipboard. It will live inside ZLE and using C-v won't paste that text in another program. This plugin synchronizes your system clipboard with ZLE buffers while it's not overriding anything. You can still use ZLE's " register if you want to.

It also synchronizes tmux clipboard buffers if tmux available and the ZSH_SYSTEM_CLIPBOARD_TMUX_SUPPORT variable is set to 'true'. See Options section for more details.

Installation

Using Plugin Managers

Use your favorite plugin manager, e.g. zplug:

zplug "kutsan/zsh-system-clipboard"

Manually

Clone this repository somewhere,

git clone https://github.com/kutsan/zsh-system-clipboard ${ZSH_CUSTOM:-~/.zsh}/plugins/zsh-system-clipboard

Source the zsh-system-clipboard.zsh file in your ~/.zshrc.

source "${ZSH_CUSTOM:-~/.zsh}/plugins/zsh-system-clipboard/zsh-system-clipboard.zsh"

The script zsh-system-clipboard.zsh parses the output of bindkey -M vicmd, bindkey -M emacs, bindkey -M visual in order to rebind your keys (along with the default ones) the ZLE widgets functions that copy from and paste to the system clipboard. This means that you should put all of your bindings before sourcing zsh-system-clipboard.zsh in your ~/.zshrc.

Note: widget functions that replace builtin functions for the emacs keymap are not yet written (see #12).

Options

ZSH_SYSTEM_CLIPBOARD_METHOD

Sets the clipboard method to either of these options:

method value meaning
tmux Use Tmux's buffer as a clipboard - useful on systems without X / wayland, requires set-option -g set-clipboard on in ~/.tmux.conf
xsc Use xsel with 'CLIPBOARD' selection.
xsp Use xsel with 'PRIMARY' selection.
xcc Use xclip with 'CLIPBOARD' selection.
xcp Use xclip with 'PRIMARY' selection.
wlc Use wl-clipboard with 'CLIPBOARD' selection.
wlp Use wl-clipboard with 'PRIMARY' selection.
pb Use pbcopy and pbpaste on Darwin - method used by default on OSx.
termux Use Termux:API - method used by default on Android's termux

ZSH_SYSTEM_CLIPBOARD_DISABLE_DEFAULT_MAPS

If set to a non-empty value, it disables the default bindings zsh-system-clipboard uses. Why would you want to do that?

zsh-system-clipboard modifies your key bindings by reading them in their current state and binds them to their corresponding widgets we implemented which change the system clipboard along the way. This variable enables you to bind the default bindings your way. This is useful if you wish e.g to use the same default bindings but with a certain prefix.

This is the function that's inside zsh-system-clipboard.zsh which actually binds the default keys:

function () {
	local binded_keys i parts key cmd keymap
	for keymap in vicmd visual emacs; do
		binded_keys=(${(f)"$(bindkey -M $keymap)"})
		for (( i = 1; i < ${#binded_keys[@]}; ++i )); do
			parts=("${(z)binded_keys[$i]}")
			key="${parts[1]}"
			cmd="${parts[2]}"
			if (( $+functions[zsh-system-clipboard-$keymap-$cmd] )); then
				eval bindkey -M $keymap $key zsh-system-clipboard-$keymap-$cmd
			fi
		done
	done
}

You can change the line eval bindkey -M $keymap $key zsh-system-clipboard-$keymap-$cmd this way:

				eval bindkey -M $keymap \"\ \"$key zsh-system-clipboard-$keymap-$cmd

And to make this change useful, unbind the single " " with:

bindkey -ar " "

This setup will force you to use space to actually use the system clipboard - whether it's paste or copy.

API

The plugin itself provides a separate cross-platform clipboard API for internal widgets. You can use this API as a standalone function.

To set system clipboard buffer:

zsh-system-clipboard-set "example text"

To get system clipboard buffer to stdout:

zsh-system-clipboard-get

It will show pretty-printed errors via stderr or stdout if something went wrong.

Additional mappings

zsh-system-clipboard emulates all of zsh's standard mappings but with system clipboard support. Some default zle commands are not mapped by default both by us and both by ZSH. However we have the binding zsh-system-clipboard-vicmd-vi-yank-eol which emulates vi-yank-eol which copies text from cursor to the end of the line but we don't map it to anything, no matter what is $ZSH_SYSTEM_CLIPBOARD_DISABLE_DEFAULT_MAPS. To use it, add to your ~/.zshrc:

# Bind Y to yank until end of line
bindkey -M vicmd Y zsh-system-clipboard-vicmd-vi-yank-eol

Thanks

Special thanks to Doron Behar (@doronbehar) for their interests, suggestions, time and pull requests.

Similar Projects

License

GPL-3.0

More Repositories

1

dotfiles

My dotfiles: Experimental, ongoing configuration files, development environment and scripts for various Unix-like systems, text-based command-line applications and interfaces.
Lua
341
star
2

stylelint-config-clean-order

A clean and complete config for stylelint-order.
JavaScript
162
star
3

qrscan-pwa

A simple QR code scanner as a progressive web application, works offline, no ads, no trackers, written in Preact and TypeScript.
TypeScript
12
star
4

homebridge-cosa

Homebridge plugin for Cosa Smart Thermostat.
TypeScript
7
star
5

vim-fetlang

Vim runtime files for Fetlang, the fetish-themed programming language. Makes syntax highlighting possible for vim when writing fetlang files.
Vim Script
6
star
6

fast-syntax-highlighting

Contains the my latest local copy of zsh fast-syntax-highlighting plugin by @psprint. Not sure what happened to @zdharma organization, seems like it's deleted. So, this is just a temporary solution for my git submodules until it's hopefully up again.
Shell
5
star
7

telegram-rss-bot

Self-hosted solution to read your RSS feeds through your own Telegram channel.
TypeScript
4
star
8

reopen-cli

Improvement over `open` utility for macOS and Chrome; open URLs in the browser but instead of creating new tabs every time, use existing ones by mutating them.
JavaScript
4
star
9

config

Sharable configurations files for tools that I use; such as eslint, prettier, stylelint.
JavaScript
3
star
10

weather-me

Simple weather application that uses OpenWeatherMap API. I needed a fast app to simply search for cities and show their current weather conditions. So, it does simply one thing, show the current weather and its details.
TypeScript
3
star
11

kutsan.dev

Source-code of my personel website, written in Preact and TypeScript.
TypeScript
3
star
12

kutsan

Placeholder repository for myself and currently it does nothing useful.
JavaScript
2
star
13

add-idea-to-mysql

Simple web server to test Node.js MySQL module.
JavaScript
2
star
14

soyaendustriyel

React source-code of Soya Endüstriyel, an e-commerce entrepreneur as a professional cleaning products dealer.
TypeScript
2
star
15

chromium-blank-new-tab

Extremely simple and almost no-code blank new tab page extension for Chromium browsers.
HTML
2
star
16

smm-cli

User-friendly command-line interface for managing mountable local and remote devices with SSHFS. Optionally powered with your `~/.ssh/config/` file Host configurations.
Shell
2
star
17

chore-strike

Simple web app to manage basic home chores with its todo-list style panel.
TypeScript
1
star