1
I have this script that I assign t oa keyboard shortcut to simulate pasting through middle click:
#!/bin/bash
aa=0
for randstring in `xsel`
do
if [[ "$randstring" =~ [ěščřžýáíéúůóťďň] ]]
then
xxx=`xsel|sed 's/ě/\\\[ecaron]/g' |sed 's/š/\\\[scaron]/g' |sed 's/č/\\\[ccaron] g' |sed 's/ř/\\\[rcaron]/g' |sed 's/ž/\\\[zcaron]/g' |sed 's/ý/\\\[yacute]/g' |sed 's/á/\\\[aacute]/g' |sed 's/í/\\\[iacute]/g' |sed 's/é/\\\[eacute]/g' |sed 's/ú/\\\[uacute]/g' |sed 's/ů/\\\[uring]/g' |sed 's/ó/\\\[oacute]/g' |sed 's/ď/\\\[dcaron]/g' |sed 's/ň/\\\[ncaron]/g' |sed 's/ť/\\\[tcaron]/g' |sed ':a;N;$!ba;s/\n/\\n/g'`
xvkbd -text "$xxx" 2>/dev/null
aa=1
break
else
aa=0
fi
done
if [[ $aa -eq 0 ]]
then
xsel | xvkbd -file - 2>/dev/null
fi
I use -text
with xvkbd when the text is in Czech (my language) because xvkbd does not understand diacritics like ě but only in form like \[ecaron]
. Now, with this option, if there is a newline int xsel, it does not get printed with xvkbd. However, when I do
xx="---8<-----\nToday date is: $(date +%Y%m%d)\n---8<-----"
xvkbd -text "$xx" 2>/dev/null
Newlines do get printed.
I suspect the trouble is in the last sed expression sed ':a;N;$!ba;s/\n/\\n/g'
, but I do not know how to make it better. I think that I need to take care for \n
s somehow?
On the side note, it has been pointed to me before I could use
sed -e 'bla/bla' -e 'bleble'
but the thing about having each expression on a new line is very neat and readable, thanks. Thanks for the answer (even the long one) anyway.One thing I am still unclear on is why I need so many
\
? I mean, withs/\n/\\n/g
, I would expect it to catch any newline (\n
) and replace it with one backslash (\\
would do that) and then printn
. Does sed for some reason think I am trying to escape a newline? – sup – 2012-04-18T18:42:32.020I've just found the real(?) problem this time (I hope)... It is to do with backticks vs
$( )
... your\\n
works fine with$( )
but loses the newlines otherwise... (but I'm starting to phase out on this one...need sleep occasionally :) ... It works when I use$( )
... which, by the way, is a much preferred and encouraged (newer) style.. – Peter.O – 2012-04-18T18:50:16.553Using
\\n
(not\\\\n
),$( )
works for me, but backticks lose every intermediate newline... There is one more issue though, and that is of having to determine before you process yourxsel
data whether or not is had a trailing newline, and then selectively add (or not add) that final trailing newline via a sed expression... – Peter.O – 2012-04-18T19:04:42.680I will remember
$()
, thanks for that, it works with just two backslashes now. As for the trailing\n
, it seems to be keeping it now, so I will let it be. In case I need to test for it, I just doxsel | grep -qE "/n$
and then act accordingly on$?
. – sup – 2012-04-18T23:54:36.173