How to start a terminal with text typed inside ?
Run a command in a terminal :
Typing a text or execute a command in a terminal are quite similar. First, let us see how we can open a terminal executing a command.
To execute a command, you can do konsole -e MY_COMMAND
.
Of course, this is not what you want, but it will help.
I first need to say that there is no official option for that in konsole
or any terminal I have seen, but we can make one… based on konsole -e
feature.
Currently, I know only how to do it with gnome-terminal
and with konsole
, and there is probably a similar way to do it with other terminal emulators.
By making several tests, I noticed a big issue : konsole -e
stops after the execution of the command : konsole -e sleep 10
will display a konsole
terminal during 10 seconds, with no ability to run a shell command, and then it closes itself.
But I need the konsole
terminal to stay open if I want to perform some actions inside it.
Why is it like that? I think it can make sense in some contexts, like konsole -e top
(task manager) or konsole -e vim
(text editor), and quit konsole
when you quit vim
(then, this vim
behaves like a GUI text editor that you can close, like gedit
).
I got the same issue with st -e MY_COMMAND
or gnome-terminal -- MY_COMMAND
.
So I saw this post and it exactly solves the issue (except it's made only for gnome-terminal
and xterm
, so there may or may not be a similar solution for konsole
).
First, let's create a function to run gnome-terminal
or konsole
that does not quit after executing something:
bash_in_gnome_terminal(){
local IFS
printf -v cmd %q ". ~/.bashrc; set -m; $*"
gnome-terminal -- bash -c "bash --rcfile <(echo $cmd)"
}
bash_in_konsole(){
local IFS
konsole -e bash --rcfile <(printf '. ~/.bashrc; set -m; %s\n' "$*")
}
You can test it with bash_in_konsole 'echo foo'
and you are supposed to see something like:
foo
username@computer:~$
Type text with tmux
:
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 :
tmux
Enter (launched the session).
Ctrl-b: (enter in command mode).
send-keys "echo foo"
Enter (type it as if you typed it), you are supposed to see something like:
username@computer:~$ echo foo
Now, you can merge the two parts :
bash_in_konsole 'exec tmux new-session -A -t "temporary" \; run-shell "sleep .1" \; send-keys "C-u" "echo foo" "C-l"'
.
I will explain :
exec
leaves the terminal when you leave tmux
, you quit konsole
.
new-session -A -t "temporary"
launches the same session if you used the command before, but then, the sessions will be numbered with huge numbers.
send-keys "echo foo"
will type echo foo
in the terminal.
Unfortunatly, it will be written twice (once before the shell prompt and once inside it).
"C-u"
will clear the line (necessary if you use the -A -t "temporary"
arguments).
"C-l"
is sent it as keys too, Ctrl-l is the official bash
shortcut to clear the screen
(avoids to see twice the echo foo
).
Note : Ctrl-Alt-l also works on some shells. Maybe not on bash
.
- It will not echo
foo
alone, until you press Enter key. You can also modify the command echo foo
to anything you want.
Clipboard without the "Enter" (newline) at the end:
If you copied 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 :
- 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.
warning: : in your case, you are using youtube-dl
, and YouTube's URL may include timestamp like http://youtube.com/foobar&t=21m17s
meaning the video 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 difficult to calcel it with Ctrl-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 ?
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) :
Step 0 : I assume the reader wants to do this with bash
, konsole
and a youtube-dl
command, and the typed command should include clipboard content. I also assume this is for a shortcut made in konsole
(for other readers who will simply run it in terminal, make sure you reload your ~/.bashrc
after editing it).
Step 1 : Install tmux
if you don't have it.
Step 2 : Copy the 7 following lines and paste it to your ~/.bashrc
file (and save the 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"'
}
Step 3 : Now, you can test with tmux_in_konsole "youtube-dl -f bestaudio[ext=m4a] $(xsel -ob | tr "\n" " " | sed "s/&/\\\\&/g")"
. This is the command you should put for your konsole
shortcut. 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
Step 3 bis : (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 expected result is:
username@computer:~$ youtube-dl -f bestaudio[ext=m4a]
Step ω : (when you quit konsole
) Once you have typed the URL of the video you want to download with youtube-dl
, and you typed Enter, and you finished to download the video, you can quit this terminal.
To quit tmux
, simply press Ctrl-bShift-d (you can also quit it the way you quit a shell, with Ctrl-d, but it means this tmux
session will then be closed, and you will get huge numbers again).
If you accidentally closed konsole
before the end of the download, tmux
will allow it to run in the background. But don't do this purposely, because it may cause some problems (at least display-related) if you run a second instance of tmux_in_konsole
while the first download didn't ended.
Note: I removed 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)
\n
). That's the symbol I see when I do Shift+Enter withlibreoffice
. A space at the end of a command will never make abash
command to execute. Also, I'm not sure I really understood your problem : do you only want to preventbash
from executing any text you pasted ? Or do you want something more ? – ewen-goisot Apr 08 '21 at 19:56printf '%s' 'MY_COMMAND' | xsel -ib
instead. – G-Man Says 'Reinstate Monica' Apr 11 '21 at 22:07