Hacker News Re-Imagined

Start all of your commands with a comma (2009)

  • 592 points
  • 11 days ago

  • @Tomte
  • Created a post

Start all of your commands with a comma (2009)


@notaclevername 11 days

Replying to @Tomte 🎙

I use a similar trick for text expansion. All my abbreviations start with a semicolon (;). It’s on the home row, it never occurs at the start of a word in normal typing so I never have a false positive expansion, and it’s easy to remember.

Reply


@keybored 11 days

Replying to @Tomte 🎙

Cool.

I’m gonna start them with `å` just to be a little different.

Reply


@gregnavis 11 days

Replying to @Tomte 🎙

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

Reply


@ghostly_s 11 days

Replying to @Tomte 🎙

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.

Reply


@gumby 11 days

Replying to @Tomte 🎙

clever idea, but does it need such a long post to describe?

Reply


@avodonosov 11 days

Replying to @Tomte 🎙

Is it secure to have ~/bin/ it the PATH?

Reply


@aendruk 11 days

Replying to @Tomte 🎙

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.

Reply


@pxeger1 11 days

Replying to @Tomte 🎙

In Zsh, you can used named "directories" (actually it works with normal files too) with tildes for the same effect, except that they must be explicitly defined, so there's no risk of collisions.

    hash -d e=/bin/echo
    ~e hello world
https://ato.pxeger.com/run?1=m724qjhjwYKlpSVpuhY3lTMSizMUdFM...

Reply


@BiteCode_dev 10 days

Replying to @Tomte 🎙

Can't you just declare:

    my() {
  script_name="$1"
  script_path="$HOME/.local/bin/my/$script_name"
  shift
  $script_path "$@"
    } 
So you have your namespace ? Of course that mean you must put all script in .local/bin, not anywhere in the PATH, and some more work is needed to figure out completion.

But it seems less weird than the comma.

-- EDIT --

With completion:

export MY_SCRIPS_DIR="$HOME/.local/bin/my"

_my_completions() {

   if [ "${#COMP_WORDS[@]}" != "2" ]; then
     return 
   fi

   COMPREPLY=($(compgen -W "$(echo $(find "$MY_SCRIPS_DIR" -printf '%P\n' ))"))
 }


 my() {
  script_name="$1"
  script_path="$MY_SCRIPS_DIR/$script_name"
  shift
  $script_path "$@"
 } 

 complete -F _my_completions my
-- EDIT 2 --

After testing both, the comma win. Kiss, more flexible.

Reply


@dan353hehe 11 days

Replying to @Tomte 🎙

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!

Reply


@parentheses 11 days

Replying to @Tomte 🎙

the aesthetics of the comma for daily use are not good

Reply


@vimsee 11 days

Replying to @Tomte 🎙

I started creating wrapper-scripts or simple scripts that has the commands with my prefered options or a selection menu for said command. I have started calling those scripts my<command>

rsync becomes myrsync mount becomes mymount ssh becoms myssh

No collision and the naming convention empathise that it is indeed used be MYself.

Reply


@Taywee 11 days

Replying to @Tomte 🎙

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

Reply


@heynowheynow 11 days

Replying to @Tomte 🎙

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.

Reply


@jamespwilliams 11 days

Replying to @Tomte 🎙



@tom_ 10 days

Replying to @Tomte 🎙

I have my bin folder in ~/usr/bin. The structure of ~/usr matches /usr and /usr/local, so I can build stuff into it with ./configure --prefix=$HOME/usr or similar. (You could of course do ./configure --prefix=$HOME, but then you run the risk of ending up with ~/share and ~/include and so on, something I don't want.)

Reply


@ok_dad 11 days

Replying to @Tomte 🎙

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.

Reply


@TacticalCoder 11 days

Replying to @Tomte 🎙

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.

Reply


@ordiel 10 days

Replying to @Tomte 🎙

Why people worry so much to create "short, easy to type" (coff coff cryptic) command names, nomenclature is so important in our profession and almost everybody sucks at it, give meaningful names to things even if they are 30+ chars long, is it really that though to press 3 or 4 keys along with maybe 3 or 4 tabs?

Reply


@luispa 11 days

Replying to @Tomte 🎙

haha neat.

Reply


@quickthrower2 11 days

Replying to @Tomte 🎙

Finally, a weird tip that is really good!

Reply


@jrms 11 days

Replying to @Tomte 🎙

I just add the .sh suffix/extension to them... And I use j- as prefix for the "tab trick"

Reply


@ZeroGravitas 11 days

Replying to @Tomte 🎙

Similar to the vim <leader> key concept which many people use comma for.

https://tuckerchapman.com/2018/06/16/how-to-use-the-vim-lead...

Reply


@malkia 11 days

Replying to @Tomte 🎙

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

Reply


@reidjs 11 days

Replying to @Tomte 🎙

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)

Reply


@hdjjhhvvhga 11 days

Replying to @Tomte 🎙

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.

Reply


@Timpy 11 days

Replying to @Tomte 🎙

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.

Reply


@krnlpnc 11 days

Replying to @Tomte 🎙

I do the same thing but with prefix …

Reply


@jedberg 11 days

Replying to @Tomte 🎙

Commas can also be used in filenames to help you sort things instead of using underscore as a separator, useful for the same reason -- you don't have to hit shift to type it.

Reply


@prosaic-hacker 11 days

Replying to @Tomte 🎙

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.

Reply


@uint16_t 9 days

Replying to @Tomte 🎙

To mitigate issues you could just append ~/bin/ to $PATH (opposed to prepending). Then system commands have priority over your user home commands, right?

Reply


@boffinAudio 10 days

Replying to @Tomte 🎙

Meh. I'd prefer to have a path not in my PATH, and then explicitly call the binary with the full path .. and then append a #comment for easy recall with CTRL-R at some point later in the future.

A bit more fiddly perhaps, but future proof.

Reply


@tlarkworthy 11 days

Replying to @Tomte 🎙

I misadvertantly lower cased my functions names when working on .net, but it made it super easy to find my code in the stack traces. (.net idiom is capitalism the first case of a function name). Usually the cause of stack traces was my code so it was an unexpected productivity booster.

Reply


@Maursault 11 days

Replying to @Tomte 🎙

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

Reply


About Us

site design / logo © 2022 Box Piper