Alias: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Added information about aliases with quotation marks within them)
m (changed explanation to use key template)
Line 21: Line 21:




A common use of <code>alias</code> is to run commands when a key is unpressed. for example, the following aliases can be used to test when <code>alt</code> is pressed and unpressed to make <code>alt + f4</code> exit the engine
A common use of <code>alias</code> is to run commands when a key is unpressed. for example, the following aliases can be used to test when {{key|alt}} is pressed and unpressed to make {{key|alt+f4}} exit the engine
  bind alt "+alt_pressed"
  bind alt "+alt_pressed"
  alias +alt_pressed "bind f4 exit"
  alias +alt_pressed "bind f4 exit"

Revision as of 15:59, 21 December 2022

alias is a console command that allows a specified command to invoke one or several other commands. Aliases will only act for the game session they were defined in. However, keys bound can last if config.cfg is not set to read-only. It is common for aliases to be defined in configuration files.

If no parameters are given, alias will print a list of all aliases and their values.

Examples

After running the following command, typing foo in the console will execute echo bar

alias foo "echo bar"


An example of an alias that toggles zoom. running ZoomToggle will alternate between running +zoom and -zoom

alias "ZoomON"	   "+zoom; alias ZoomToggle ZoomOFF"
alias "ZoomOFF"    "-zoom; alias ZoomToggle ZoomON"
alias "ZoomToggle" "ZoomON"


An example of using aliases to increment/decrement the field of view. Note that running alias fovIN makes the command fovIN no longer do anything

alias "fov_85" "fov 85; alias fovIN;        alias fovOUT fov_90"
alias "fov_90" "fov 90; alias fovIN fov_85; alias fovOUT fov_95"
alias "fov_95" "fov 95; alias fovIN fov_90; alias fovOUT"
alias "fovIN"  "fov_85"
alias "fovOUT" "fov_95"


A common use of alias is to run commands when a key is unpressed. for example, the following aliases can be used to test when alt is pressed and unpressed to make alt+f4 exit the engine

bind alt "+alt_pressed"
alias +alt_pressed "bind f4 exit"
alias -alt_pressed "unbind f4"


The alias command is unique in that it does not require quotation marks for the contents of the alias. This means that aliases can be mapped to commands with quotation marks in them, as long as there are no external quotation marks.

alias healthpls "ent_fire !self addoutput "health 200"" //does not work
alias healthpls ent_fire !self addoutput "health 200"   //works


While this works for aliases mapped to single commands, it does not work with a list of semicolon-seperated commands as the contents of an alias, because what follows the first semicolon will be executed independently from the alias command. This can be avoided by mapping the alias to execute a file that has the commands as its contents.

alias healthpls ent_fire !self addoutput "health 200"; echo health restored. //does not work

The following, however, will work:

alias healthpls "exec health_wrapper.cfg"

Contents of health_wrapper.cfg:

 ent_fire !self addoutput "health 200"
 echo health restored.

Advanced Uses

The alias command alone makes the source console Turing complete. In other words, it is able to simulate any computer calculation, meaning a command can be run based on an arbitrarily complex way that aliased commands were invoked. For example, an alias can run another command if the amount of times the alias was invoked previously was prime. There are many uses that are more practical.

See Also

Console Command List