2

This is bash 5. I want the output of a command or pipeline to end up on the edit line.

$ perl -E'say "hi"; say "more lines";'
hi
more lines
$ perl -E'say "hi"; say "more lines";' | ???magic-goes-here???
$ hi
> more lines
> █

or perhaps

$ hi more lines█
codeforester
  • 39,467
  • 16
  • 112
  • 140
daxim
  • 39,270
  • 4
  • 65
  • 132
  • Can't you use `xclip` to capture the output, remove the newlines and then press `Shift+Insert` to paste? – choroba Jul 04 '19 at 14:31
  • 1
    `bash` (or more precisely, Readline) doesn't have this feature. `zsh` does; for example, `print -z foo` puts `foo` in the edit buffer. – chepner Jul 04 '19 at 14:43
  • @chepner, the feature must exist, [tag:fzf] uses it. – daxim Jul 04 '19 at 14:51
  • 3
    As far as I can tell, `fzf` is doing its own command-line handling on *top* of the shell. In any case, it's open source: if you believe it does what you want, you can look in the source to see how it does it. – chepner Jul 04 '19 at 15:01
  • Something like using eval or `\`your_command_here\`` (e.g. `\`perl -E'say "hi"; say "more lines";'\``)? What more do you want? – gmargari Jul 05 '19 at 06:24
  • @gmargari, it appears you did not try what you were writing in your suggestion. – daxim Jul 05 '19 at 06:43
  • @daxim I'm trying to understand what you want. What I wrote replaces the command executed with its stdout, in edit line. You want to continue editing the stdout (the output of the command) from then on? – gmargari Jul 05 '19 at 07:59
  • @gmargari, you have the wrong idea. Run `print -z foo` in zsh as chepner says in an earlier comment to see what I mean. – daxim Jul 05 '19 at 08:10
  • I closed the older question [Can a bash script prepopulate the prompt with a command to run when it exits](https://stackoverflow.com/questions/40188292/can-a-bash-script-prepopulate-the-prompt-with-a-command-to-run-when-it-exits) as a duplicate of this one, insofar as this instance is more clearly asked and has a better answer. – Charles Duffy Jul 06 '19 at 16:24

1 Answers1

6

Ctrl+Alt+e expands command substitutions ($() and ``). It also replaces other kinds of expressions in your command prompt, e.g. aliases, see resources below for more details.

In my experience it is a very useful and little known feature of Bash.

You can use `` and Ctrl+Alt+e to replace a command line with its output. It doesn't preserve newlines, though. Also be careful with the side effects of anything you expand on your command line prompt. If you expand an `rm filename`, it will remove filename when you use Ctrl+Alt+e.


Example:

$ `perl -E'say "hi"; say "more lines";'`

Ctrl+Alt+e

$ hi more lines

If the shortcut doesn't work for you, try Esc, then Ctrl+e. It has the same effect. You have to do it this way e.g. in the default Mac terminal.


Resources:

Miłosz Łakomy
  • 996
  • 5
  • 12