diff --git a/.config/fish/functions/fish_primary_copy.fish b/.config/fish/functions/fish_primary_copy.fish new file mode 100644 index 00000000..d2a1cd87 --- /dev/null +++ b/.config/fish/functions/fish_primary_copy.fish @@ -0,0 +1,7 @@ +function fish_primary_copy + if type -q pbcopy + commandline | pbcopy + else if type -q xsel + commandline | xsel + end +end diff --git a/.config/fish/functions/fish_primary_paste.fish b/.config/fish/functions/fish_primary_paste.fish new file mode 100644 index 00000000..1cf2df0e --- /dev/null +++ b/.config/fish/functions/fish_primary_paste.fish @@ -0,0 +1,29 @@ +function fish_primary_paste + set -l data + if type -q pbpaste + set data (pbpaste) + else if type -q xsel + # Return if `xsel` failed. + # That way any xsel error is printed (to show e.g. a non-functioning X connection), + # but we don't print the redundant (and overly verbose for this) commandline error. + # Also require non-empty contents to not clear the buffer. + if not set data (xsel) + return 1 + end + end + # Also split on \r to turn it into a newline, + # otherwise the output looks really confusing. + set data (string split \r -- $data) + + # If the current token has an unmatched single-quote, + # escape all single-quotes (and backslashes) in the paste, + # in order to turn it into a single literal token. + # + # This eases pasting non-code (e.g. markdown or git commitishes). + if __fish_commandline_is_singlequoted + set data (string replace -ra "(['\\\])" '\\\\\\\$1' -- $data) + end + if test -n "$data" + commandline -i -- $data + end +end diff --git a/.config/fish/functions/fish_user_key_bindings.fish b/.config/fish/functions/fish_user_key_bindings.fish index d51a9495..7506f920 100644 --- a/.config/fish/functions/fish_user_key_bindings.fish +++ b/.config/fish/functions/fish_user_key_bindings.fish @@ -11,6 +11,9 @@ function fish_user_key_bindings bind -M insert \ce edit_command_buffer bind -m insert \ce edit_command_buffer + bind \cv fish_primary_paste + bind \cx fish_primary_copy + ## fzf if type -pq fzf source $fish_config_path/fzf.fish