Autocomplete integration script explained Autocomplete integration script explained

Autocomplete integration script explained

Dmitry Babinsky Dmitry Babinsky

When autocomplete feature is enabled, Termius will execute a script on every session connection. This script is needed to set up a shell integration.

if [ "$_t_orig_ps1" = "" ]; then
_t_orig_ps1="$PS1" _t_set_cwd() { printf '\001\033]4545;P;Cwd=$(pwd)\007\002' ; } PS1="$(_t_set_cwd)$_t_orig_ps1" fi

The following script provides access for Termius to a current user path. It modifies the command prompt by adding the current working directory to it. Find the full explanation of this script bellow:

  1. if [ "$_t_orig_ps1" = "" ]; then: Checks if the variable _t_orig_ps1 is empty. This variable is used to store the original value of PS1 before it is modified.
  2. _t_orig_ps1="$PS1": If _t_orig_ps1 is empty (meaning the script has not been sourced before), store the current value of PS1 in _t_orig_ps1.
  3. _t_set_cwd() { printf '\001\033]4545;P;Cwd=$(pwd)\007\002' ; }: Defines a function called _t_set_cwd, which uses the printf command to output a special sequence of characters with the current working directory $(pwd). This sequence can be interpreted by some terminal emulators to display the working directory.
  4. PS1="$(_t_set_cwd)$_t_orig_ps1": Sets the new value of PS1 by calling the _t_set_cwd function and appending the original value of PS1 stored in _t_orig_ps1. This adds the current working directory to the command prompt without losing the original appearance.
  5. fi: Closes the if statement, marking the end of the code snippet.

In summary, this code snippet checks if the original PS1 variable has been saved. If not, it saves it, defines a function to add the current working directory to the prompt, and then modifies the `PS1` variable to include the current working directory while retaining the original appearance of the command prompt.

Add comment

Please sign in to leave a comment.