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

Re: s/pattern/pattern/g on the commandline ?



Hi,

2012/2/26  <meino.cramer@xxxxxx>:
> ...an additional question:
>
> Suppose I had entered such an ugly looooong commandline about two days
> ago. I did not remember when it was and what the context was.
> And: I have a very looong history file.
>
> I want to recycle that line and do the s//g-trick on it to get a shiny
> new fresh commmandline for the next action.

If the command in your long line is unusual (let's say “makecoffee”),
you could use:

!makecoffee:gs/<before>/<after>

> When I press ^r<pattern> I easily can find that line, it will
> displayed instantly but without its numbering. In the moment the
> commandline is displayed I loose the ability to do the s//g-trick.
>
> Is there a way to browse the commandlines like with ^r only to get its
> numbering to apply the s//g-trick to the choosen line ?

Hacking history-incremental-pattern-search-backward is an option, but
that wouldn't be easy.

Instead, you could use:

fc -lm <pattern>

…to list previous commands matching the pattern, with their numbers.

Also, what I do in non trivial cases, is either:
 - use edit-command-line as pointed out by Phil: you just have to
ctrl+r the line you want and then edit it using a real text editor, or
 - use a custom widget to run sed (or awk, or perl, or…) on the
current line : you just have to ctrl+r the line you want then to use
whatever key you've bound the widget to to run sed on it.

If you put this in your .zshrc:

function sed-line ()
{
   read-from-minibuffer 'Sed: '
   if [[ -n $REPLY ]]; then
      BUFFER=`echo $BUFFER | sed $REPLY`
   fi
}
zle -N sed-line
bindkey "^[e" sed-line

… you can then use meta-e to run a sed command on the line currently
being edited in your shell.

Best regards,

-- 
Jérémie



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