-
Notifications
You must be signed in to change notification settings - Fork 2
/
rm-newlines-and-paste
executable file
·48 lines (43 loc) · 1.85 KB
/
rm-newlines-and-paste
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
#
# On LINUX (Gnome):
# Reads the selection clipboard, removes all new lines, and then saves it into main clipboard.
# To setup a shortcut in Gnome:
# - Keyboard > Keyboard Shortcuts > <path-to>/bashutils/rm-newlines-and-paste
# - Set shortcut to "Ctrl+Alt+V" (see keyup below for clear the conflict)
#
# If it fails, check Gnome logs: journalctl -f
#
# On MAC:
# It is much better to do this by:
# - Creating a Shortcuts entry that gets clipboard, replaces \n chars, updates clipboard, and
# finally runs an applescript that does
# tell application "System Events"
# keystroke "v" using {command down}
# end tell
# - Set "Use as Quick Action" -> Services Menu -> Run with a shortcut of choice (I prefer Ctrl+Cmd+V).
# - Give app-specific accessibility permissions.
#
# ChatGPT & Gokberk Cinbis, Dec 2024
# [TODO] wayland support
unameOut="$(\uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
*) machine="UNKNOWN:${unameOut}"
esac
#echo ${machine}
if [[ $machine == "Mac" ]]; then
pbpaste | sed -z 's/\n/ /g' | pbcopy # not yet tested but setting up a "Shortcuts" solution is much better anyways.
echo "Clipboard contents have been updated to remove all newlines."
else
#xsel -op | sed -z 's/\n/ /g' | xsel -ib
xclip -o -selection clipboard | tr -d '\n' | xclip -selection clipboard
# echo "Clipboard contents have been updated to remove all newlines."
# Doesn't work: xdotool key ctrl+v
# Doesn't work: xdotool key --clearmodifiers ctrl+v
xdotool keyup ctrl+alt+v sleep 0.2 key ctrl+v # assuming ctrl+alt+v shortcut
# keyup trick is from https://askubuntu.com/questions/1332788/xdotool-does-not-work-with-gnome-keyboard-shortcuts
fi