Settings

Theme

Show HN: Redo – Command line utility for quickly creating shell functions

github.com

124 points by silentprog 4 years ago · 31 comments

Reader

Crespyl 4 years ago

Not to be confused with the redo build system[0]

[0] https://redo.readthedocs.io/en/latest/

chubot 4 years ago

FWIW the title says "functions", but it apparently uses aliases.

Shell functions have all the same functionality as aliases and they'll catch more syntax errors upon "source myscript". Example:

    ls() {
      command ls --color=auto "$@"
    }
    
is equivalent to

    alias ls='ls --color=auto'
but IMO less error prone. You also get syntax highlighting.

The only tricky thing is to remember the 'command' prefix if the "alias" has the same name as the command. Otherwise you'll get an infinite loop of the function trying to call itself!

  • emmelaich 4 years ago

    Note there is one possibly important difference; aliases are resolved before globbing.

    • chubot 4 years ago

      Hm what's an example of where you might want to take advantage of that?

      In practice

          alias 'lspy=ls *.py'
      
      seems to be interchangeable with

          lspy() {
            ls *.py "$@"
          }
      • emmelaich 4 years ago

        I have a semi-made desk calculator with prefix notation.

        You can make it an alias of * , / etc, so ...

           $ * 4 6
           24
           $
        
        It would keep it's own stack, but let a persistent process do the actual maths.

        The persistent process could almost any repl of python, ruby etc.

        Or use a 'real' calculator like Pike's ivy; https://aplwiki.com/wiki/Ivy

        • emmelaich 4 years ago

          Unfortunately, ( is not a valid alias name, so lispy things are difficult.

          [ is a valid function name tho so fun could be had there.

        • emmelaich 4 years ago

          Also = is a valid function name.

          So you could use = to start the calc, and use infix or postfix notation.

    • abathur 4 years ago

      You can do some wild ~metaprogramming stuff with aliases.

  • silentprogOP 4 years ago

    It generates functions. Though not with a “$@“ at the end, so passing additional arguments at this moment is not supported. I also run a syntax check before the function is added to the file.

  • 8n4vidtmkvmk 4 years ago

    I've always just written that as \ls to call the non-alias

    • emmelaich 4 years ago

      Won't work with functions, so be careful.

          $ function ls () {
          > echo denied
          > }
          $ ls
          denied
          $ \ls
          denied
buu700 4 years ago

Currently, I'll append a comment to a frequently used command for easy searching from my history. For a simple example, I can access `git diff -w ; git diff -w --cached # gitdiff` by pressing Ctrl+R and typing `# gitd`.

For commands I use frequently or that are clunky to maintain as one-liners, I'll convert them into functions in my bashrc.

This seems like the best of both worlds in many ways, or at least is a great third option to have.

junkblocker 4 years ago

I use this https://github.com/erichs/composure which seems to do more.

stevenhuang 4 years ago

I do this whenever working on terminal.

The mnemonic is erc=edit rc, src=source rc.

alias erc="vim ~/.bash_aliases" alias src="source ~/.bash_aliases"

Whenever I need to add or modify some shell function or alias (or even make a quick note) I type erc to open the alias file. Then I type src to load it. Very handy.

felixhummel 4 years ago

My workflow looks like this:

hack hack, then run

    nuscript-bash something
Hit C-x C-h, select some lines, edit, then run

    something
To do maintenance:

    edcom something
I wrote a little more about it here: https://blag.felixhummel.de/basics/bash/wrapper-scripts.html
opan 4 years ago

This looks very cool. I use shell aliases heavily on all my devices and try to avoid typing out the same one-liners more than once or twice when possible.

  • silentprogOP 4 years ago

    Thank you! Currently the software is very much alpha, so some bugs are expected. I also use shell aliases a lot, though redo is currently not creating aliases but shell functions. In the future I want to add support for creating actual aliases as well instead of shell functions. A simple solution would be to not create a function but an actual alias and chain the commands with &&, this would work but then you lose the power to edit the function and for example have arguments to some commands and not to others.

unixhero 4 years ago

This reminds me of what I had in 1995 with the 4DOS.COM command interpreter (command.com replacement).

replwoacause 4 years ago

Love it! I am going to start using it immediately.

qorrect 4 years ago

Very cool

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection