I was first thinking about xdotool to type text, but it will be some issues if the konsole window isn't focused.
Usually, tmux isn't pre-installed. On a Debian-based distro, the command to install it is sudo apt-get install tmux.
Actually, tmux has a send-keys feature. Of course, if it's with tmux, you should run a tmux session. Maybe you will need to install tmux first.
Try this if you want to understand how this works :
Official solution,Clipboard without the "Enter" (merge of the 2 previous partsnewline) at the end:
WhatIf you couldcopied the URL http://youtube.com/foobar, the output of xsel -ob contains a trailing newline \n (the same char you usually get when you press Enter). Depending on the context, there are several things you can do is this :
bash_in_konsole(){
local IFS
konsole -e bash --rcfile <(printf '. ~/.bashrc; set -m; %s\n' "$*")
}
tmux_in_konsole(){
bash_in_konsole 'exec tmux new-session -A -t "temporary" \; run-shell "sleep .1" \; send-keys "C-u" "' "$*" '" "C-l"'
}
- To get only the first line without a trailing space :
xsel -ob | head -1 | tr -d "\n"
- To put spaces instead of newlines :
xsel -ob | tr "\n" " ".
why a space and not "nothing"? Because you may have copied several URL (several lines) and youtube-dl can download a space-separated set of videos.
(of coursewarning: : in your case, you are using youtube-dl, and YouTube's URL may include timestamp like http://youtube.com/foobar&t=21m17s meaning the same canvideo will automatically start at 21:17 when you launch it in web browser. Also, for some reason, timestamps may appear automatically. And the ampersand & is the bash command to do a fork. It means it will be donedifficult to calcel it with some other terminalsCtrl-c if you accidently pressed Enter. You may also want to escape the characters that are both usually escaped and presents in YouTube's URL, like gnome-terminal? and =, but for zsh (and maybe some other shells) users only : using zsh, the ? causes a no matches found error message.
bash, To escape the ampersand only : xsel -ob | tr "\n" " " | sed "s/&/\\\\&/g".
zsh , To escape the 3 problematic symbols : xsel -ob | tr "\n" " " | sed "s/\(?\|=\|&\)/\\\\\1/g".
note: most of time, I assume the reader is a bash user, but I personally use zsh, so most commands are supposed to work with zsh too. If you face an issue with another shell, please, let me know.
Official solution, (merge of the 3 previous parts) :
I assume the reader wants to do this with bash, konsole and a youtube-dl command, and the typed command should include clipboard content.
Install tmux if you don't have it.
Copy the 7 following lines and paste it to your ~/.bashrc file :
bash_in_konsole(){
local IFS
konsole -e bash --rcfile <(printf '. ~/.bashrc; set -m; %s\n' "$")
}
tmux_in_konsole(){
bash_in_konsole 'exec tmux new-session -A -t "temporary" ; run-shell "sleep .1" ; send-keys "C-u" "' "$" '" "C-l"'
}
(of course, the same can be done with some other terminals, like gnome-terminal).
Now, you can test with tmux_in_konsole "youtube-dl -f bestaudio[ext=m4a] $(xsel -ob | tr "\n" " " | sed "s/&/\\\\&/g")". Let's say you copied the URL http://youtube.com/watch?v=foobarlorem&t=10s. The expected result is:
username@computer:~$ youtube-dl -f bestaudio[ext=m4a] http://youtube.com/watch?v=foobarlorem\&t=10s
Now,3bis. (alternative to 3). If you don't want the clipboard content to be pasted, it's simpler. You can test with tmux_in_konsole "youtube-dl -f bestaudio[ext=m4a] ", the. The expected result is:
Note: I revomedremoved every other part of my comment because it is messy and I first misunderstood a part of your question.
I think there is still a way to see it by seeing the revisions history. I also keep a copy of it on my computer if needed.
Note: personal zsh shortcut is Ctrl-Alt-l instead of Ctrl-l, if you still see the first one, it's a pasting mistake from me. If you see this note, it could also be a pasting mistake from me ! (but this time, it wasn't)
Step by step:
- Install
tmux
- open your bash config file, usually, it's
~/.bashrc
- copy and paste the "functions" (the 7 lines just after "what you could do is this" at the end of the
~/.bashrc file,
- open a new terminal to make sure the
~/.bashrc changes are active
- run
tmux_in_konsole "youtube-dl -f bestaudio[ext=m4a]"
- Expected result : it opens a new terminal with a typed command inside.