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

Re: bck-i-search for single parameters



On 02/04/2008, Dan Vanderkam <danvdk@xxxxxxxxx> wrote:
> I love being able to hit Ctrl-R to do a search backwards through my
>  command line history. I don't know how I lived without it. My only
>  complaint is that, more often than not, I find myself writing command
>  lines by combining flags from several other CLs. For example, I might
>  want to run a program (A) over the output of a previous program (B).
>  In my perfect zsh world, I'd ctrl-R back to the CL I previously typed
>  in to run program A. Then I'd do a history search for that single
>  parameter to program B to put them together.
>
>  Is there any way to do this? It would be like alt-period, only it
>  would let you search, rather than just pulling up the last parameter
>  to previous commands. Any ideas would be greatly appreciated.

I like to use narrow-to-region for this. It was a bit hard to use so
i made a wrapper function for it.

zle -N _narrow_to_region_marked
bindkey "^X^I"    _narrow_to_region_marked
autoload -U narrow-to-region
function _narrow_to_region_marked()
{
  local right
  local left
  if ((MARK == 0)); then
    zle set-mark-command
  fi
  if ((MARK < CURSOR)); then
    left="$LBUFFER[0,$((MARK-CURSOR-1))]"
    right="$RBUFFER"
  else
    left="$LBUFFER"
    right="$BUFFER[$((MARK+1)),-1]"
  fi
  narrow-to-region -p "$left>>|" -P "|<<$right"
}

So you would ctrl-R search for command A, then type a | and press ^X^I
and then ctrl-R for command B and press enter, that will exit the
narrow-to-region mode and combine the two commandlines.

-- 
Mikael Magnusson



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