zsh-users
 help / color / mirror / code / Atom feed
* Any way to have ".sh" be optional?
@ 2013-09-26 20:12 TJ Luoma
  2013-09-26 20:39 ` Jérémie Roquet
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: TJ Luoma @ 2013-09-26 20:12 UTC (permalink / raw)
  To: Zsh-Users List

The Subject line may not be the best description of what I want, but
it was the best I could come up with.

I tend to name all of my Zsh scripts to end with '.sh' so I can easily
`fgrep -i Whatever *.sh` when I'm looking for something.

However, I would rather not have to type the ".sh" if not necessary.
(Yes, I am that lazy.)

So assume I have a script "mkseries.sh" which I do not want to rename
but which I want to use in zsh just by typing "mkseries" -- is there a
way to tell zsh "If I use the command 'foo' and there is no 'foo' but
there is 'foo.sh' then I want to use 'foo.sh'?

As always, thanks for your time,

Tj


ps - I am only asking about this when using Zsh in Terminal as my
login shell; in shell scripts I don't mind adding the ".sh"


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Any way to have ".sh" be optional?
  2013-09-26 20:12 Any way to have ".sh" be optional? TJ Luoma
@ 2013-09-26 20:39 ` Jérémie Roquet
  2013-09-26 21:27 ` Micah Elliott
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Jérémie Roquet @ 2013-09-26 20:39 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh-Users List

Hi,

2013/9/26 TJ Luoma <luomat@gmail.com>:
> The Subject line may not be the best description of what I want, but
> it was the best I could come up with.
>
> I tend to name all of my Zsh scripts to end with '.sh' so I can easily
> `fgrep -i Whatever *.sh` when I'm looking for something.
>
> However, I would rather not have to type the ".sh" if not necessary.
> (Yes, I am that lazy.)
>
> So assume I have a script "mkseries.sh" which I do not want to rename
> but which I want to use in zsh just by typing "mkseries" -- is there a
> way to tell zsh "If I use the command 'foo' and there is no 'foo' but
> there is 'foo.sh' then I want to use 'foo.sh'?

Something like

command_not_found_handler() { test -x $1.sh && $1.sh $@[2,-1] }

should do the trick.

Best regards,

-- 
Jérémie


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Any way to have ".sh" be optional?
  2013-09-26 20:12 Any way to have ".sh" be optional? TJ Luoma
  2013-09-26 20:39 ` Jérémie Roquet
@ 2013-09-26 21:27 ` Micah Elliott
  2013-09-26 23:35   ` TJ Luoma
  2013-09-26 21:40 ` Bart Schaefer
  2013-09-26 23:18 ` TJ Luoma
  3 siblings, 1 reply; 6+ messages in thread
From: Micah Elliott @ 2013-09-26 21:27 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh-Users List

On Thu, Sep 26, 2013 at 1:12 PM, TJ Luoma <luomat@gmail.com> wrote:
>
> I tend to name all of my Zsh scripts to end with '.sh' so I can easily
> `fgrep -i Whatever *.sh` when I'm looking for something.

Cool, I tend to use .zsh, but mostly so I know they're not system scripts.

> However, I would rather not have to type the ".sh" if not necessary. (Yes, I
> am that lazy.)

Leaving out the .«tab» saves a little bit of typing. But you might have
already fully tab-completed a couple chars in.

> is there a way to tell zsh "If I use the command 'foo' and there is no 'foo'
> but there is 'foo.sh' then I want to use 'foo.sh'?

You could tie into the command_not_found_hook (briefly mentioned in
zshmisc(1)). Something like this:

  command_not_found_handler() {
    actual=$@[1].sh
    print "Proxying for actual: $actual"
    for p in $path; do
      if [[ -x $p/$actual ]]; then
        $actual $@[2,-1]
        break
      fi
    done
  }

Then use:

  % foo aa bb
  Proxying for actual: foo.sh
  <regular output>...

A couple caveats:

  * I don't actually do this, but maybe I'll try; the func is not really
    tested

  * zsh syntax highlighting
    (https://github.com/zsh-users/zsh-syntax-highlighting) won't match foo

  * running in cwd as ./foo isn't handled

  * not sure if looping over path is best approach; would something with
    hash work?

--
twitter:@MicahElliott  |  email:mde@MicahElliott.com  |  http://membean.com
Remember your words with Membean!


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Any way to have ".sh" be optional?
  2013-09-26 20:12 Any way to have ".sh" be optional? TJ Luoma
  2013-09-26 20:39 ` Jérémie Roquet
  2013-09-26 21:27 ` Micah Elliott
@ 2013-09-26 21:40 ` Bart Schaefer
  2013-09-26 23:18 ` TJ Luoma
  3 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2013-09-26 21:40 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 518 bytes --]

On Thu, Sep 26, 2013 at 1:12 PM, TJ Luoma <luomat@gmail.com> wrote:

> So assume I have a script "mkseries.sh" which I do not want to rename
> but which I want to use in zsh just by typing "mkseries" -- is there a
> way to tell zsh "If I use the command 'foo' and there is no 'foo' but
> there is 'foo.sh' then I want to use 'foo.sh'?
>

command_not_found_handler() {
  local cmd=$1
  shift
  # apply whatever tests you want to figure out if $cmd.sh exists, return 1
if not
  # if it does exist, then
  $cmd.sh "$@"
}

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Any way to have ".sh" be optional?
  2013-09-26 20:12 Any way to have ".sh" be optional? TJ Luoma
                   ` (2 preceding siblings ...)
  2013-09-26 21:40 ` Bart Schaefer
@ 2013-09-26 23:18 ` TJ Luoma
  3 siblings, 0 replies; 6+ messages in thread
From: TJ Luoma @ 2013-09-26 23:18 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 1054 bytes --]

command_not_found_handler


Yet another reason Zsh is amazing.


Thanks to everyone who took the time to reply. "command_not_found_handler"
is pretty much exactly what I was hoping for.


Tj



On Thu, Sep 26, 2013 at 4:12 PM, TJ Luoma <luomat@gmail.com> wrote:

> The Subject line may not be the best description of what I want, but
> it was the best I could come up with.
>
> I tend to name all of my Zsh scripts to end with '.sh' so I can easily
> `fgrep -i Whatever *.sh` when I'm looking for something.
>
> However, I would rather not have to type the ".sh" if not necessary.
> (Yes, I am that lazy.)
>
> So assume I have a script "mkseries.sh" which I do not want to rename
> but which I want to use in zsh just by typing "mkseries" -- is there a
> way to tell zsh "If I use the command 'foo' and there is no 'foo' but
> there is 'foo.sh' then I want to use 'foo.sh'?
>
> As always, thanks for your time,
>
> Tj
>
>
> ps - I am only asking about this when using Zsh in Terminal as my
> login shell; in shell scripts I don't mind adding the ".sh"
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Any way to have ".sh" be optional?
  2013-09-26 21:27 ` Micah Elliott
@ 2013-09-26 23:35   ` TJ Luoma
  0 siblings, 0 replies; 6+ messages in thread
From: TJ Luoma @ 2013-09-26 23:35 UTC (permalink / raw)
  To: Micah Elliott; +Cc: Zsh-Users List

On Thu, Sep 26, 2013 at 5:27 PM, Micah Elliott <mde@micahelliott.com> wrote:

>

> On Thu, Sep 26, 2013 at 1:12 PM, TJ Luoma <luomat@gmail.com> wrote:

> >

> > I tend to name all of my Zsh scripts to end with '.sh' so I can easily

> > `fgrep -i Whatever *.sh` when I'm looking for something.

>

> Cool, I tend to use .zsh, but mostly so I know they're not system scripts.


Mine are all in ~/Dropbox/bin/ (which I've added to $PATH) so I know
they aren't system scripts, and since my Dropbox only syncs between
Macs I don't have to worry about cross platform script compatibility
but I always have all of my scripts on all of my Macs :-)



> > However, I would rather not have to type the ".sh" if not necessary. (Yes, I

> > am that lazy.)

>

> Leaving out the .«tab» saves a little bit of typing. But you might have

> already fully tab-completed a couple chars in.


Yup, I use 'tab' whenever possible to autocomplete. The problem is
that I have recently converted some functions (which did not have
".sh" suffix) to scripts, mostly so I can call them from other scripts
without putting them into .zsh(rc|env) but 'muscle memory' keeps me
typing them the old way.



> > is there a way to tell zsh "If I use the command 'foo' and there is no 'foo'

> > but there is 'foo.sh' then I want to use 'foo.sh'?

>

> You could tie into the command_not_found_hook (briefly mentioned in

> zshmisc(1)). Something like this:

>

>   command_not_found_handler() {

>     actual=$@[1].sh

>     print "Proxying for actual: $actual"

>     for p in $path; do

>       if [[ -x $p/$actual ]]; then

>         $actual $@[2,-1]

>         break

>       fi

>     done

>   }


FYI this worked perfectly for me, and I love the touch of having it
remind me that this is catching me when I type something incorrectly,
which is more likely to help me remember to do it correctly in the
future. I think. Theoretically. I definitely like what it does,
regardless of its actual effect on my habits.



>   * I don't actually do this, but maybe I'll try; the func is not really

>     tested


Worked as expected for me.



>   * zsh syntax highlighting

>     (https://github.com/zsh-users/zsh-syntax-highlighting) won't match foo


I don't use that but now I'm going to head over and see what else is there :-)


>   * running in cwd as ./foo isn't handled


It is extremely unlikely that I would ever do `./foo` anyway.


>   * not sure if looping over path is best approach; would something with

>     hash work?


It was certainly fast enough for me that I wouldn't spend time trying
to optimize it. Even if it was slow it would be another way that maybe
it would push me towards correcting my habit of mistyping.


Thanks again.


Tj


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-09-26 23:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-26 20:12 Any way to have ".sh" be optional? TJ Luoma
2013-09-26 20:39 ` Jérémie Roquet
2013-09-26 21:27 ` Micah Elliott
2013-09-26 23:35   ` TJ Luoma
2013-09-26 21:40 ` Bart Schaefer
2013-09-26 23:18 ` TJ Luoma

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).