2

It's a long shot, but I was wondering if there was a way in zsh, with certain commands, to automatically quote the rest of the line. Eg, for the command he

he (foo*bar + baz)^2

I want it to parse as if I had written

he '(foo*bar + baz)^2'

The reason is that I like to call out to Perl or Haskell for certain tasks and I don't like having to worry about quoting (esp. because sometimes I have reason to use single quote in the expression).

1 Answer 1

6

I think rc_quotes option is good enough.

RC_QUOTES
       Allow the character sequence  `'''  to  signify  a  single  quote
        within singly quoted strings.  Note this does not apply in quoted
        strings using the format $'...', where a backslashed single quote
        can be used.

But if you want, you can write a custom accept-line widget to auto quote for you.

Here is an example, it will quote everything behind python -c:

function quote-accept-line() {
    local -a starts_with=("python -c ")
    for str ($starts_with) {
        if [[ ${(M)BUFFER#$str} ]] {
            BUFFER=$str${(qq)BUFFER#$str}
        }
    }
    zle accept-line
}
zle -N quote-accept-line
# bind it to "Enter"
bindkey "^M" quote-accept-line

Copy these code to your ~/.zshrc.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.