Skip to content

Instantly share code, notes, and snippets.

@austintraver
Last active July 13, 2021 13:59
Show Gist options
  • Select an option

  • Save austintraver/87a3d684b899d1291b59fe50cf9d929d to your computer and use it in GitHub Desktop.

Select an option

Save austintraver/87a3d684b899d1291b59fe50cf9d929d to your computer and use it in GitHub Desktop.
A set of sensible defaults for a shell environment running on a remote host.
# _
# _______| |__ _ __ ___
# |_ / __| '_ \| '__/ __|
# / /\__ \ | | | | | (__
# /___|___/_| |_|_| \___|
# ========================
# Share command history between active shell sessions
setopt SHARE_HISTORY
# Save command timestamps
setopt EXTENDED_HISTORY
# Trim unnecessary whitespace from commands in history
setopt HIST_REDUCE_BLANKS
# Don't save command if preceded by whitespace
setopt HIST_IGNORE_SPACE
# If the internal history needs to be trimmed to add the current command line,
# remove the oldest history event that has a duplicate. You should be sure to set the value of HISTSIZE to a larger number than SAVEHIST in order to give you some room for the duplicated events, otherwise this option will behave just like HIST_IGNORE_ALL_DUPS once the history fills up with unique events.
setopt HIST_EXPIRE_DUPS_FIRST
# If a new command line being added to the history list duplicates an older one
# then the older command is removed from the list
setopt HIST_IGNORE_ALL_DUPS
# When searching for history entries in the line editor,
# do not display duplicates of a line previously found,
# even if the duplicates are not contiguous.
setopt HIST_FIND_NO_DUPS
# ---------------------------------------------------------
# In the line editor, the number of matches to list without asking first. If
# the value is negative, the list will be shown if it spans at most as many
# lines as given by the absolute value. If set to zero, the shell asks only if
# the top of the listing would scroll off the screen.
typeset -gx LISTMAX=0
# Duration to wait for further input during a key-sequence
# (in hundredths of a second)
typeset -gx KEYTIMEOUT=20
# `pushd` and `popd` will no longer print the directory name
setopt PUSHD_SILENT
# `pushd` with no arguments act like 'pushd $HOME'
setopt PUSHD_TO_HOME
# `cd` pushes the old directory onto the directory stack
setopt AUTO_PUSHD
# `cd` is run if a valid command is not found
setopt AUTO_CD
# If arg is not a directory, see if it's a variable, and `cd` to its value
setopt CDABLE_VARS
# Immediately cycle through options for tab completion
setopt MENU_COMPLETE
# Output hex numbers in the familiar C syntax (e.g. '0x2f')
setopt C_BASES
# Output octal numbers in the familiar C syntax (e.g. '077')
setopt OCTAL_ZEROES
# Do arithmetic evaluation using C's operator precedence
setopt C_PRECEDENCES
# Clear right-side prompt upon submission of command
setopt TRANSIENT_RPROMPT
# Use a version of `echo` compatible with bash
setopt BSD_ECHO
# Enable prompt substitution
setopt PROMPT_SUBST
# When disowning a process, send a SIGCONT
setopt AUTO_CONTINUE
# Single letter commands call `fg`
setopt AUTO_RESUME
# Allow comments, even in an the interactive shell
setopt INTERACTIVE_COMMENTS
# List jobs in a verbose format
setopt LONG_LIST_JOBS
# Do not beep during listing completions
setopt NO_LIST_BEEP
# Print the exit value of programs with non-zero exit status
setopt PRINT_EXIT_VALUE
# Don't globally export environment variables by default
setopt NO_GLOBAL_EXPORT
bindkey -v '^P' vi-up-line-or-history
bindkey -v '^N' vi-down-line-or-history
bindkey -v '^F' vi-forward-char
bindkey -v '^B' vi-backward-char
bindkey -v '^A' vi-beginning-of-line
bindkey -v '^E' vi-end-of-line
bindkey -v '^U' backward-kill-line
bindkey -v '^K' vi-kill-eol
bindkey -v '^R' history-incremental-pattern-search-backward
bindkey -v '^S' history-incremental-pattern-search-forward
bindkey -v '^[f' vi-forward-word
bindkey -v '^[b' vi-backward-word
bindkey -v '^H' backward-delete-char
bindkey -v '^?' backward-delete-char
bindkey -a '^H' backward-delete-char
bindkey -a '^?' backward-delete-char
# Also add the GNU readline keybinding for accepting the current search
bindkey -M isearch '^M' accept-search
bindkey -M isearch '^J' accept-search
# Enable the use of regular expressions during a history search
# w/ GNU readline style history search
bindkey -M isearch '^R' history-incremental-pattern-search-backward
bindkey -M isearch '^S' history-incremental-pattern-search-forward
# w/ vi style history search
bindkey -M isearch '^P' history-incremental-pattern-search-backward
bindkey -M isearch '^N' history-incremental-pattern-search-forward
# Enable zsh surround
autoload -Uz surround
zle -N delete-surround surround
bindkey -a 'ds' delete-surround
zle -N add-surround surround
bindkey -a 'ys' add-surround
bindkey -M visual 'S' add-surround
zle -N change-surround surround
bindkey -a 'cs' change-surround
# Enable changing text vi-style within delimiters ' " `
autoload -Uz select-quoted
zle -N select-quoted
for char in {a,i}{\',\",\`}; {
bindkey -M visual ${char} select-quoted
bindkey -M viopp ${char} select-quoted
}
# Enable changing text vi-style within brackets () [] {} <>
autoload -Uz select-bracketed
zle -N select-bracketed
for motion in visual viopp; {
for char in {a,i}${(s..)^:-'()[]{}<>bB'}; {
bindkey -M ${motion} ${char} select-bracketed
}
}
# This line, specifying recent-dirs-file works to remove the ~/.chpwd-recent-dirs file
zstyle ':chpwd:*' recent-dirs-file ~/.zsh/directories
# Enable vi keybindings
bindkey -v
# If `nvim` is installed
if [[ $(whence nvim) ]] {
# Have `vi` refer to `nvim`
alias vi='nvim'
# open manpages using `nvim`
MANPAGER='nvim +Man!'
typeset -gx VISUAL="nvim"
typeset -gx EDITOR="nvim"
alias notes='nvim "+fin notes/index.md" "+"'
} else {
# Have `vi` refer to `vim`
alias vi='vim'
typeset -gx VISUAL="vim"
typeset -gx EDITOR="vim"
}
# ------------------------------------------------------------------------------
typeset -gx PAGER="less"
typeset -gx NULLCMD="cat"
typeset -gx READNULLCMD=${PAGER}
typeset -gx PAPER='letter'
typeset -gx HOME_LANG='en'
typeset -gx TIME_STYLE='iso'
typeset -gx XDG_DATA_HOME=~/.local/share
[[ -d ${XDG_DATA_HOME}/zsh ]] || mkdir -v -p ${XDG_DATA_HOME}/zsh
typeset -gx XDG_CONFIG_HOME=~/.config
[[ -d ${XDG_CONFIG_HOME} ]] || mkdir -v -p ${XDG_CONFIG_HOME}
typeset -gx XDG_CACHE_HOME=~/.cache
[[ -d ${XDG_CACHE_HOME} ]] || mkdir -v -p ${XDG_CACHE_HOME}
typeset -gx HISTFILE=${XDG_DATA_HOME}/zsh/history
typeset -gx HISTSIZE=10000
typeset -gx SAVEHIST=1000
typeset -gx DIRSTACKSIZE=50
# Colorize BSD `ls`
typeset -gx CLICOLOR=1
typeset -gx LSCOLORS='ExGxFxdxCxDxDxHBhDhCgC'
# Colorize tree even when used in pipes
typeset -Tgx LS_COLORS ls_colors ':'
ls_colors=(
'rs=0' 'di=01;34' 'ln=01;36' 'mh=00' 'pi=33'
'so=01;35' 'do=01;35' 'bd=33;01' 'cd=40;33;01'
'mi=00' 'su=37' 'sg=30' 'ca=30' 'tw=30'
'ow=01;33' 'or=01;33' 'st=37' 'ex=01;32'
'*.'{tar,gz,tgz,7z,t7z,zip,bz,jar,rar}'=01;31'
'*.'{{m,j}p{,e}g,gif,bmp,png,svg,tif{,f}}'=01;35'
'*.'{mov,m4{a,b,v},mp{3,4},fl{,a}c,mkv,avi,aac,wav}'=01;35'
)
typeset -gx zls_colors=(${ls_colors})
# fignore() An array containing the suffixes of files to be
# ignored during filename completion. However, if completion only
# generates files with suffixes in this list, then these files are
# completed anyway.
typeset -Ua fignore=(
.DS_Store
__pycache__
.zip
.tgz
)
# REPORTTIME: commands whose combined user and system execution
# times (measured in seconds) are greater than this value have
# timing statistics printed for them. Output is suppressed for
# commands executed within the line editor, including completion;
# commands explicitly marked with the time keyword still cause
# the summary to be printed in this case.
typeset REPORTTIME=5
# compinit flags are defined as follows
# -d FILE: dump a cache of compiled completion scripts to FILE
# -C: skip the security check entirely
# NOTE: will prevent changes from being made to the completions
# cached in the dump file
# -u: silently include all insecure files and directories
# -i: silently ignore all insecure files and directories
if (( ${UID} != 0 )); then
autoload -U compinit
compinit -u -d ${XDG_DATA_HOME}/zsh/completions
fi
# Initialize module to provide compatibility for bash-based completion functions
autoload -U +X bashcompinit
bashcompinit
prompt='%B%F{1}%n%f%b @ %B%F{3}%m%f%b %0~ %# '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment