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

Re: Disable most shell expansion to function or script



On Thu, Jan 4, 2024 at 10:10 AM Sam B. <me@xxxxxx> wrote:
>
> I'm looking for a way to disable most shell expansion for the arguments
> to a script or function

>      todo Text inc. $var, *glob?, events!, <redirs, >(sub) & more

accept-and-do-nothing() {
  BUFFER=': !#:0:s/\://:p !"'"$BUFFER"
  zle accept-line
}
zle -N accept-and-do-nothing

It's really difficult to avoid having the history references get
backslash-quoted ( in this example !, becomes \!, ), but otherwise
this skips everything.

What this does is:
- insert a : command as the word in command position.
- insert a history substitution that replaces that word with nothing
and then reprints the whole command without executing it
  (substitution is to avoid doubling the first word when !#:0 is reprinted)
- insert the !" reference that disables history for the remainder of
the command line
- accept the result to add the whole thing to the history list

If "todo" is the only command for which you want this effect, you
could replace the ":" with "todo".

> Mostly I'm interested joting down some plain text which could include
> '?!&' and have "todo" receive it all without the shell having done some
> magic beforehand.

The foregoing could be augmented with a zshaddhistory hook to
recognize the magic first word and dump the rest of the line into a
file.  Then
  todo $(<thatfile)
would pass the whole thing unmodified into "todo" which would have to
"eval" it or something.

However, have you considered using edit-command-line (possibly
repeatedly) to jot things down, and then quote-region or similar when
happy before executing?




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