Shell – How to use the output of a command without using the mouse

gnu-screenshellterminalzsh

Imagine you issued a find command and now you want to use one line of the resulting output as an argument.

$ find ..
fileA
fileB
$ vi <CURSOR>   # now do what to achive:
$ vi fileB      

Current workaround:
Double click on fileB (selection is extended automatically) and press middle mouse button (paste on X). Works but uses the mouse.

Possible solutions:

  1. Tab completion — Works in most shells but only for files.
  2. Hook the shell — Like scm_breeze; This is pretty much what I seek, but it's limited to git.
  3. Use screen — Pretty close, but still many key strokes. And it needs screen….
  4. store output in bash variable (GMaster) — That would only work if the find command outputs one line. How do I access the second line like in the example?
  5. pipe command to editor (jasonwryan) — Works but executes the command twice. That may be time consuming or not even possible.
  6. use emacs Dynamic Abbrev Expansion (Arkadiusz Drabczyk, Michael Vehrs) — Thats what I am looking for but I would not like to start with emacs.

Solution for me:
"Dynamic Abbrev Expansion". Now I have the name of this feature I was looking for. I will start a new question about how to get his "dabbrev-expand" in other software's:

https://unix.stackexchange.com/questions/281298/list-of-sofwares-inspired-by-emacs-dabbrev-expand.

Vision of the best solution for me:
Something like the hint mode from vimperator or easymotion in Vim, to yank text from the output of the current terminal.

Best Answer

Use emacs, start an inferior shell and issue your command. The output will be available in the shell buffer and can be selected using the usual commands. Alternatively,

select file in $(find <whatever>); do vi $file; break; done

The emacs approach is more practical if you already know the editor. Emacs can run arbitrary "inferior processes", ie. interactive commands with their input and output connected to an emacs buffer. So, M-x shell-mode starts an inferior shell, you type your find command and the output of that command is available in the buffer

$ find .. -name "*.sh"
../self.sh
../scheme/random.sh
../scheme/sample.sh

Now, you move you cursor to the file you are interested in and type M-x find-file-at-point to edit that file in a different buffer.