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

Re: Is there any possible way to automatically escape characters when executing an alias in zsh?



On Sat, 2021-09-11 at 14:41 -0400, Steve Dondley wrote:
> I have many aliases set up for taskwarrior in zsh:
> 
> alias tasn="task +sn add"`
> 
> On the command line, I'll type in:
> 
> tasn some task description here
> 
> Which works unless I put in single quote or some other character:
> 
> tasn call Tom's friend
> 
> This will thrown an error unless I escape the apostrophe.
> 
> I don't want to turn these into functions because then I lose the 
> autocomplete capabilities. I'm wondering if there might be some neat 
> trick that will allow me to automatically escape apostrophes (and other 
> problematic characters) in the task description.

Like this.


accept-with-quote() {
  if [[ $BUFFER = 'tasn '* && $BUFFER != *\\* ]]; then
      BUFFER=${BUFFER//\'/\\\'}
  fi
}
zle -N zle-line-finish accept-quote


Note that the line as updated is what goes into the history --- that's
why I've looked for the backslash and not update the line further if
there's one there.

pws





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