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
2
  • @vembutech i'm not sure i understand.. can you repeat the above point using my example?
    – abbood
    Dec 26, 2014 at 8:10
  • I use M-x grep-find in emacs. I don't know how to do that in vi.
    – umeboshi
    Dec 26, 2014 at 9:58

3 Answers 3

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 !!
5
  • 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
    Dec 26, 2014 at 8:32
  • 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
    Dec 26, 2014 at 8:34
  • $ is the last line in sed. You can use a line number instead, for example 4p. Is that close enough?
    – janos
    Dec 26, 2014 at 8:43
  • 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
    Dec 26, 2014 at 8:57
  • 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
    Dec 26, 2014 at 9:41
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
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .