Thursday, November 13, 2008

bash chooser

Note: I've found a better way here:

My friend hacked up a ruby script to enable some better use of the bash command line. The problem I was having was that sometimes I wanted the bash command line to be interactive so that could pick one of several outputs. For instance let's say I am hunting for a file to edit from bash

so I run:

jalvarado:~/bobo jalvarado$ find . -name "Have*"

Well, the second file is the one I want to edit so to get to in the normal bash may I may have to write.

vi `find . -name "Have*" | head -n 2 | tail -n 1`

and that would open the second file, but that is alot of typing and if I then wanted to open a different file of that set I would have to modify the command line in two places. So he wrote a script that let's you use this type of syntax

jalvarado:~/bobo jalvarado$ choose vi `find . -name "Have*"`
[1] ./bobo-browse/src/com/browseengine/local/glue/
[2] ./bobo-browse/src/com/browseengine/local/service/geosearch/
[3] ./bobo-browse/src/com/browseengine/local/service/geosearch/

Then you type "2" and up comes your file. Even better would be syntax like

find . -name "Have*" | choose vi

but haven't figured that one out yet.

Here is the Ruby script.


require 'Open3'

if ((ARGV.nil?) || (ARGV.size <> filename1 filename2 ...';
puts 'example: choose "cvs log" `find . -iname "**"`';

command = ARGV[0];
filenames = ARGV[1,ARGV.size];

filenames.each_with_index do | line, index |
puts '[' + (index+1).to_s + '] ' + line

selection = STDIN.readline

filename = filenames[selection.to_i-1]

Open3.popen3(command + " " + filename) do |read_stdin, read_stdout, read_stderr|
puts read_stdout.readlines;

No comments: