Skip to content

Instantly share code, notes, and snippets.

@meramsey
Created July 24, 2021 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meramsey/e8a04da2ffaf863fe1bb7bf275811afd to your computer and use it in GitHub Desktop.
Save meramsey/e8a04da2ffaf863fe1bb7bf275811afd to your computer and use it in GitHub Desktop.
Xonsh Shell Customizations
# Save to ~/.xonshrc
# XONSH WIZARD START
# source-bash "echo loading xonsh foreign shell"
# XONSH WIZARD END
# Uncomment the below if running from appimage and need python/pip on your path from AppImage
# $PATH = [f'{$APPDIR}/usr/bin'] + $PATH
# Setup vox autovox to auto activate for .venv folders
# https://xon.sh/python_virtual_environments.html#automatically-switching-environments
@events.autovox_policy
def dotvenv_policy(path, **_):
venv = path / '.venv'
if venv.exists():
return venv
# name of the venv folder. If found will activate it.
# if set to None then local folder activation will not work.
$XSH_AVOX_VENV_NAME = ".venv"
# exclude activation of certain paths by setting
$XSH_AVOX_EXCLUDED_PATHS = {"xsh-src", "~/.virtualenvs"}
# for poetry to work with above set the below so it creates the .venv's inside the project folder for ease of use.
# https://python-poetry.org/docs/configuration/#virtualenvsin-project
# poetry config virtualenvs.create true;
# poetry config virtualenvs.in-project true;
# XONSH prompt_starship START:
# https://github.com/starship/starship
# https://github.com/anki-code/xontrib-prompt-starship
# https://starship.rs/config/
# https://gist.github.com/meramsey/95e6e50f3ac7e2e57f34919d87951104
# $STARSHIP_CONFIG = "~/.config/starship_xonsh.toml"
# $STARSHIP_CONFIG = '~/.config/starship.toml'
$XONTRIB_PROMPT_STARSHIP_LEFT_CONFIG = "~/.config/starship_xonsh_left.toml"
#$XONTRIB_PROMPT_STARSHIP_RIGHT_CONFIG = "~/.config/starship_xonsh_right.toml"
# $XONTRIB_PROMPT_STARSHIP_BOTTOM_CONFIG = "~/.config/starship_xonsh_bottom.toml"
xontrib load prompt_starship
# XONSH prompt_starship END
# https://github.com/dyuri/xontrib-powerline-binding
# xontrib load powerline_binding
# https://github.com/xxh/xxh
# https://github.com/xxh/xxh-shell-xonsh
#
xontrib load ergopack
#
# https://github.com/dyuri/xontrib-ssh-agent
# https://github.com/dyuri/repassh
# xontrib load ssh_agent
# XONSH WEBCONFIG START
#$PROMPT = '{env_name}{BOLD_GREEN}{user}@{hostname}{BOLD_BLUE}:{cwd} {branch_color}{gitstatus: {}}{RESET} {localtime}{BOLD_BLUE}{prompt_end}{RESET} '
$XONSH_COLOR_STYLE = 'solarized-dark'
xontrib load coreutils distributed output_search docker_tabcomplete autovox # avox_poetry
# XONSH WEBCONFIG END
# XONSH fzf START
# https://github.com/junegunn/fzf#installation
# sudo apt-get install fzf
# https://github.com/laloch/xontrib-fzf-widgets
$fzf_history_binding = "c-r" # Ctrl+R
$fzf_ssh_binding = "c-s" # Ctrl+S
$fzf_file_binding = "c-t" # Ctrl+T
$fzf_dir_binding = "c-g" # Ctrl+G
$fzf_find_command = "fd"
$fzf_find_dirs_command = "fd -t d"
# XONSH fzf END
"""
Awesome snippets code to make .xonshrc better - https://github.com/anki-code/awesome-xonshrc
If you like the idea click ⭐ on the repo and stay tuned.
Add awesome xonshrc to the end of current xonshrc:
curl -s https://raw.githubusercontent.com/anki-code/awesome-xonshrc/main/xonshrc >> ~/.xonshrc
"""
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
# Cross platform
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
# The SQLite history backend saves command immediately
# unlike JSON backend that save the commands at the end of the session.
$XONSH_HISTORY_BACKEND = 'sqlite'
# What commands are saved to the history list. By default all commands are saved.
# The option ‘ignoredups’ will not save the command if it matches the previous command.
# The option `erasedups` will remove all previous commands that matches and updates the frequency.
# Docs: https://xonsh.github.io/envvars.html#histcontrol
$HISTCONTROL='ignoredups,erasedups'
# Remove front dot in multiline input to make the code copy-pastable.
$MULTILINE_PROMPT=' '
# cd-ing shortcuts.
aliases['-'] = 'cd -'
aliases['..'] = 'cd ..'
aliases['....'] = 'cd ../..'
# network manager aliases for work vpn
aliases['wvpn'] = "nmcli conn up work-linux-config"
aliases['wvpnkill'] = "nmcli conn down work-linux-config"
# gp='git push origin "$(git symbolic-ref --short HEAD)"'
#
# Avoid typing cd just directory path.
# Docs: https://xonsh.github.io/envvars.html#auto-cd
$AUTO_CD = True
#
# Xontribs | Official list - https://xon.sh/xontribs.html | Github topic with thumbnails - https://github.com/topics/xontrib
#
_xontribs = [
#'whole_word_jumping', # Jumping across whole words (non-whitespace) with Ctrl + Left/Right and Alt + Left/Right.
]
if _xontribs:
xontrib load @(_xontribs)
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
# Platform specific
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
from xonsh.platform import ON_LINUX # ON_DARWIN, ON_WINDOWS, ON_WSL, ON_CYGWIN, ON_MSYS, ON_POSIX, ON_FREEBSD, ON_DRAGONFLY, ON_NETBSD, ON_OPENBSD
if ON_LINUX:
# Globbing files with “*” or “**” will also match dotfiles, or those ‘hidden’ files whose names begin with a literal ‘.’.
# Note! This affects also on rsync and other tools.
$DOTGLOB = True
# Don't clear the screen after quitting a manual page.
$MANPAGER = "less -X"
$LESS = "--ignore-case --quit-if-one-screen --quit-on-intr FRXQ"
# List all files: sorted, with colors, directories will be first (Midnight Commander style).
aliases['ll'] = lambda args: $[$LC_COLLATE='C' ls --group-directories-first -lAh --color @(args)]
# Make directory and cd into it.
# Example: md /tmp/my/awesome/dir/will/be/here
aliases['md'] = lambda args: execx(f'mkdir -p {repr(args[0])} && cd {repr(args[0])}')
# Using rsync instead of cp to get the progress and speed of copying.
aliases['cp'] = ['rsync', '--progress', '--recursive', '--archive']
# Grepping string occurrences recursively starting from current directory.
# Example: cd ~/git/xonsh && greps environ
aliases['greps'] = 'grep -ri'
# Copy output to current clipboard using xclip
# Example: echo hello | clp
aliases['clp'] = 'xclip -sel clip'
# SSH: Suppress "Connection close" message.
aliases['ssh'] = 'ssh -o LogLevel=QUIET'
# Run http server in the current directory.
aliases['http-here'] = 'python3 -m http.server'
# The example of alias that calls the command with arguments
aliases['docker-exec-bash'] = lambda args: $[docker exec -it @(args) bash]
#
# Xontribs | Official list - https://xon.sh/xontribs.html | Github topic with thumbnails - https://github.com/topics/xontrib
#
_xontribs = [
'argcomplete', # Tab completion of python and xonsh scripts. URL: https://github.com/anki-code/xontrib-argcomplete
'autoxsh', # Adds automatic execution of xonsh script files called .autoxsh when enterting a directory with cd function. URL: https://github.com/Granitas/xonsh-autoxsh
#'back2dir' # Back to the latest used directory when starting xonsh shell. URL: https://github.com/anki-code/xontrib-back2dir
#'docker_tabcomplete' # Adds tabcomplete functionality to docker inside of xonsh. URL: https://github.com/xsteadfastx/xonsh-docker-tabcomplete
#'prompt_bar', # The bar prompt for xonsh shell with customizable sections. URL: https://github.com/anki-code/xontrib-prompt-bar
#'output_search', # Get words from the previous command output for the next command. URL: https://github.com/tokenizer/xontrib-output-search
'pipeliner', # Let your pipe lines flow thru the Python code. URL: https://github.com/anki-code/xontrib-pipeliner
'pyenv', # pyenv integration for xonsh. URL: https://github.com/dyuri/xontrib-pyenv
'fzf-widgets', # Adds some fzf widgets to your xonsh shell. URL: https://github.com/laloch/xontrib-fzf-widgets
'onepath', # Associate files with app and run it without preceding commands. URL: https://github.com/anki-code/xontrib-onepath
'hist_navigator', # Move through directory history with nextd and prevd also with keybindings. URL: https://github.com/jnoortheen/xontrib-hist-navigator
'histcpy', # Useful aliases and shortcuts for extracting links and textfrom command output history and putting them into the clipboard. URL: https://github.com/con-f-use/xontrib-histcpy
'readable-traceback', # Make traceback easier to see for xonsh. URL: https://github.com/6syun9/xontrib-readable-traceback
'sh', # Paste and run commands from bash, zsh, fish, tcsh in xonsh shell. URL: https://github.com/anki-code/xontrib-sh
#'xo', # Adds an ‘xo’ alias to run the exofrills text editor in the current Python interpreter session. URL: https://github.com/scopatz/xo
#'xlsd', # xontrib-xlsd is the next gen ls command for xonsh shell, inspired by lsd. URL: https://github.com/cafehaine/xontrib-xlsd
'z', # Tracks your most used directories, based on ‘frequency’. URL: https://github.com/AstraLuma/xontrib-z
'zoxide', # Zoxide integration for xonsh. URL: https://github.com/dyuri/xontrib-zoxide
]
if _xontribs:
xontrib load @(_xontribs)
if $(which lsb_release):
linux_name, linux_version = $(lsb_release --id --release --short).strip().split('\n')
if linux_name == 'Ubuntu':
xontrib load apt_tabcomplete
pass
# XONSH zoxide START
# Prereqs
# https://github.com/ajeetdsouza/zoxide
# curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/ajeetdsouza/zoxide/master/install.sh | sh
# cargo install zoxide
# https://github.com/dyuri/xontrib-zoxide
# xpip install xontrib-zoxide
execx($(zoxide init xonsh), 'exec', __xonsh__.ctx, filename='zoxide')
# XONSH zoxide END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment