Core Events

The following events are defined by xonsh itself. For more information about events, see the events tutorial.

on_chdir

on_envvar_change

on_envvar_new

on_lscolors_change

on_post_prompt

on_postcommand

on_pre_prompt

on_pre_prompt_format

on_pre_spec_run_ls

on_precommand

on_ptk_create

on_transform_command

Listing

on_chdir(olddir: str, newdir: str) -> None

Fires when the current directory is changed for any reason.


on_envvar_change(name: str, oldvalue: Any, newvalue: Any) -> None

Fires after an environment variable is changed. Note: Setting envvars inside the handler might cause a recursion until the limit.


on_envvar_new(name: str, value: Any) -> None

Fires after a new environment variable is created. Note: Setting envvars inside the handler might cause a recursion until the limit.


on_lscolors_change(key: str, oldvalue: Any, newvalue: Any) -> None

Fires after a value in LS_COLORS changes, when a new key is added (oldvalue is None) or when an existing key is deleted (newvalue is None). LS_COLORS values must be (ANSI color) strings, None is unambiguous. Does not fire when the whole environment variable changes (see on_envvar_change). Does not fire for each value when LS_COLORS is first instantiated. Normal usage is to arm the event handler, then read (not modify) all existing values.


on_post_prompt() -> None

Fires just after the prompt returns


on_postcommand(cmd: str, rtn: int, out: str or None, ts: list) -> None

Fires just after a command is executed. The arguments are the same as history.

Parameters:

  • cmd: The command that was executed (after transformation)

  • rtn: The result of the command executed (0 for success)

  • out: If xonsh stores command output, this is the output

  • ts: Timestamps, in the order of [starting, ending]


on_pre_prompt() -> None

Fires just before showing the prompt


on_pre_prompt_format() -> None

Fires before the prompt will be formatted


on_pre_spec_run_ls(spec: xonsh.built_ins.SubprocSpec) -> None

Fires right before a SubprocSpec.run() is called for the ls command.


on_precommand(cmd: str) -> None

Fires just before a command is executed.


on_ptk_create(prompter: PromptSession, history: PromptToolkitHistory, completer: PromptToolkitCompleter, bindings: KeyBindings) ->

Fired after prompt toolkit has been initialized


on_transform_command(cmd: str) -> str

Fired to request xontribs to transform a command line. Return the transformed command, or the same command if no transformation occurs. Only done for interactive sessions.

This may be fired multiple times per command, with other transformers input or output, so design any handlers for this carefully.

Event Categories

Additionally, there are a few categories of events whose names are part of the specification of the event. These events are fired if they exist, and are ignored otherwise. Here are their specifications.


on_pre_spec_run_<cmd-name>(spec: SubprocSpec) -> None

This event fires whenever a command with a give name (<cmd-name>) has its SubprocSpec.run() method called. This is fired prior to the run call executing anything at all. This receives the SubprocSpec object as spec that triggered the event, allowing the handler to modify the spec if needed. For example, if we wanted to intercept an ls spec, we could write:

@events.on_pre_spec_run_ls
def print_when_ls(spec=None, **kwargs):
    print("Look at me list stuff!")

on_post_spec_run_<cmd-name>(spec: SubprocSpec) -> None

This event fires whenever a command with a give name (<cmd-name>) has its SubprocSpec.run() method called. This is fired after to the run call has executed everything except returning. This recieves the SubprocSpec object as spec that triggered the event, allowing the handler to modify the spec if needed. Note that because of the way process pipelines and specs work in xonsh, the command will have started running, but won’t necessarily have completed. This is because SubprocSpec.run() does not block. For example, if we wanted to get an ls spec after ls has started running, we could write:

@events.on_post_spec_run_ls
def print_while_ls(spec=None, **kwargs):
    print("Mom! I'm listing!")