2

I often

  1. write commands in the terminal that I don't know by heart, and then

  2. paste some path or file name from the clipboard, before pressing Enter.

I thought that it would be fun to do the first part with one shortcut and then just paste, achieving the above with key actions.


Based on this answer under "How to assign a certain keyboard shortcut to paste specific item?", I have added to a shortcut bash -c "xsel -ib <<< 'MY_COMMAND'", which sends a command to the clipboard. But, when pasting that in the terminal, the command is automatically executed, because an "Enter" (newline) is present at the end:

displaying clipboard after executing "xsel -ib <<< 'MY_COMMAND' "


[This is in Kubuntu 20.04.]

cipricus
  • 1,529
  • 1
    The thing you see at the end is definitely not a "space", it's a "Enter" (aka "newline" or \n). That's the symbol I see when I do Shift+Enter with libreoffice. A space at the end of a command will never make a bash command to execute. Also, I'm not sure I really understood your problem : do you only want to prevent bash from executing any text you pasted ? Or do you want something more ? – ewen-goisot Apr 08 '21 at 19:56
  • 1
    @ewen-goisot - answered under you answer. – cipricus Apr 09 '21 at 07:29
  • FYI, bash automatically appends a newline to a "here string".  See bash(1): "The result is supplied as a single string, with a newline appended, to the command on its standard input".  Try printf '%s' 'MY_COMMAND' | xsel -ib instead. – G-Man Says 'Reinstate Monica' Apr 11 '21 at 22:07

2 Answers2

1

Based on answer to "How to avoid xsel sending text to clipboard with space/Enter at the end" I now have a workaround. I don't know how to start the terminal with a text therein waiting for a new input (typed, pasted), with one action (script/command/shortcut) but now I can do it in two steps:

  1. Use a shortcut associated with the bash -c 'printf "%s" "MY_TEXT" | xsel -b' ; konsole to send that text to clipboard and open a terminal like konsole
  2. Ctrl-V to paste text from clipboard to terminal (or Ctrl-Shift-V)

(These two keyboard actions achieve what in the initial question is point no 1. An example of how that can be useful is that of downloading audio from youtube: (#1) bash -c 'printf "%s" "youtube-dl -f bestaudio[ext=m4a]" | xsel -b' && konsole copies to clipboard the necessary command and opens a terminal where (#2) it can be pasted, thus putting forth a window for you to add the youtube link with another (#3)copy+(#4)paste -- or maybe just one drag & drop (that's #3), followed by (#4/5) Enter. A total of 4 or 5 keyboard & mouse actions without the need for actually writing anything.)

I would love to have that with one command but I think this fits here as a complementary answer anyway (rather than within the question body).

cipricus
  • 1,529
  • When you say 2. Ctrl-V to paste text from clipboard to terminal you probably mean Ctrl-Shift-V instead of Ctrl-V if you want to paste with a terminal. (I tried with konsole too and Ctrl-V does nothing like paste). – ewen-goisot Apr 08 '21 at 21:13
  • 1
    @ewen-goisot - True. I will edit. I'm one of those that change Ctrtl-Shift to Ctrl for copy/paste early after installing the OS. Right click on the menu option to edit it in Konsole. – cipricus Apr 09 '21 at 07:18
  • Ok, I understand. I also changed some shortcuts, and my current paste shortcut is Ctrl ' (control + single quote), and also have the Ctrl+Insert shortcut when my keyboard is in "mouse mode". What is written right now is clear enough : you put both, so, other users who didn't configured anything will still understand. Note: I use Ctrl-Alt-l and not Ctrl-l to clear the console (and the other is for lf file manager). I may accidentaly write the wrong one on my answer; (now it's corrected, issue will be only if I edit again). – ewen-goisot Apr 10 '21 at 02:54
1

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)