Pasting from clipboard includes unicode characters

I’m trying to configure my kakrc copy/paste capabilities. Here’s a snippet of my kakrc:

# System clipboard handling
# ─────────────────────────

evaluate-commands %sh{
    if [ -n "$SSH_TTY" ]; then
        copy='printf "\033]52;;%s\033\\" $(base64 | tr -d "\n") > $( [ -n "$kak_client_pid" ] && echo /proc/$kak_client_pid/fd/0 || echo /dev/tty )'
        paste='printf "paste unsupported through ssh"'
        backend="OSC 52"
    else
        case $(uname) in
            Linux)
                if [ -n "$WAYLAND_DISPLAY" ]; then
                    copy="wl-copy -p"; paste="wl-paste -p"; backend=Wayland
                else
                    copy="xclip -i"; paste="xclip -o"; backend=X11
                fi
                ;;
            Darwin)  copy="pbcopy"; paste="pbpaste"; backend=OSX ;;
        esac
    fi

}

map global user "c" "<a-|> xclip -selection clipboard -in<ret>" -docstring "copy to clipboard"
map global user "p" "<!> xclip -selection clipboard -out<ret>" -docstring "paste from clipboard"

When pasting from session to session, everything works just fine.

However, when I copy something from, lets say, outside of the terminal, and paste it in my kak session, I get these “?” unicode characters. For example:

image

Am I missing something with my kakrc? Apologize for being a noob here, I’m still fairly new to Kakoune. Thank you for any help and assistance!

Since the unknown characters appear at the end of the line, I assume you might be copying from some context which has line endings represented with \r\n while Unix environment relies only on \n as line ending marker, so the question mark might be the unprintable \r character(carriage return character, ASCII decimal value 13).

In addition to cipharius’ suggestion, what happens if you paste the same content directly in your terminal (for example, you may try to suspend Kakoune with Ctrl-z, then after pasting go back to Kakoune with the fg command)?

You may also want to pipe what you are pasting to cat -A to have a better idea of what the troublesome characters are.

Can I add something to my paste command to check for \r characters and remove them if they’re present?

When I paste in my terminal, it looks completely fine. I guess I should also mention I am using a WSL client. I never had this problem when I would use my u2204 VM.

That would do it, \r\n line ending markers are the standard on Windows.

You can pipe your clipboard content through sed or tr. If you’re fine with stripping all \r characters, then you can use tr to replace them with empty string, or if you want to convert only \r\n to \n then you should use sed with s/\r\n/\n/.

Indeed, without piping to cat -A (for example), carriage returns would not show up because they just bring the cursor to the leftmost position before printing the next line. In any case, cipharius’ suggestion to pipe clipboard content through sed or tr will work.

Awesome! thank you @cipharius and @ftonneau , that worked. Looks like WSL was the issue.

For those wondering, this link also gave me some additional context for using WSL and pasting: Registers Clipboard · mawww/kakoune Wiki · GitHub

The final addition to my kakrc looks like this:

map global user "c" "<a-|> xclip -selection clipboard -in<ret>" -docstring "copy to clipboard"
map global user "p" "<!> powershell.exe Get-Clipboard | tr -d '\r' <ret>" -docstring "paste from clipboard"

Thank you all!