2020-05-03 06:49:27 +02:00
# alias
2020-06-23 20:21:47 +02:00
2020-05-24 19:42:20 +02:00
This command allows you to define shortcuts for other common commands. By default, they only apply to the current session. To persist them, add `--save` .
Syntax: `alias {flags} <name> [<parameters>] {<body>}`
2020-05-03 06:49:27 +02:00
The command expects three parameters:
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
* the name of alias
* the parameters as a space-separated list (`[a b ...]`), can be empty (`[]`)
* the body of the alias as a `{...}` block
2020-05-24 19:42:20 +02:00
## Flags
* `-s` , `--save` : Save the alias to your config (see `config --path` to edit them later)
2020-05-03 06:49:27 +02:00
## Examples
Define a custom `myecho` command as an alias:
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
```shell
> alias myecho [msg] { echo $msg }
> myecho "hello world"
hello world
```
Since the parameters are well defined, calling the command with the wrong number of parameters will fail properly:
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
```shell
> myecho hello world
error: myecho unexpected world
- shell:1:18
1 | myecho hello world
| ^^^^^ unexpected argument (try myecho -h)
```
The suggested help command works!
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
```shell
> myecho -h
Usage:
> myecho ($msg) {flags}
parameters:
($msg)
flags:
-h, --help: Display this help message
```
## Persistent aliases
Aliases are most useful when they are persistent. For that, add them to your startup config:
2020-06-23 20:21:47 +02:00
```shell
2020-05-03 06:49:27 +02:00
> config --set [startup ["alias myecho [msg] { echo $msg }"]]
```
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
This is fine for the first alias, but since it overwrites the startup config, you need a different approach for additional aliases.
To add a 2nd alias:
2020-06-23 20:21:47 +02:00
```shell
> config --get startup | append "alias s [] { git status -sb }" | config --set_into startup
2020-05-03 06:49:27 +02:00
```
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
This first reads the `startup` config (a table of strings), then appends another alias, then sets the `startup` config with the output of the pipeline.
To make this process easier, you could define another alias:
2020-06-23 20:21:47 +02:00
```shell
2020-05-03 06:49:27 +02:00
> alias addalias [alias-string] { config --get startup | append $alias-string | config --set_into startup }
```
2020-06-23 20:21:47 +02:00
2020-05-03 06:49:27 +02:00
Then use that to add more aliases:
2020-06-23 20:21:47 +02:00
```shell
> addalias "alias s [] { git status -sb }"
2020-05-03 06:49:27 +02:00
```