Skip to content

Commit e8a9497

Browse files
committed
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.
1 parent dba22c6 commit e8a9497

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

book/src/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ To override global configuration parameters, create a `config.toml` file located
2323
| `idle-timeout` | Time in milliseconds since last keypress before idle timers trigger. Used for autocompletion, set to 0 for instant. | `400` |
2424
| `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` |
2525
| `auto-info` | Whether to display infoboxes | `true` |
26+
| `tmux-system-clipboard` | Sync with the system clipboard when running in tmux. | `true` |
2627
| `true-color` | Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. | `false` |
2728

2829
`[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.

helix-view/src/clipboard.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use anyhow::Result;
44
use std::borrow::Cow;
55

6+
use crate::editor::Config;
7+
68
pub enum ClipboardType {
79
Clipboard,
810
Selection,
@@ -56,7 +58,7 @@ macro_rules! command_provider {
5658
}};
5759
}
5860

59-
pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
61+
pub fn get_clipboard_provider(config: &Config) -> Box<dyn ClipboardProvider> {
6062
// TODO: support for user-defined provider, probably when we have plugin support by setting a
6163
// variable?
6264

@@ -110,9 +112,17 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
110112
copy => "termux-clipboard-set";
111113
}
112114
} else if env_var_is_set("TMUX") && exists("tmux") {
113-
command_provider! {
114-
paste => "tmux", "save-buffer", "-";
115-
copy => "tmux", "load-buffer", "-";
115+
if config.tmux_system_clipboard {
116+
command_provider! {
117+
// Refresh tmux clipboard, wait a bit for it to be updated and paste it
118+
paste => "sh", "-c", "tmux refresh-client -l; sleep 0.1; tmux save-buffer -";
119+
copy => "tmux", "load-buffer", "-w", "-";
120+
}
121+
} else {
122+
command_provider! {
123+
paste => "tmux", "save-buffer", "-";
124+
copy => "tmux", "load-buffer", "-";
125+
}
116126
}
117127
} else {
118128
#[cfg(target_os = "windows")]

helix-view/src/editor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub struct Config {
105105
/// Whether to display infoboxes. Defaults to true.
106106
pub auto_info: bool,
107107
pub file_picker: FilePickerConfig,
108+
/// Sync with the system clipboard when running in tmux. Defaults to `true`.
109+
pub tmux_system_clipboard: bool,
108110
/// Set to `true` to override automatic detection of terminal truecolor support in the event of a false negative. Defaults to `false`.
109111
pub true_color: bool,
110112
}
@@ -139,6 +141,7 @@ impl Default for Config {
139141
completion_trigger_len: 2,
140142
auto_info: true,
141143
file_picker: FilePickerConfig::default(),
144+
tmux_system_clipboard: true,
142145
true_color: false,
143146
}
144147
}
@@ -214,7 +217,7 @@ impl Editor {
214217
syn_loader,
215218
theme_loader,
216219
registers: Registers::default(),
217-
clipboard_provider: get_clipboard_provider(),
220+
clipboard_provider: get_clipboard_provider(&config),
218221
status_msg: None,
219222
idle_timer: Box::pin(sleep(config.idle_timeout)),
220223
last_motion: None,

0 commit comments

Comments
 (0)