Domanda

I have been looking around, but I haven't found any answer so far. I Bash/Zsh, I know of a variation of

echo !! 
echo !$

and so on. But is there a way to recall the last output line? I know I could simply edit the line and add some piping information but I was wondering whether there were an alternative. To illustrate, I might want to do the following

ls -tr
vim {whatever key to recall the last line}

which would allow me to edit the most recent file.

If a similar question was already asked and I was unable to find it, could you please redirect me to it?

È stato utile?

Soluzione

I think you're out of luck. Potentially the shell would have to store a huge amount of info (imagine an long running process and its output).

For your example:

ls -tr
vim {whatever key to recall the last line}

perhaps an acceptable alternative is to use a subshell thus:

ls -tr
vim $(!!)

which re-executes your last command in a subshell and the output is used as the argument to vim.

Altri suggerimenti

Ok, there is a way to accomplish your goal. This is just a proof-of-concept, I wouldn't really recommend to do so:

> script --flush /tmp/myscript
Script started, file is /tmp/myscript
> LASTLINE() {
     tail -2 /tmp/myscript | head -1 | tr -d '\r'
  }
> bind '"^[[15~":"$(LASTLINE)\n"'
> ls -tr1
file1
file2
fileN
> vi [press F5]

The idea: use script as the tool which remembers in- and output. In the example, it stores all data in file "/tmp/myscript" which in turn can be read to get the last result line. Since the actual command (vi ...) is the last line in "myscript", we read the last two lines of that file and use the first one of them. (Additionally, I had to filter the return character via tr.) Now we bind this command LASTLINE to F5 (look at In bash, how do I bind a function key to a command? to see in detail how to bind a command to a key). And from now on, you can press F5 to get the contents of the last output line.

If you do so, remember that you are within a sub-shell (because of script) which can be left via exit to get back to your original shell.

Careful: this is more or less playing-with-bash. There are race-conditions and other draw-backs, so I'd recommend not to use this solution in sensitive environment.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top