zsh-users
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <okiddle@yahoo.co.uk>
To: Zsh-Users List <zsh-users@zsh.org>
Subject: Re: Function or Alias - Does it matter?
Date: Wed, 19 Nov 2014 09:59:23 +0100	[thread overview]
Message-ID: <7708.1416387563@thecus.kiddle.eu> (raw)
In-Reply-To: <CADjGqHuZpMyRCvEMivTZhW63Dk0wgSR8K+q0Whd_MhW57qB0Ag@mail.gmail.com>

TJ Luoma wrote:
> function if they do the same thing?
> 
> For example, these are (as far as I know) functionally identical:
> 
>          function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p"
> "$EPOCHSECONDS" }
> 
>          alias timestamp_r='strftime "%Y/%m/%d at %-I:%M:%S %p" "$EPOCHSECONDS"'
> 
> Is there any reason to prefer one or the other?

If you find your function finishes by passing "$@" as the last argument
to whatever command it is, then there's a good chance that an alias will
suffice. Note that (unless you unsetopt completealiases), completion
after an alias will look at the alias definition so with, e.g:
  alias ll='ls -lFb'
  ll -<tab>
this will still complete options to ls. With the function equivalent that
won't work without an explicit compdef.

Functions can be more flexible with their arguments. For example, the
following slight modification to your function makes EPOCHSECONDS
merely a default if you don't specify a different time. This is not
possible with an alias:
  function timestamp_r { strftime "%Y/%m/%d at %-I:%M:%S %p" "${1:-$EPOCHSECONDS}" }

Oliver


  parent reply	other threads:[~2014-11-19  9:05 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-19  0:13 TJ Luoma
2014-11-19  0:45 ` James Pearson
2014-11-19  2:51 ` Bart Schaefer
2014-11-19  8:59 ` Oliver Kiddle [this message]
2014-11-23 22:35 ` zzapper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7708.1416387563@thecus.kiddle.eu \
    --to=okiddle@yahoo.co.uk \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).