Tools (

Misc. xonsh tools.

The following implementations were forked from the IPython project:


  • decode()
  • encode()
  • cast_unicode()
  • safe_hasattr()
  • indent()
exception, command, output=None, stderr=None, completed_command=None)[source]

Raised when there’s an error with a called process

Inherits from XonshError and subprocess.CalledProcessError, catching either will also catch this error.

Raised after iterating over stdout of a captured command, if the returncode of the command is nonzero.

for line in !(ls):
except subprocess.CalledProcessError as error:
print(“Error in process: {}.format(

This also handles differences between Python3.4 and 3.5 where CalledProcessError is concerned.


Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.


Alias for output attribute, to match stderr


Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.


Singleton for representing when no default value is given.


A class that implements an environment path, which is a list of strings. Provides a custom method that expands all paths if the relevant env variable has been set.


S.append(value) – append value to the end of the sequence

clear() → None -- remove all items from S
count(value) → integer -- return number of occurrences of value

S.extend(iterable) – extend sequence by appending elements from the iterable

index(value[, start[, stop]]) → integer -- return first index of value.

Raises ValueError if the value is not present.

insert(index, value)[source]
pop([index]) → item -- remove and return item at index (default last).

Raise IndexError if list is empty or index is out of range.


S.remove(value) – remove first occurrence of value. Raise ValueError if the value is not present.


S.reverse() – reverse IN PLACE


Returns the list of directories that this EnvPath contains.


Context manager for temporarily redirecting stderr to another file.


Context manager for temporarily redirecting stdout to another file:

# How to send help() to stderr
with redirect_stdout(sys.stderr):

# How to write help() to a file
with open('help.txt', 'w') as f:
    with redirect_stdout(f):

Mostly for backwards compatibility.[source]

Returns False[source]

Returns True, force=False)[source]

Returns an argument quoted in such a way that that CommandLineToArgvW on Windows will return the argument string unchanged. This is the same thing Popen does when supplied with an list of arguments. Arguments in a command line should be separated by spaces; this function does not add these spaces. This implementation follows the suggestions outlined here:[source]

Moves an existing file to a new name that has the current time right before the extension.[source]

Converts a boolean or integer to a string.[source]

Converts a sequence of bools to a comma-separated string.[source]

Converts a bool to an empty string if False and the string ‘1’ if True., encoding=None)[source][source]

Returns the starting index (inclusive), ending index (exclusive), and starting quote string of the most recent Python string found in the input.

check_for_partial_string(x) -> (startix, endix, quote)


x : str

The string to be checked (representing a line of terminal input)


startix : int (or None)

The index where the most recent Python string found started (inclusive), or None if no strings exist in the input

endix : int (or None)

The index where the most recent Python string found ended (exclusive), or None if no strings exist in the input OR if the input ended in the middle of a Python string

quote : str (or None)

A string containing the quote used to start the string (e.g., b”, ”, ‘’‘), or None if no string was found.[source]

Returns the current color map.[source]

Returns an iterable of all available style names., width=80, newline='\n')[source]

Takes an iterable of strings and returns a list of lines with the elements placed in columns. Each line will be at most width columns. The newline character will be appended to the end of each line.[source]

Uses the debian/ubuntu command-not-found utility to suggest packages for a command that cannot currently be found.[source]

Takes a comma-separated string and converts it into a list of bools.[source]

Convert a comma-separated list of strings to a set of strings., encoding=None)[source][source]

Tries to decode the bytes using XONSH_ENCODING if available, otherwise using sys.getdefaultencoding().[source]

Prints the error message of the current exception on stderr.[source]

Convert a canonical cwd_width tuple to a string., encoding=None)[source][source]

Try to convert an object into a slice, complain on failure[source]

Returns a string if x is not a string, and x if it already is., datetime_format=None)[source][source]

Converts an environment path to a string by joining on the OS separator.[source]

Returns a string that is usable by the Windows cmd.exe. The escaping is based on details here and emperical testing:[source]

Returns a generator of files in path that the user could execute.[source]

Expands a string to a case insensitive globable string.[source]

Expand the style’s gray scale color range. All gray scale colors has a tendency to map to the same default GRAY in cmd.exe., expand_user=True)[source]

Takes a string path and expands ~ to home if expand_user is set and environment vars if EXPAND_ENV_VARS is set.[source]

Provides user expanded absolute path[source]

Expand shell variables of the forms $var, ${var} and %var%. Unknown variables are left unchanged., backup)[source]

Decorator for returning the object if cond is true and a backup if cond is false., mincol=0, lexer=None)[source]

Returns the column number of the next logical break in subproc mode. This function may be useful in finding the maxcol argument of subproc_toks()., substrs)[source]

Finds whichever of the given substrings occurs first in the given string and returns that substring, or returns None if no such strings occur., **kwargs)[source]

Formats strings that may contain colors. This simply dispatches to the shell instances method of the same name. The results of this function should be directly usable by print_color().[source]

Format datetime object to string base on $XONSH_DATETIME_FORMAT Env., env=None)[source]

Formats a template prefix/postfix string for a standard buffer. Returns a string suitable for prepending or appending., slices)[source]

Yield from portions of an iterable.


it: iterable

slices: a slice or a list of slice objects[source]

Returns the appropriate filepath separator char depending on OS and xonsh options set, ignore_case=False, return_empty=False, sort_result=None)[source]

Simple wrapper around glob that also expands home and env vars.[source]

Converts a valid history tuple to a canonical string., ignore_case=False, sort_result=None)[source]

Simple wrapper around iglob that also expands home and env vars., nspaces=4, ntabs=0, flatten=False)[source]

Indent a string a given number of spaces or tabstops.

indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.


instr : basestring

The string to be indented.

nspaces : int (default: 4)

The number of spaces to be indented.

ntabs : int (default: 0)

The number of tabs to be indented.

flatten : bool (default: False)

Whether to scrub existing indentation. If True, all lines will be aligned to the same indentation. If False, existing indentation will be strictly increased.


outstr : string indented by ntabs and nspaces., replace_colors=None, ansi=False)[source]

Returns a modified style to where colors that maps to dark colors are replaced with brighter versions. Also expands the range used by the gray colors[source]

Resets the style when setting the INTENSIFY_COLORS_ON_WIN environment variable., ltok, rtok)[source]

Determines whether an expression has unbalanced opening and closing tokens.[source]

Tests if something is a boolean.[source]

Returns whether a value is a boolean or integer.[source]

Tests if an object is a sequence of bools.[source]

Tests if something is callable[source]

Tests if something is a class[source][source]

Determine if the input is a valid input for the DYNAMIC_CWD_WIDTH environement variable.[source]

This tests if something is an environment path, ie a list of strings.[source]

Tests if something is a float[source]

Tests if something is a valid history backend.[source]

Tests if something is a proper history value, units tuple.[source]

Tests if something is an integer[source]

Test if string x is an integer. If not a string return False.[source]

Checks if x is a valid $XONSH_TRACEBACK_LOGFILE option. Returns False if x is not a writable/creatable file or an empty string or None.[source]

Tests if something is a sequence of strings, where the top-level sequence is not a string itself.[source]

Tests if something is a slice[source]

Test if string x is a slice. If not a string return False.[source]

Tests if something is a string[source]

Tests if something is a string or callable[source]

Tests if something is a sequence of strings[source]

Tests if something is a set of strings[source]

Checks if a filepath is valid for writing., b, max_dist=inf)[source]

Calculates the Levenshtein distance between a and b.[source]

Detypes a $XONSH_TRACEBACK_LOGFILE option.[source]

Retuns as normalized absolute path, namely, normcase(abspath(p))[source]

Checks if we are on the main thread or not.[source]

Converts a os.pathsep separated string to a sequence of strings.[source]

Converts a os.pathsep separated string to a set of strings.[source]

Converts a os.pathsep separated string to a sequence of uppercase strings., **kwargs)[source]

Prints a string that may contain colors. This dispatched to the shell method of the same name. Colors will be formatted if they have not already been.[source]

Print exceptions with/without traceback., attr)[source]

In recent versions of Python, hasattr() only catches AttributeError. This catches all errors.[source]

Converts a sequence to an os.pathsep separated string.[source]

Converts a sequence to an uppercase os.pathsep separated string.[source]

Convert a set of strings to a comma-separated list of strings., sort=False)[source]

Converts a set to an os.pathsep separated string. The sort kwarg specifies whether to sort the set prior to str conversion.[source]

“Enables or disables unicode display on windows.[source]

Converts a string to an environment path, ie a list of strings, splitting on the OS separator., ltok, rtok)[source]

Obtains the expression prior to last unblanced left token., ltok, rtok)[source]

Attempts to pull out a valid subexpression for unbalanced grouping, based on opening tokens, eg. ‘(‘, and closing tokens, eg. ‘)’. This does not do full tokenization, but should be good enough for tab completion., mincol=-1, maxcol=None, lexer=None, returnline=False)[source]

Excapsulates tokens in a source code line in a uncaptured subprocess ![] starting at a minimum column. If there are no tokens (ie in a comment line) this returns None., env, aliases)[source]

Suggests alternative commands given an environment and aliases., y)[source]

Returns a score (lower is better) for x based on how similar it is to y. Used to rank suggestions., name, value, default=<object object>)[source]

Swaps a current variable name in a namespace for another value, and then replaces it when the context is exited., updates, default=<object object>)[source]

Updates a dictionary (or other mapping) with values from another mapping, and then restores the original mapping when the context is exited.[source]

“Converts to a boolean in a semantically meaningful way.[source][source]

Converts a value to a boolean or an integer.[source][source]

Convert to a canonical cwd_width tuple.[source]

Converts to a canonincal history tuple.[source]

No conversion, returns itself.[source]

Converts a $XONSH_TRACEBACK_LOGFILE option to either a str containing the filepath if it is a writable file or None if the filepath is not valid, informing the user on stderr about the invalid choice.[source]

Decorator that specifies that a callable alias should not be run with any capturing. This is often needed if the alias call interactive subprocess, like pagers and text editors.[source]

Decorator that specifies that a callable alias should be run only on the main thread process. This is often needed for debuggers and profilers. = {'': ('commands', <class 'int'>), 'b': ('b', <class 'int'>), 'sec': ('s', <class 'float'>), 'files': ('files', <class 'int'>), 'kilobyte': ('b', <function <lambda>>), 'meg': ('b', <function <lambda>>), 'mins': ('s', <function <lambda>>), 'kb': ('b', <function <lambda>>), 'bytes': ('b', <class 'int'>), 'second': ('s', <class 'float'>), 'mon': ('s', <function <lambda>>), 'days': ('s', <function <lambda>>), 'yrs': ('s', <function <lambda>>), 'gig': ('b', <function <lambda>>), 'month': ('s', <function <lambda>>), 'hour': ('s', <function <lambda>>), 'terabyte': ('b', <function <lambda>>), 'gigabyte': ('b', <function <lambda>>), 'c': ('commands', <class 'int'>), 'f': ('files', <class 'int'>), 'mb': ('b', <function <lambda>>), 's': ('s', <class 'float'>), 'day': ('s', <function <lambda>>), 'gb': ('b', <function <lambda>>), 'gigabytes': ('b', <function <lambda>>), 'megs': ('b', <function <lambda>>), 'megabytes': ('b', <function <lambda>>), 'kilobytes': ('b', <function <lambda>>), 'cmd': ('commands', <class 'int'>), 'd': ('s', <function <lambda>>), 'gigs': ('b', <function <lambda>>), 'megabyte': ('b', <function <lambda>>), 'terabytes': ('b', <function <lambda>>), 'byte': ('b', <class 'int'>), 'hours': ('s', <function <lambda>>), 'commands': ('commands', <class 'int'>), 'm': ('s', <function <lambda>>), 'seconds': ('s', <class 'float'>), 'year': ('s', <function <lambda>>), 'y': ('s', <function <lambda>>), 'cmds': ('commands', <class 'int'>), 'months': ('s', <function <lambda>>), 'min': ('s', <function <lambda>>), 'command': ('commands', <class 'int'>), 'years': ('s', <function <lambda>>), 'tb': ('b', <function <lambda>>), 'hr': ('s', <function <lambda>>), 'yr': ('s', <function <lambda>>), 'h': ('s', <function <lambda>>)}

Maps lowercase unit names to canonical name and conversion utilities. = re.compile('([bBrRuU]*("""|\'\'\'|"|\'))')

Regular expression matching the start of a string, including quotes and leading characters (r, b, or u) = <xonsh.lazyasd.LazyDict object>

Dictionary mapping starting quote sequences to regular expressions that match the contents of a string beginning with those quotes (not including the terminating quotes) = re.compile('[bBrRuU]*')

Regular expression matching the characters before the quotes when starting a string (r, b, or u, case insensitive)[source]