views:

46

answers:

3

I'd like to set up my Bash in such a way that I could yank text from the previous command's stdout. The example use case I'll use is resolving conflicts during a git rebase.

$ git status
# Not currently on any branch.
# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified:      app/views/report/index.html.erb
#
$ vim app/views/report/index.html.erb
# .... edit, resolve conflicts ....
$ git add <Alt+.>

The problem is that the easiest way to grab the filename for the 2nd command (vim ...) is to move my hand over to the mouse. One option is screen, but that has its own set of issues as a day-to-day shell. (Not the least of which is that I use and abuse Ctrl+A as a readline shortcut)

Where could I start at making this work for me? Ideally I'd like to be able to pull the Nth line from the stdout of the previous command somewhere that I can manipulate it as a command.

+2  A: 

Other than using the mouse, the only way I can think of is to use grep, sed and/or awk, perhaps with tee and/or a Bash function and process substitution and/or process and/or command substitution:

vim $(git status | tee /dev/tty | grep ...)

or

var=$(git status | tee /dev/tty | grep ...)
vim "$var"
git add "$var"

The tee allows you to see the full output while capturing the modified output. Creating a function would allow you to easily pass an argument that would select a certain line:

var=$(some_func 14)
etc.

The disadvantage is that you have to do this from the start. I don't know of any way to do this after the fact without using screen or some other output logging and scripting a rummage through the log.

Dennis Williamson
Yeah, it's becoming obvious that I'll need to modify my environment to capture stdout/stderr and then write something to pull those lines in. Your suggestions look good, but I'm not sure how I'd get this from a variable onto the bash command line (i.e. expand the variable inline, or even better, do all this from a shortcut key)
Shaun
@Shaun: Do you really need to expand it? Can't you just reference the variable as in my example? You can press Ctrl-Alt-e to expand the variable on the command line if you need to (it will do other expansions at the same time). Search for "shell-expand-line" in `man bash` for more information. Unfortunately, readline functions can't be called programmatically.
Dennis Williamson
That's unfortunate - it's more effort than just using the mouse to do it. I was hoping I'd be able to code my way around it, but it looks like that's not really an option without modifying Bash. Thanks for the answer.
Shaun
A: 

pipe the output through sed:

git status | sed -n '5p'

to get the 5th line

ennuikiller
+1  A: 

I don't know of a good, clean solution, but as a hack you could try the script command, which logs all input and output to a file. For GNU script:

$ script -f
Script started, file is typescript
$ ls -1
bar
baz
foo
typescript
$ echo $(tail -3 typescript | head -1)
foo
Jander