how to select nth result on command line without using mouse

3

I got this alias for searching for a file recursively and case insensitive

type fr
fr is aliased to `find . | grep -i'

and so let's say i'm searching for a file

fr nsstring+util
./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.d
./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.dia
./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.o
./SmartTaxi/Classes/Categories/NSString+Util.h
./SmartTaxi/Classes/Categories/NSString+Util.m

and i want to select the last file.. without having to leave the keybaord and highlighting it etc.. is there a shortcut for that on cli?

To be more specific.. the entire scenario is - search for a file - see retrieved results - basically execute a command on one of them.. in this case I just want to run

vi SmartTaxi/Classes/Categories/NSString+Util.m

without leaving the keyboard

Update:

The idea is that this command must be done in two parts:

  • fr filename and manually see the results returned.. there could be many results and the desired one could be in any one of the lines
  • vi %result of above command% at a specific line

abbood

Posted 2014-12-26T08:00:50.230

Reputation: 702

@vembutech i'm not sure i understand.. can you repeat the above point using my example? – abbood – 2014-12-26T08:10:48.527

I use M-x grep-find in emacs. I don't know how to do that in vi. – umeboshi – 2014-12-26T09:58:36.887

Answers

2

Without leaving the keyboard, you can select specific lines with sed, and wrap the previous command within $(...), like this:

vi "$(fr nsstring+util | sed -ne 3p)"

To select the last line, you can use $ instead of a concrete number, but then you have to quote the sed command:

vi "$(fr nsstring+util | sed -ne '$p')"

For a more general purpose, you can define a function:

nvi() { n=$1; shift; vi "$(eval "$*" | sed -n ${n}p)"; }

Then, to edit the 5th line of the output of the previous command, you could do this:

fr nsstring+util
nvi 5 !!

janos

Posted 2014-12-26T08:00:50.230

Reputation: 2 449

that's close.. but what if i want to open NSString+Util.h instead? the result i want isn't guaranteed to be in the last line – abbood – 2014-12-26T08:32:57.190

i also modified my alias to get rid of some of the bloat.. so your cut -d won't be necessary.. but i'm still wondering about selecting which line – abbood – 2014-12-26T08:34:25.177

$ is the last line in sed. You can use a line number instead, for example 4p. Is that close enough? – janos – 2014-12-26T08:43:42.763

it works.. but i wouldn't say it's usable.. it's def harder to type all that instead of just highlighting the line.. see my answer

– abbood – 2014-12-26T08:57:42.030

See my update, hope this is more usable. In any case, it's more efficient to use sed -n Np construct than head -N | tail -1 – janos – 2014-12-26T09:41:56.580

0

Basically the idea is to create a function that can take arguments.. see here

here are the functions:

function fr { find . | grep -in "$1"; }
function vifr { vi "`find . | head -$1 | tail -1`"; }

a sample usage is:

$ fr nsstring+util
9675:./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.d
9676:./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.dia
9677:./Build/Intermediates/SmartTaxi.build/Debug-iphonesimulator/SmartTaxi.build/Objects-normal/i386/NSString+Util.o
10232:./SmartTaxi/Classes/Categories/NSString+Util.h
10233:./SmartTaxi/Classes/Categories/NSString+Util.m

$ vifr 10233 

the last commands does the same as

vi ./SmartTaxi/Classes/Categories/NSString+Util.m

abbood

Posted 2014-12-26T08:00:50.230

Reputation: 702

0

If you have dialog then you can do this:

find . | grep -i "$1" | sed -e '/$/G' | xargs -d'\n' dialog --menu "text" 0 0 0 3>&1 1>&2 2>&3 | xargs vi

You could do something similar with zenity if you only have that and X output.

Thraidh

Posted 2014-12-26T08:00:50.230

Reputation: 131