1

My end-game here is to have the following:

$ python3 -c "print(\"Hello, world!\")"
Hello, world!
$ >>> print("Hello, world!")
Hello, world!

Basically, every time my command line starts with >>>, I want the rest of the command line to be fed to python3 with the -c switch.

If it was possible to declare (and use) a >>> function, it would be implemented as follow:

>>>() {
    python3 -c "$@"
}

For the record, I'm using zsh.

My first try was to create an alias named >>>, but it's not valid on any of the two shells. Next I tried to create a >>> function, and even though it's valid on ZSH, there is seemingly no way to call it (be it with >>> or \>\>\>).

How can I achieve this?

1

Since that needs to happen before zsh parses the line (as you need it to be python syntax, not zsh syntax), you'd need to do it in the accept-line widget:

accept-line() {
  emulate -L zsh
  set -o extendedglob
  case $BUFFER in
    ('>>>'*)
      zle -I
      fc -R =(print -r -- $BUFFER)
      python -c "${BUFFER##>>> #}"
      BUFFER=
      ;;
    (*) zle .$WIDGET
  esac
}
zle -N accept-line
1
  • Wow, that's great. I didn't know about accept-line. This works like a charm. Thanks! – T. Barusseau May 29 '20 at 7:25
1

This works for zsh, remember to escape special characters/quote when defining the function:

'>>>'() {
    python3 -c "$@"
}
'bad|idea<for>a command name'() { :; }
1
  • The issue is that now I need to call it with $ \>\>\> which is not desirable – T. Barusseau May 28 '20 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.