0

This question already has an answer here:

I find myself typing these commands a lot (on Mac):

$ touch example.txt
$ open example.txt

I was wondering if there was an easier way to do this. I tried this:

$ alias t='touch $0 && open $0’

But I got this:

$ t awef.txt
touch: illegal option -- b

Note: I do not want to empty the file. If the file already has content, I just want to open it.

marked as duplicate by Anthony Geoghegan, Community Oct 26 '18 at 21:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Don't use an alias. They are extremely limited and, for example, won't let you interpolate an argument into the definition. Instead, use a function:

t() { [[ ! -f "$1" ]] && touch "$1"; open "$1"; }

This creates (touches) a file only if it doesn't already exist.

  • Thanks! I was looking for something like this but couldn't find it. – rassar Oct 26 '18 at 21:53

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