What is this autoexec.cfg, and what does it do?

I see a lot of configurations for Source games(Valve games) using an autoexec.cfg file, and I wonder why it is used instead of the config.cfg that is already in the game folder of each Source game.

What is an autoexec.cfg file and what does it do?

Furthermore do I have any advantages using that instead of in-game consoles or config.cfg?


In short:

It is used because it preserves your configuration. If you edit the config.cfg or use the ingame console directly the settings will only stay until you closed the game.

First we need to clarify what happens when you start a source-engine game from steam.

At startup steam - if activated - loads a config from the cloud and checks back with the local config. After that the config is loaded into the game but not into the settings.

However if you change the settings via ingame menu - let's say controls for example - the config file will be rewritten from scratch.

That's why changing the config directly might work for one startup but will be reset after the game closes because the game rewrites the config too at this point.

Autoexec

Autoexec does execute commands and settings on startup after the cloud sync and rewrites the config. There is no autoexec.cfg file per default so it needs to be created in the same folder as config.cfg (For example DotA2: …\Steam\SteamApps\common\dota 2 beta\dota\cfg\, some games have it's cfg folder one directory up).

If you open the settings via ingame menu you will still see the old values because those consumerfriendly settings have higher priority resulting in another rewrite of the config. To circumvent this you need to unbind all keys from the ingame menu and set it once manually in the autoexec. This is not necessary for settings that have no binding at all like con_enable 1

However the autoexec itself is not touched by the source-engine, only read. So the config will be rewritten on every startup without the burden of changing the config itself every time.

Another note:

Using autoexec enables you to specify settings for your local machine that will not be overwritten if changed by another machine.

For example imaging changing the net_graph position for your laptop resolution. Without usage of autoexec it will be saved in the cloud and overwrite your desktop configuration forcing you to adjust the net_graph position to your 1920x1080 desktop resolution again.

If you use autoexec and start the game on the desktop resolution it will overwrite the net_graph position and adjust it for you since you defined it previously in the autoexec.

Example

// Net_graph positioning
net_graph "1"
net_graphinsetbottom "438"
net_graphinsetright "-80"
net_graphproportionalfont "0"

echo "autoexec loaded"

Lines with // will be ignored (comment). The remaining lines contain a command and a corresponding value. echo is a command to write any text (value here is "autoexec loaded") into the ingame console so you can see that your autoexec was loaded.

Furthermore if you changed your autoexec.cfg and you do not want to restart the game you can use the exec command like exec autoexec.cfg.

In fact you can exec any .cfg file but autoexec.cfg gets automatically loaded at startup.

Summary

Using autoexec.cfg is the safest way to control configurations in a source-engine game(valve) game and should be used especially for general settings like networking, graphics, etc