chezmoi icon indicating copy to clipboard operation
chezmoi copied to clipboard

A way to set command aliases

Open lyderic opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe.

This is not related to a problem but that'd be a nice to have.

Command aliases would be a cool feature indeed. Silly example, for illustration purpose: I could be typing chezmoi s instead of chezmoi status --verbose.

In git for example, this is achieved as follows:

$ git config alias.s 'status --verbose'

Describe the solution you'd like

An alias could be set like this:

$ chezmoi alias s 'status --verbose'

The command chezmoi alias without arguments could display a list of the current aliases.

Another way of setting up an alias could be to put entries directly in the config file:

$ cat ~/.config/chezmoi/chezmoi.yaml
aliases:
  s: "status --verbose"

Describe alternatives you've considered

The only alternative I found is to create a script and use the chezmoi-${COMMAND} facility. It works, but it is a little cumbersome for a mere alias and it's not portable across operating systems and/or shells:

$ cat > ~/bin/chezmoi-s <<EOF
> #!/bin/bash
> 
> chezmoi status --verbose
> EOF
$ chmod +x ~/bin/chezmoi-s
$ chezmoi s
(outputs status)

NOTE: setting up a bash alias doesn't work:

$ alias 'chezmoi s'='chezmoi status --verbose'
-bash: alias: `chezmoi s': invalid alias name

Additional context

The alternative I suggest works for me, using bash on Linux. I am not sure bash is provided on Mac OS anymore and I guess a bash script won't work on Windows.

lyderic avatar Mar 03 '24 11:03 lyderic

I'd like to add this, but it is not easy to do. chezmoi uses github.com/spf13/cobra for command line parsing, and sadly cobra does not support this kind of alias.

twpayne avatar Mar 03 '24 13:03 twpayne

For now the only workaround I know is creating a simple shell function. Here's one for bash, ksh, ash, and other compatible shells:

cm() {
  if [ "$1" = "st" ]; then
    shift

    chezmoi status "$@"
  else
    chezmoi "$@"
  fi
}

vaygr avatar Aug 18 '24 20:08 vaygr