Skip to content

Commit

Permalink
Sync tmux clipboard and system clipboard
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Flakebi committed Dec 23, 2021
1 parent dba22c6 commit e8a9497
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use anyhow::Result;
use std::borrow::Cow;

use crate::editor::Config;

pub enum ClipboardType {
Clipboard,
Selection,
Expand Down Expand Up @@ -56,7 +58,7 @@ macro_rules! command_provider {
}};
}

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

Expand Down Expand Up @@ -110,9 +112,17 @@ pub fn get_clipboard_provider() -> Box<dyn ClipboardProvider> {
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")]
Expand Down
5 changes: 4 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e8a9497

Please sign in to comment.