Documentation

Aliases

Alias exampleAn Alias is a shortcut name that contains a list of commands to be executed. In terms of other programming languages, an Alias is like a Procedure or Method call. When the Alias name is entered on the command line, any script assigned to the Alias is executed.

For example:

#ALIAS ed {vi}

This defines an Alias named "ed". When you type "ed" on the command line and press Enter, the script assigned to the Alias is executed. In this case, "vi" is sent to the server.

As shown above, you can create an Alias on the command line using the #ALIAS command. You can also create an alias withing the Package Editor Aliases screen.

Arguments

You can pass command line arguments to an alias. This is similar to command line arguments passed to commands on the Unix or DOS command lines. Just separate each argument with a space. For example:

ed file.txt

would pass "file.txt" as the first argument on the command line.

To access the arguments within the script, use the %1..%99 syntax, where %1 is the first argument, %2 is the second argument, and so on. For example:

#ALIAS ed {vi %1}

defines an Alias named "ed" with a script that sends "vi" followed by the first argument to the server. So if you type:

ed file.txt

on the command line, then "vi file.txt" is sent to the server. Instead of trying to remember which number the argument is, you can also use "Named Arguments". To use a named argument, you must declare the argument list just after the name of the alias using the following syntax:

#ALIAS name($arg1,$arg2,...) {script}

Each named argument must start with a $ character (because a Named Argument actually creates a Local Variable). Each argument must be separated by a comma and the list is enclosed in () parenthesis.

NOTE: There must NOT be any space between the alias name and the ( parenthesis that begins the argument list.

So, the above example would now look like this:

#ALIAS ed($file) {vi $file}

That's a bit more typing for this simple example, but using Named arguments can make your scripts much easier to read when they are more complicated.

Sometimes you might want to access all of the arguments at once. The special syntax %-1..%-99 is used for this. %-1 expands to all arguments from the first to the last. %-2 expands to all arguments from the second to the last.

For example, if you typed "myalias this is a test" where "myalias" is the name of your alias, then %1 = "this" but %-1 = "this is a test". %2 = "is" and %-2 = "is a test", and so on.

When using Named arguments, you can use the %param and %params functions to access the arguments. %param($arg1) is the same as $arg1 or %1. %params($arg1) is the same as %-1. %numparam can be used to return the number of arguments that were passed to the alias.

Add comment

Login or register to post comments