Skip to content

Instantly share code, notes, and snippets.

@progzone122
Last active December 20, 2024 13:20
Show Gist options
  • Save progzone122/32061f068c345a0644ad9a47ad390b82 to your computer and use it in GitHub Desktop.
Save progzone122/32061f068c345a0644ad9a47ad390b82 to your computer and use it in GitHub Desktop.
Fixing the wps-office clipboard in Linux on Wayland
#!/bin/bash
# Credits
# DiabloSat - https://github.com/progzone122
# Andrés Alberto - https://gist.github.com/andriandreo
while true; do
# Capture the current clipboard content
clipboard_content=$(wl-paste)
# Remove trailing newlines using sed
cleaned_content=$(echo "$clipboard_content" | sed ':a; /^$/{$d; N;}; /\n$/ba')
# Place the cleaned content back into the clipboard
echo -n "$cleaned_content" | xclip -selection clipboard
# Use xclip to set the clipboard content to itself (optional redundancy)
xclip -selection clipboard -o | wl-copy
# Wait for 0.5 seconds before the next iteration
sleep 0.5
done

Fixing Clipboard Issue for WPS Office on Linux with Wayland

WPS Office uses XWayland for operation, which causes issues with copying text from applications running via Wayland (such as Chromium, Firefox, and others).

This happens due to the incompatibility of the clipboard between Wayland and XWayland.

As a result, the WPS Office application cannot access the clipboard content, leading to errors when attempting to paste text.

To fix this, you can use the following workaround:

  • Install dependencies:
    • Arch Linux: sudo pacman -S xclip wl-clipboard
    • Ubuntu/Debian: sudo apt-get install xclip wl-clipboard
  • Create a Bash script (e.g., clipboard.sh):
#!/bin/bash
# Credits
# DiabloSat - https://github.com/progzone122
# Andrés Alberto - https://gist.github.com/andriandreo
while true; do
    # Capture the current clipboard content
    clipboard_content=$(wl-paste)
    
    # Remove trailing newlines using sed
    cleaned_content=$(echo "$clipboard_content" | sed ':a; /^$/{$d; N;}; /\n$/ba')
    
    # Place the cleaned content back into the clipboard
    echo -n "$cleaned_content" | xclip -selection clipboard
    
    # Use xclip to set the clipboard content to itself (optional redundancy)
    xclip -selection clipboard -o | wl-copy
    
    # Wait for 0.5 seconds before the next iteration
    sleep 0.5
done
  • Make the Bash script executable:
chmod +x clipboard.sh
  • Configure the Bash script to run at system startup using any convenient method:

    • For hyprland:

    Edit the file hyprland.conf:

    # fix buffer copy&paste between wayland and xwayland
    exec-once = sleep 2 && $HOME/.config/hypr/clipboard.sh # Enter your path to the script
    • For i3wm:

    Edit the file config:

    # fix buffer copy&paste between Wayland and XWayland
    exec --no-startup-id $HOME/.config/i3/clipboard.sh & # Enter your path to the script
    • For bspwm:

    Edit the file ~/.config/bspwm/bspwmrc:

    nohup $HOME/.config/bspwm/clipboard.sh & # Enter your path to the script
    • For KDE:

    Create file ~/.config/autostart/clipboard.desktop

    [Desktop Entry]
    Type=Application
    Name=my-script
    GenericName=my-script
    Comment=My Script
    Exec='absolute_path_to_your_script'
    Terminal=false
    • For Gnome:

    Create file ~/.config/autostart/clipboard.desktop

    [Desktop Entry]
    Name=my-script
    GenericName=my-script
    Comment=My Script
    Exec='absolute_path_to_your_script'
    Terminal=false
    Type=Application
    X-GNOME-Autostart-enabled=true

Исправление буфера обмена wps-office в Linux на Wayland

WPS Office использует XWayland для работы, что вызывает проблемы с копированием текста из приложений, запущенных через Wayland (такие как Chromium, Firefox и другие)

Это происходит из-за несовместимости буфера обмена между Wayland и XWayland.

В результате, приложение WPS Office не может получить доступ к содержимому буфера обмена, что приводит к ошибкам при попытке вставки текста.

Для исправления можно использовать данный хак:

  • Устанавливаем зависимости:
    • Arch Linux: sudo pacman -S xclip wl-clipboard
    • Ubuntu/Debian: sudo apt-get install xclip wl-clipboard
  • Создайте bash скрипт (например clipboard.sh):
#!/bin/bash
# Credits
# DiabloSat - https://github.com/progzone122
# Andrés Alberto - https://gist.github.com/andriandreo
while true; do
    # Capture the current clipboard content
    clipboard_content=$(wl-paste)
    
    # Remove trailing newlines using sed
    cleaned_content=$(echo "$clipboard_content" | sed ':a; /^$/{$d; N;}; /\n$/ba')
    
    # Place the cleaned content back into the clipboard
    echo -n "$cleaned_content" | xclip -selection clipboard
    
    # Use xclip to set the clipboard content to itself (optional redundancy)
    xclip -selection clipboard -o | wl-copy
    
    # Wait for 0.5 seconds before the next iteration
    sleep 0.5
done
  • Сделайте bash скрипт исполняемым
chmod +x clipboard.sh
  • Настройте запуск bash скрипта при запуске системы любым удобным способом
    • hyprland:

      Файл hyprland.conf

      # fix buffer copy&paste between wayland and xwayland
      exec-once = sleep 2 && $HOME/.config/hypr/clipboard.sh # Введите свой путь до скрипта
      
    • i3wm:

      Файл config

      # fix buffer copy&paste between wayland and xwayland
      exec --no-startup-id $HOME/.config/i3/clipboard.sh & # Введите свой путь до скрипта
      
    • bspwm:

      Файл ~/.config/bspwm/bspwmrc

      nohup $HOME/.config/bspwm/clipboard.sh & # Введите свой путь до скрипта
      
    • KDE

      Создайте файл ~/.config/autostart/clipboard.desktop

      [Desktop Entry]
      Type=Application
      Name=my-script
      GenericName=my-script
      Comment=My Script
      Exec='абсолютный_путь_до_вашего_скрипта'
      Terminal=false
    • Gnome

      Создайте файл ~/.config/autostart/clipboard.desktop

      [Desktop Entry]
      Name=my-script
      GenericName=my-script
      Comment=My Script
      Exec='абсолютный_путь_до_вашего_скрипта'
      Terminal=false
      Type=Application
      X-GNOME-Autostart-enabled=true
@Cvtq
Copy link

Cvtq commented May 23, 2024

Also fixes VirtualBox Shared Clipboard on Wayland because it is supposed to work with the X11 clipboard only.

@progzone122
Copy link
Author

Also fixes VirtualBox Shared Clipboard on Wayland because it is supposed to work with the X11 clipboard only.

🔥🔥🔥

@Kirunx
Copy link

Kirunx commented Sep 7, 2024

For some reason clips in a lot of blank lines. Any way to fix? Using hyprland

@progzone122
Copy link
Author

For some reason clips in a lot of blank lines. Any way to fix? Using hyprland

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment