[HN Gopher] Start all of your commands with a comma (2009)
       ___________________________________________________________________
        
       Start all of your commands with a comma (2009)
        
       Author : Tomte
       Score  : 219 points
       Date   : 2022-06-23 09:50 UTC (13 hours ago)
        
 (HTM) web link (rhodesmill.org)
 (TXT) w3m dump (rhodesmill.org)
        
       | keybored wrote:
       | Cool.
       | 
       | I'm gonna start them with `a` just to be a little different.
        
       | grn wrote:
       | I do something similar but 1. for aliases and 2. use period. For
       | example, when working on a Rails project I define the following
       | alias:                   function .routes() {           if [[ $#
       | -eq 0 ]]; then             .rails routes           else
       | .rails routes | grep $@           fi         }
       | 
       | where .rails is another alias (for running bundle exec rails
       | ...). I can then run ".routes webhook" and see all routes
       | containing "webhook".
       | 
       | Last but not least, I automated the whole thing with bash-ctx
       | (https://github.com/gregnavis/bash-ctx).
        
       | ghostly_s wrote:
       | I've never once had a problem with one of my commands shadowing a
       | system command so I think I will continue on rather than solving
       | this non-problem, thanks.
        
       | gumby wrote:
       | clever idea, but does it need such a long post to describe?
        
       | avodonosov wrote:
       | Is it secure to have ~/bin/ it the PATH?
        
         | paulv wrote:
         | The issue is that an exploit could write a file in ~/bin/ and
         | trick you in to executing it. But, if something malicious is
         | writing files in your home directory, you've already lost.
        
         | suprjami wrote:
         | It's a default behaviour of at least bash. It's as secure as
         | your home directory is.
        
         | syoc wrote:
         | If someone can write to your home directory they can also
         | update your bashrc file and add whatever they want to PATH
        
       | [deleted]
        
       | aendruk wrote:
       | I tend to give programs longer, more descriptive names and then
       | do the short names as shell aliases. That way there's little risk
       | of name collision for the original program, scripts using the
       | canonical name remain readable, and the shorthand is free to
       | evolve to fit varying work habits.
        
         | quasarj wrote:
         | My keyboard came with a tab key, so I don't worry about my
         | commands having short names.
        
           | keybored wrote:
           | Until you have five commands which all use the prefix `sort-
           | incoming-` and then three commands named `sort-incoming-
           | hourly` and `sort-incoming-monthly` etc.
        
             | layer8 wrote:
             | It would be nice to have camel-case completion in the
             | shell, i.e. have "SIH" be completed to "SortIncomingHourly"
             | and "SIM" to "SortIncomingMonthly", etc. (only for upper-
             | case characters). That way one could get more mileage out
             | of the possible character combinations.
        
               | andra_nl wrote:
               | Well, not exactly tab completion, but I have `fzf`[0]
               | tied to my history search.
               | 
               | That allows me to type `soriloco`, punch ctrl+r and be
               | prompted with `some ridiculously long command` (if, of
               | course, I've run it before).
               | 
               | Combined with infinite history this trick has saved me a
               | bunch of typing and even proper remembering. :P
               | 
               | [0]: https://github.com/junegunn/fzf
        
       | dan353hehe wrote:
       | I do basically the same thing with any aliases or commands that I
       | have just for me. Except I prefix with _ (an underscore.) Having
       | some sort of prefix is extremely nice as it ensures you never
       | override a built-in on accident, AND it makes it easy to tab
       | complete just the ones that you want.
       | 
       | For example I have '_f' aliased to a fuzzy find command that will
       | launch vim on the selected files, and a different one for
       | launching sql clients to various servers etc.
       | 
       | As a prefix, commas work great!
        
         | simias wrote:
         | Unfortunately zsh uses the underscore prefix heavily, _<TAB>
         | results in hundreds of possible completions.
        
       | Taywee wrote:
       | Clever trick. Too bad there's no good way to namespace commands.
       | It would be interesting if you could have, for instance:
       | $ mkdir -p /tmp/bin/my       $ printf '%s\n%s\n' '#!/bin/sh'
       | 'echo hello' > /tmp/bin/my/hello       $ chmod 755
       | /tmp/bin/my/hello       $ PATH="/tmp/bin:$PATH"       $ my/hello
       | hello
        
         | layer8 wrote:
         | You could (ab)use home directories for that. I.e. place your
         | hello into /home/my, and then you can invoke it as ~my/hello.
         | You even get namespace completion for free after the `~`.
        
         | floren wrote:
         | Plan 9 does that, I'm not sure why it never made it into Unix
         | because I always loved it. Your networking-related commands
         | were in `/bin/ip` and you'd run e.g. `ip/ping 1.1.1.1`. If you
         | created the dir `$home/bin/rc/my` and put scripts in it, they
         | would have been runnable as `my/hello` etc like in your
         | example.
        
           | yellowapple wrote:
           | Plan 9 also more or less does away with the concept of $PATH
           | in the first place; just union-mount (with `bind`) whatever
           | you want onto `/bin` and call it a day. Yet another thing
           | that would've been great for Unix-likes to pull in (I think
           | some Linuxen can technically do it, but with some
           | limitations).
        
         | woodruffw wrote:
         | I don't know if I would actually recommend this, but it works:
         | $ touch foo::bar         $ chmod +x foo::bar         $
         | ./foo::bar && echo $?
         | 
         | It's an empty file so it does nothing, but most shells' lexers
         | should be smart enough to not try and lex '::'.
         | 
         | Edit: Forgot to mention, this also works with bash functions
         | and aliases. I'm not sure if POSIX sh will allow it. You can
         | also do things like this (again, not sure I'd recommend it):
         | $ alias +x='chmod +x'
        
         | eurasiantiger wrote:
         | Just add another comma. Of course, this needs some deeper magic
         | to work :)                   ,my,hello
        
         | lou1306 wrote:
         | Ahem, technically there is one way (don't know if it is "good",
         | though):                   $ export $MY = /tmp/my         $
         | $MY/hello         hello
        
         | jwilk wrote:
         | > mkdir -p /tmp/bin/my
         | 
         | This is not a secure way of using /tmp.
        
           | Taywee wrote:
           | Sure, but that's not the point. It's just an illustrative
           | example, and I wanted a short absolute path that is easily
           | recognizable to the reader. I'll fix it, though, to avoid
           | having an example with bad security practices, though.
           | 
           | edit: Or I would fix it, but I guess I can't edit it. Oh
           | well.
        
       | heynowheynow wrote:
       | Cool.
       | 
       | I have a script that creates new ~/bin scripts that checks for
       | collisions locally (functions, builtins, and executables) and in
       | linux distros.
       | 
       | For example, I have a ~/bin/up script on every box and platform
       | to install updates. So far so good on collisions.
        
       | jamespwilliams wrote:
       | This brought https://github.com/Shopify/comma to mind
        
         | scubbo wrote:
         | Do you know any of the history of why your link to
         | `.../Shopify/...` redirects to `.../nix-community/...`? Is the
         | latter an open-source contribution by Shopify?
        
           | jamespwilliams wrote:
           | I think it was originally developed and maintained by
           | Shopify, but was then handed off to the nix community
        
       | ok_dad wrote:
       | I use this method for aliases in my `.*rc` file so I can remember
       | what the heck I called things. Otherwise, I forget whether I have
       | to type something like `COMMANDNAME-log` or `log-COMMANDNAME` or
       | some other weird combo like `print-COMMANDNAME-log`. With a
       | comma, I type `,<tab>` and get all of my custom commands listed
       | easily.
        
       | [deleted]
        
       | potiuper wrote:
       | Tomte, is there any reason why are you reposting this now?
       | https://news.ycombinator.com/item?id=22778988 Such as some thing
       | that was not covered before? To all the prejudiced bigots down
       | voting this comment, the question does not imply that it should
       | not have been posted.
        
         | alar44 wrote:
         | Oh no it was posted over 2 years ago
        
         | dang wrote:
         | Reposts are ok after a year or so. This is in the FAQ:
         | https://news.ycombinator.com/newsfaq.html.
         | 
         | Could you please review the site guidelines at
         | https://news.ycombinator.com/newsguidelines.html and stick to
         | them? Your last sentence there breaks more than one of the
         | rules.
        
           | potiuper wrote:
           | Again, to reiterate, the original comment was not a statement
           | that this should not have been reposted. I am not sure how I
           | could make that clearer? I have read the site guidelines.
           | Many of them are subjective and inconsistent with unknown
           | applicability. If you, as the administrator, are unable to
           | accept that evaluation, then please just delete this account
           | and all associated posts.
        
         | geph2021 wrote:
         | Tomte has >100k karma (that seems insanely high, but also a 10+
         | year-old account), and a plethora of reposts in his submission
         | history.
         | 
         | If you wanted to juice-up your karma (I can't fathom why
         | someone would bother doing this), it seems like simply
         | reposting popular submissions from previous years is a very
         | easy way to do that (if it was popular on HN in years past,
         | then it's likely it would again). I'm not saying that's Tomte's
         | motivation.
         | 
         | This would also result in interesting/popular submissions
         | resurfacing again and again, which anecdotally I can say I've
         | seen quite a bit on HN. I don't think that's necessarily a bad
         | thing, but perhaps it can get tedious for some. For example, I
         | had not read this post before and found it useful/interesting.
        
         | bravasaurus wrote:
         | That was long enough ago that I think it's worthy of being
         | reposted. It was new to me.
        
       | TacticalCoder wrote:
       | I do something a bit special... Instead of naming my script
       | _foobarnator_ (and hence risking that another _foobarnator_ may
       | exist in the future and may clash with my script), I name my
       | script _foobarnatorToBeAliased.sh_. Then in my list of aliases I
       | alias _foobarnator_ to _foobarnatorToBeAliased.sh_.
       | 
       | The alias itself looks a bit weird in that to pass arguments to
       | an aliased command you need to use a function (in Bash at least
       | AFACIT) but it all works fine.
       | 
       | As aliases aren't sourced when other scripts are running, I'm
       | sure my scripts/commands ain't ever going to clash with anything.
       | 
       | I've been doing that since years.
       | 
       | It also works fine to add "wrappers" to existing commands: for
       | example to add additional input sanitization catching common
       | mistakes I'm making when calling some commands.
        
         | rlkf wrote:
         | Porque no los dos? It seems that                   function
         | ,foo() { printf "Fooo\n"; }         ,foo
         | 
         | actually works! (at least in my bash 5.0.17 shell)
        
         | ta8645 wrote:
         | What has always been a blocker for me to follow your example,
         | is the desire to use many such scripts from the command lines
         | provided inside an $editor or $email client. There probably is
         | a way to configure both of them to see the aliases, but it's
         | more straightforward to just have everything in the PATH.
        
           | nequo wrote:
           | Yes, it seems cleaner to just put "export
           | PATH=$HOME/bin:$PATH" in $HOME/.bashrc. That avoids
           | collisions with /usr/bin and /usr/local/bin. (Or rather,
           | resolves collisions in favor of your personal bin directory.)
        
       | jrms wrote:
       | I just add the .sh suffix/extension to them... And I use j- as
       | prefix for the "tab trick"
        
       | malkia wrote:
       | I almost always add "," in cmd.exe to anything I run from Windows
       | Command Prompt? Why? Because cygwin, or mingw's `dir.exe` would
       | be called instead of built-in dir (well, this is further
       | complicated that I don't really use `cmd.exe` but FAR Commander),
       | so if I want to enforce the built-in over external (same name)
       | command I have to prepend it with "," - so I basically started
       | typing "," in front of each of my commands...
        
       | reidjs wrote:
       | This is a good trick to avoid collisions with system commands.
       | Anytime you add one of your own executables to PATH, prefix it
       | with a comma (,). This way you avoid name collisions and have a
       | handy way of seeing what commands are available on your system
       | using tab completion (, then tab lists all of your personal
       | executables)
        
         | [deleted]
        
         | urmish wrote:
         | Is this comment from a bot? You just reiterated everything from
         | the article.
        
           | heleninboodler wrote:
           | This is an explanation that the parent comment simply
           | reiterated the point of the article rather than adding
           | content.
        
             | scubbo wrote:
             | This is a link to a story titled "This is the title of this
             | story, which is also found several times in the story
             | itself": https://stuff.mit.edu/people/dpolicar/writing/pros
             | e/text/tit...
        
       | hdjjhhvvhga wrote:
       | In the comments, the author argues against the underscore by
       | saying Bash/Zsh already uses it for completion. But it looks like
       | it's a Zsh thing - at least my Bash install is pretty
       | conservative about it.
        
         | kelnos wrote:
         | Not sure what I have installed/sourced, but "_<tab>" says there
         | are 381 possibilities for me. (This is with bash.)
         | 
         | Also in the article itself, the author argues against any
         | prefix char that requires using the shift key.
        
       | Timpy wrote:
       | If I unknowingly named my command the same as some system command
       | and the custom command ends up earlier in my path it doesn't
       | really cause any problems does it? I wasn't using that system
       | command anyways, and my custom command is only going to take
       | priority if my .bashrc file was run first.
        
         | xigoi wrote:
         | Until you use a different machine and accidentally type your
         | command without realizing it's not there.
        
         | Taywee wrote:
         | It can if you are executing programs that then try to execute
         | those commands and get your commands instead.
        
           | nerdponx wrote:
           | I wish it was easier to create "snapshots" of a particular
           | set of environment variables in order to use them later as
           | run environments. I also wish that Unix desktop environments
           | generally made it easier to manage env vars, but that's
           | another complaint. And don't get me started on PAM env
           | vars...
        
             | OJFord wrote:
             | Just env vars?                   env > "$(date +s).env-
             | snapshot"
             | 
             | (And use it again slightly more awkwardly as
             | <$file | xargs -I@ echo '"@"' | tr '\n' ' ' | xargs -I@ env
             | @ cmd
             | 
             | or something more convenient to `cmd`.)
        
             | LanternLight83 wrote:
             | Direnv has been mentioned, and it meshes well with
             | functional package managers like Nix and Guix, each of
             | which include the concept of "Profiles". Each profile can
             | access a specific set of programs via it's environment, but
             | PATH is far from the last variable that you can use Direnv
             | or Profiles to control.
        
             | fragmede wrote:
             | https://direnv.net/
        
             | verrp wrote:
             | > I wish it was easier to create "snapshots" of a
             | particular set of environment variables in order to use
             | them later as run environments.
             | 
             | docker gives you this (but only as part of a whole
             | ecosystem, which for various reasons you might not want to
             | hitch your horse to).
        
         | tasuki wrote:
         | Yes, all of that. And if you later decide to start using the
         | system command, you just rename your custom command.
        
           | eriner_ wrote:
           | I use zsh and have the following alias (rather, function):
           | 
           | ``` cp () { rsync -avhW --progress --inplace ${@} } ```
           | 
           | If I ever need to use the `cp` binary, I execute `=cp`, which
           | evaluates to `/bin/cp`.
        
       | prosaic-hacker wrote:
       | I have used a similar windows trick. I wanted to add to the
       | default window system a place for my library of tools. Most are
       | "portable" cmd apps from various sources (sysinternals, nirsoft).
       | I put them all into a directory called ] in the root of a drive.
       | 
       | Because of some common layout of keyboards the \ and ] are near
       | each other, or in reach of left and right pinkies I can type \\]\
       | before the name of the tools. This means I don't even have add a
       | dir to the path.
       | 
       | I am sure this violates someone sensibilities but it has saved
       | time over the years as a consultant when I had to deal with a
       | wonky machine and owner did not want permanent install of
       | diagnostic tools.
        
         | lkxijlewlf wrote:
         | I put c:\tools in my path, then put shortcuts to all my tools
         | in there (ie: shortcut to AgentRansack named ar). Now, I pull
         | up the run command (WND+R) type ar and agent ransack opens.
        
       | Maursault wrote:
       | The comma is employed in bash and other shell for brace
       | expansion.[1] I can't see how this would conflict with OP's
       | master plan, but I have fundamental objections towards the depths
       | of OP's laziness. Lazy is ok, but there should not be competition
       | to see who can be laziest. At some point, things just won't
       | compress any further, and the attempt at further streamlining
       | efficiency beyond what is useful has a negative effect on
       | efficiency overall. Besides, tab completion is fine as it is and
       | doesn't need what passes for a hack these days. To be clear,
       | contrary to what OP has claimed about not being creative enough
       | to come up with original script names that won't conflict with
       | the flood of new Linux commands every update (wot? LOL), this is
       | only about the OP not being satisfied with tab completion.
       | 
       | [1] https://superuser.com/a/184493
        
       ___________________________________________________________________
       (page generated 2022-06-23 23:00 UTC)