From e8a94973cd248decac884b5de4143ebcccd23c0c Mon Sep 17 00:00:00 2001 From: Flakebi Date: Wed, 22 Dec 2021 21:01:22 +0100 Subject: [PATCH] Sync tmux clipboard and system clipboard When copying, also write the content into the system clipboard. When pasting, request tmux to refresh its paste buffer from the system clipboard, wait for the update to propagate and the request the clipboard content from tmux. With `set -g set-clipboard on` in tmux, this enables sharing the system clipboard with a helix running in tmux, running in an ssh session, running in alacritty. The need for a wait is unfortunate, but I didn't find a better way. Tmux asks the terminal for the clipboard content and updates its buffer after it got an answer. The answer may come or may not. It may take long, e.g. when running through a slow ssh connection. The feature works well on alacritty. On konsole, it doesn't do anything, but doesn't harm either. On xterm, `tmux refresh-client -l` prints gibberish for me, also without using helix, so I added an option to disable this feature. --- book/src/configuration.md | 1 + helix-view/src/clipboard.rs | 18 ++++++++++++++---- helix-view/src/editor.rs | 5 ++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index 33a933b2b96c3..e20500db7d4aa 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -23,6 +23,7 @@ To override global configuration parameters, create a `config.toml` file located | `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` | | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` | | `auto-info` | Whether to display infoboxes | `true` | +| `tmux-system-clipboard` | Sync with the system clipboard when running in tmux. | `true` | | `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` | `[editor.filepicker]` section of the config. Sets options for file picker and global search. All but the last key listed in the default file-picker configuration below are IgnoreOptions: whether hidden files and files listed within ignore files are ignored by (not visible in) the helix file picker and global search. There is also one other key, `max-depth` available, which is not defined by default. diff --git a/helix-view/src/clipboard.rs b/helix-view/src/clipboard.rs index a492652d87013..53bd349dea887 100644 --- a/helix-view/src/clipboard.rs +++ b/helix-view/src/clipboard.rs @@ -3,6 +3,8 @@ use anyhow::Result; use std::borrow::Cow; +use crate::editor::Config; + pub enum ClipboardType { Clipboard, Selection, @@ -56,7 +58,7 @@ macro_rules! command_provider { }}; } -pub fn get_clipboard_provider() -> Box { +pub fn get_clipboard_provider(config: &Config) -> Box { // TODO: support for user-defined provider, probably when we have plugin support by setting a // variable? @@ -110,9 +112,17 @@ pub fn get_clipboard_provider() -> Box { copy => "termux-clipboard-set"; } } else if env_var_is_set("TMUX") && exists("tmux") { - command_provider! { - paste => "tmux", "save-buffer", "-"; - copy => "tmux", "load-buffer", "-"; + if config.tmux_system_clipboard { + command_provider! { + // Refresh tmux clipboard, wait a bit for it to be updated and paste it + paste => "sh", "-c", "tmux refresh-client -l; sleep 0.1; tmux save-buffer -"; + copy => "tmux", "load-buffer", "-w", "-"; + } + } else { + command_provider! { + paste => "tmux", "save-buffer", "-"; + copy => "tmux", "load-buffer", "-"; + } } } else { #[cfg(target_os = "windows")] diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index fd6eb4d57e030..8b53bfb2bdb82 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -105,6 +105,8 @@ pub struct Config { /// Whether to display infoboxes. Defaults to true. pub auto_info: bool, pub file_picker: FilePickerConfig, + /// Sync with the system clipboard when running in tmux. Defaults to `true`. + pub tmux_system_clipboard: bool, /// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`. pub true_color: bool, } @@ -139,6 +141,7 @@ impl Default for Config { completion_trigger_len: 2, auto_info: true, file_picker: FilePickerConfig::default(), + tmux_system_clipboard: true, true_color: false, } } @@ -214,7 +217,7 @@ impl Editor { syn_loader, theme_loader, registers: Registers::default(), - clipboard_provider: get_clipboard_provider(), + clipboard_provider: get_clipboard_provider(&config), status_msg: None, idle_timer: Box::pin(sleep(config.idle_timeout)), last_motion: None,