Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: cd $(locate zoo.txt|head -1)(:h)

On Feb 12,  6:32pm, zzapper wrote:
} Subject: Re: cd $(locate zoo.txt|head -1)(:h)
} zzapper <david@xxxxxxxxxxxxxx> wrote in
} news:XnsA5AC8D35FCD60davidrayninfocouk@ 
} > cd $(locate zoo.txt|head -1)(:h)
} > 
} > I'd like to make this a bit more versatile, i.e detect more than one
} > result and may be offer a list?
}  cd $(locate -r -l1 "/zoo.txt$")(:h)

Are you answering yourself or changing the question?

(By the way, that doesn't work for me; on my system the -r option has
to be immediately followed by the regular expression, so this searches
for files whose names contain "-l1".)

Obviously if you just execute that command as typed and the locate
finds more than one file, "cd" is going to choke on it.  So are you
asking for a "cd" function that does something clever when handed a
bunch of contradictory arguments?

Conversely if you press TAB the default thing is going to expand all
the names on the command line, which probably also isn't what you
are looking for.  So maybe you're looking for a completer that runs
"locate" to get the list of matches?

_locate () {
  local -a expl files
  [[ -o magicequalsubst ]] && compset -P '*='
  files=($(_call_program locate locate -l1 -r "/$words[CURRENT]\$")) 2>/dev/null
  if (( $#files ))
    local -au dirs=(${(D)${(b)files:h}}) 
    _wanted directories expl "directories containing $words[CURRENT]" \
	compadd -QU -f -a dirs
    return 1

That could obviously use some work, e.g., you might want more regex
decoration around $words[CURRENT], which ought to be controlled by a
zstyle, etc.  However adding this completer should make

 % cd zoo.txt<TAB>

display a list of the directories that contain a zoo.txt file.

Messages sorted by: Reverse Date, Date, Thread, Author