6

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.

  • Maybe this one stackoverflow.com/questions/5955577/… – GMaster May 3 '16 at 0:58
  • 2
    Arrow up to retrieve the command from history (or Ctrl-r if it is not the last), v to edit the command and pipe the (grepped, if necessary) output to $EDITOR... – jasonwryan May 3 '16 at 1:01
  • I'd have to agree with @jasonwryan on this one. The major advantage to building up a long pipeline that does exactly what you want, instead of doing the bits and pieces of what you want interactively, is that it's reusable. The whole long command line can be dumped into a script as-is and will do exactly what you want it to do, as many times as you want to use it. – Wildcard May 3 '16 at 1:29
  • 1
    A rule you will learn if you work with computers professionally is: There's no task you will perform only once, ever. – Wildcard May 3 '16 at 1:29
  • 1
    @Tristan, no, it's a perfectly good question. I upvoted it and I'd be interested in the answer. But most of the time, the best action to take is to simply modify the previous command by appending a pipe | and another command to parse the output. You could do that with e.g. !! | someothercommand – Wildcard May 3 '16 at 5:48
2

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.

  • The emacs sounds promising. Would you mind to eloberate? – Tristan May 3 '16 at 5:53
0

There is a Zsh plugin created to do specifically what you request: https://github.com/psprint/ztrace

  • Ztrace seems to be designed to be explicitly invoked before running a few commands, not to be always running in your shell. – sondra.kinsey Nov 25 '18 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.