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

Re: implicit previous command, only state what should change



To add to what's already been said ...

On Jul 14, 12:06am, Emanuel Berg wrote:
}
} Is there a way to tell zsh to "execute the previous
} command again, only substitute the first argument for
} user-emacs-directory"? Like
} 
} $ !!:1->user-emacs-directory

There's no shorthand quite like for arbitrary word positions.  To drop
just the *last* word, you can use !!:-

However, you can always do the obvious:

zsh% !:0 user-emacs-directory !:2*

Or for e.g. the third word:

zsh% !:0-2 new-third-word !:4*

The default event is the previous command so you don't need to double
the "!" -- but note that subsequent "!" refer to the same command as
the preceding reference. So if you want to swap the second word of the
fifth preceding command:

zsh% !-5:0-1 new-second-word !:3* 

There, because !-5 has already selected the fifth previous command, !:3*
selects the third and remaining words of that same command.

You can automate this for some simple cases (the following assumes you
are using the default non-vi key bindings):

bindkey -s ^X1 '^U!!:0 \e!^  !!:2*^X^X\e!'
bindkey -s ^X2 '^U!!:0-1 \e!^  !!:3*^X^X\e!'
bindkey -s ^X3 '^U!!:0-2 \e!^  !!:4*^X^X\e!'
bindkey -s ^X4 '^U!!:0-3 \e!^  !!:5*^X^X\e!'
bindkey -s ^X5 '^U!!:0-4 \e!^  !!:6*^X^X\e!'

With this, typing ctrl+x 1 will re-insert the previous line with the first
argument omitted, leaving the cursor where the new first argument should
be typed.  Similarly for ctrl+x 2 through 5, though they don't work quite
ideally if there are no arguments following the one you're replacing (try
it to see what I mean).



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