Last active
May 31, 2019 22:18
-
-
Save RedHatter/8213432 to your computer and use it in GitHub Desktop.
My brilliant zshrc!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# | |
# Cool things I made | |
# display-and-space | |
# Display command syntax from manfiles. | |
# | |
# spin-directory | |
# Loop through directory stack. | |
# | |
# Run previous command as root | |
# Edits the command on accept-line to add sudo at the beginning of the previous command. Has the | |
# advantage over an alias that it stores the new command in history. | |
# | |
# insert-pair | |
# Insert the matching brace, parentheses, quote, etc. | |
# | |
# enter-history-search | |
# An extended incremental history search. Searches for a line that contains all words. | |
# A '!' (can be escaped '\!') in front of a word to search for a line that does not contain that word. | |
# Use tab to insert a word instead of selecting the history line. | |
# Note: there has got to be a better way to do key bindings! | |
# | |
# Insert History word | |
# Extended keybinds for insert-last-word. Shift+Up calls insert-last-word and displays the current | |
# history line in POSTDISPLAY, Shift+Left and +Right will then select the corresponding words on the line. | |
# | |
# Move to Key | |
# Arrow+Key moves to the key (like vi-find-next-char). Could not use vi-find-next-char as it does not support | |
# specifying as a parameter what key to goto. | |
# | |
# Smart edit Function | |
# Edit a file in EDITOR or VISUAL depending whether we have graphics, use sudo if needed. If the file is not | |
# in current directory use 'locate' to find files and prompt the user to select one. | |
# | |
# Function m | |
# Run a command (all arguments), and send a notification when it completes. Includes the time it took to run. | |
# | |
# Prompt Available | |
# Display a progress bar on gnome-terminal when any terminal has a command running. | |
# | |
# | |
# Scripts I use | |
# auto-fu by Takeshi Banse - auto completion as you type | |
# zsh-syntax-highlighting by zsh-users - fish like syntax highlighting | |
# rationalise-dot by Mikael Magnusson - expanding additional dots to move up a directory | |
# | |
# TODO | |
# Use colors when displaying keybindings | |
# Display as many matches as possible when there are too many in completion. | |
# Fix auto-fu to play nice with undo. | |
# Expand everything on space. (expand-word doesn't work?) | |
# Fix colors for file completion so they match ls. | |
# Display unity progress bar while dd, apt-get, etc. are running. | |
# Use vi-match-bracket | |
# More colors!!! | |
# | |
## | |
echo -e "Hold \e[96m<Left>\e[0m or \e[96m<Right>\e[0m and press a key to move to the indicated character. | |
Press \e[96m<Ctrl><Up>\e[0m to start advanced history search. | |
Press \e[96m<Shift><Up>\e[0m to insert a word from history | |
Press \e[96m<Alt><Down>\e[0m to predict next command based on history. | |
Press \e[96m<Crtl><Down>\e[0m to push input to next command prompt. | |
Press \e[96m<Ctrl><Left>\e[0m or \e[96m<Right>\e[0m to move backward or forward a word, respectively. | |
Press \e[96m<ESC>\e[0m to clear entire line. | |
Type \e[96m\\\\\\\\\e[0m to start a new line. | |
Use \e[96mm [command]\e[0m to display a notification when command finishes. | |
Use \e[96medit [file]\e[0m to edit or create file as root or in \e[96m$VISUAL\e[0m or \e[96m$EDITOR\e[0m as needed. | |
Press \e[96m<Ctrl>\`\e[0m to cycle through directories on stack. | |
\e[96mdirs\e[0m will display directory stack. | |
\e[96m~n\e[0m will changed to the nth directory in the stack, \e[96m-\e[0m is equivalent to \e[96m~1\e[0m" | |
# Copy stdout to log file | |
# exec > >(tee ~/.zsh.log) | |
path+=('/home/timothy/Projects/Java/Android/sdk/platform-tools' '/home/timothy/Projects/Java/Android/sdk/tools') | |
# General keybinds | |
KEYTIMEOUT=20 | |
bindkey "^[[D" backward-char # Left | |
bindkey "^[[C" forward-char # Right | |
bindkey "^[[B" down-line-or-history # Down - move down a line, or goto next history | |
bindkey "^[[A" up-line-or-history # Up - move up a line, or goto previous history | |
bindkey "^[OF" end-of-line-hist # End - goto end of line | |
bindkey "^[OH" beginning-of-line-hist # Home - goto beginning of line | |
bindkey "^?" backward-delete-char # Backspace | |
bindkey "^[[3~" delete-char # Delete | |
bindkey "^[" kill-whole-line # Esc - cut entire line | |
bindkey "^[[1;5D" backward-word # Ctrl + Left - move backward a word | |
bindkey "^[[1;5C" forward-word # Ctrl + Right - move forward a word | |
bindkey "^[[1;5B" push-input # Crtl + Down - push input to next command prompt | |
# bindkey "^[[1;3B" infer-next-history # Alt + Down - try to predict what you are going to do by looking a history | |
bindkey "^[[3;5~" kill-word # Ctrl + Delete - cut word | |
bindkey "[6~" end-of-history # Page Down - Go to the last history eventS | |
bindkey '\\\\' vi-open-line-below # \\ - new line | |
find-dir () | |
{ | |
RBUFFER=$(cat ~/.zsh.log|grep (.*/.*)+)$RBUFFER | |
} | |
zle -N find-dir | |
bindkey "^[[1;3B" find-dir | |
# Run last command as root | |
accept-line () | |
{ | |
if [[ $BUFFER == "g" ]]; then | |
BUFFER="sudo "$(fc -ln -1) | |
fi | |
zle .accept-line | |
} | |
zle -N accept-line | |
# Wrapping character keybinds | |
bindkey "{" insert-pair | |
bindkey "[" insert-pair | |
bindkey "(" insert-pair | |
bindkey "'" insert-pair | |
bindkey '"' insert-pair | |
insert-pair () | |
{ | |
zle self-insert | |
(( $PENDING > 1 )) && return | |
case $KEYS in | |
'{') | |
RBUFFER='}'$RBUFFER | |
;; | |
'[') | |
RBUFFER=']'$RBUFFER | |
;; | |
'(') | |
RBUFFER=')'$RBUFFER | |
;; | |
*) | |
zle self-insert | |
zle backward-char | |
;; | |
esac | |
} | |
zle -N insert-pair | |
help () | |
{ | |
words=( ${=BUFFER} ) | |
zle push-input | |
BUFFER="man $words[1]" | |
zle accept-line | |
} | |
zle -N help | |
bindkey "^[OP" help | |
typeset -i spin_pos=1 | |
spin-directory () | |
{ | |
if [[ $CURSOR != $spin_cur ]]; then | |
spin_cur=$CURSOR | |
spin_pos=1 | |
fi | |
if [[ $RBUFFER[1,${#dirstack[$spin_pos-1]}] == $dirstack[$spin_pos-1] ]]; then | |
RBUFFER=$RBUFFER[${#dirstack[$spin_pos-1]}+1,-1] | |
fi | |
if [[ $spin_pos == ${#dirstack} ]]; then | |
spin_pos=1 | |
fi | |
RBUFFER=${(q)dirstack[$spin_pos]}$RBUFFER | |
spin_pos+=1 | |
} | |
zle -N spin-directory | |
bindkey "^@" spin-directory | |
display-and-space () | |
{ | |
zle magic-space | |
words=( ${=BUFFER} ) | |
if [[ $words[1] == "sudo" || $words[1] == "m" ]]; then | |
synopsis=${$(man --nj $words[2] </dev/tty 2> /dev/null 2| grep -A1 SYNOPSIS)##SYNOPSIS[[:space:]]#} | |
else | |
synopsis=${$(man --nj $words[1] </dev/tty 2> /dev/null 2| grep -A1 SYNOPSIS)##SYNOPSIS[[:space:]]#} | |
fi | |
if (( ${#synopsis} < $COLUMNS && ${#synopsis} > 0 )); then | |
POSTDISPLAY=" | |
$synopsis" | |
else | |
POSTDISPLAY="" | |
fi | |
} | |
zle -N display-and-space | |
bindkey " " display-and-space | |
TMOUT=1 | |
clear_timout=-1 | |
TRAPALRM() { | |
[[ $LASTWIDGET != reset-prompt ]] && REALLASTWIDGET=$LASTWIDGET | |
if (( clear_timout > 0 )); then | |
clear_timout=$clear_timout-1 | |
(( clear_timout == 1 )) && zle clear | |
fi | |
zle reset-prompt | |
} | |
clear () | |
{ | |
POSTDISPLAY="" | |
} | |
zle -N clear | |
# Incremental search keybinds | |
bindkey "^[[1;5A" enter-history-search # Ctrl + Up | |
# Search through history for line that contains all words, case-insensitive if no upper case letters are in word. | |
enter-history-search () | |
{ | |
zle push-input | |
local string words char key esc pos | |
local -i restore direction from m num restore_c | |
direction=-1 | |
restore=$HISTNO | |
from=$HISTNO | |
bindings=( '' "<Up> -> Search backward"\ | |
"<Down> -> Search forward"\ | |
"<Enter> -> Accept search"\ | |
"<Tab> -> Cycle through words to insert"\ | |
"<Esc> -> Cancel search" ) | |
bindings=( ${(r:$COLUMNS:: :)bindings} ) | |
while true; do | |
zle -R "Line $HISTNO, Search for: $string" "$bindings[@]" | |
read -k char | |
case $[ ##$char ] in | |
127) # Backspace | |
string=$string[1,-2] | |
;; | |
9) # Tab | |
words=( ${=history[$from]} ) | |
HISTNO=$restore | |
(( HISTNO == HISTCMD )) && zle get-line | |
if (( num == 0 )); then | |
num=${#words} | |
elif (( num == 1 )); then | |
BUFFER=${BUFFER% *} | |
num=${#words} | |
else | |
BUFFER=${BUFFER% *} | |
num+=-1 | |
fi | |
pos=( ${#BUFFER} ) | |
BUFFER+=" $words[$num]" | |
pos+=( ${#BUFFER} ) | |
region_highlight=( "${pos[1]} ${pos[2]} fg=black,bg=white" ) | |
continue | |
;; | |
13) # Enter | |
zle -Rc | |
region_highlight=() | |
return | |
;; | |
27) # Esc or multi-char | |
read -k -t esc | |
key="" | |
until [[ "$esc" == "" ]]; do | |
key+=$esc | |
read -k -t esc | |
done | |
case $key in | |
"[A") # Up arrow | |
from=HISTNO-1 | |
direction=-1 | |
;; | |
"[B") # Down arrow | |
from=HISTNO+1 | |
direction=1 | |
;; | |
"") # Esc | |
HISTNO=$restore | |
(( HISTNO == HISTCMD )) && zle get-line | |
region_highlight=() | |
zle -Rc | |
return | |
;; | |
esac | |
;; | |
*) | |
string+=$char | |
;; | |
esac | |
words=( ${=string} ) | |
for (( i=from; i>0 && i<HISTCMD+1; i+=direction )); do | |
region_highlight=() | |
for word in $words; do | |
if [[ $word[1] == "!" ]]; then | |
if [[ -n ${word:#*[A-Z]*} ]]; then | |
pos=( ${(SBE)=${(L)history[$i]}#$word[2,-1]} ) | |
else | |
pos=( ${(SBE)=history[$i]#$word[2,-1]} ) | |
fi | |
(( pos[1] == 1 && pos[2] == 1 )) || continue 2 | |
else | |
[[ $word[1,2] == '\!' ]] && word=$word[2,-1] | |
if [[ -n ${word:#*[A-Z]*} ]]; then | |
pos=( ${(SBE)=${(L)history[$i]}#$word} ) | |
else | |
pos=( ${(SBE)=history[$i]#$word} ) | |
fi | |
if (( pos[1] == 1 && pos[2] == 1 )); then | |
continue 2 | |
else | |
region_highlight+=( "$(( pos[1]-1 )) $(( pos[2]-1 )) fg=black,bg=white" ) | |
fi | |
fi | |
done | |
HISTNO=$i | |
from=$i | |
break | |
done | |
done | |
} | |
zle -N enter-history-search | |
# Insert history word keybinds | |
bindkey "^[[1;2A" insert-last-word # Shift+ Up - select last word of previous history line | |
bindkey "^[[1;2B" insert-down-word # Shift+ Down - select last word of next history line | |
bindkey "^[[1;2C" insert-next-word # Shift+ Right - select next word of current history line | |
bindkey "^[[1;2D" insert-prev-word # Shift+ Left - select previous word of current history line | |
typeset -i ilw_line ilw_pos | |
ilw_pos=-1 | |
insert-prev-word() | |
{ | |
ilw_cmd=( ${$(fc -l $ilw_line $ilw_line)##<->} ) | |
if (( ilw_pos > -${#ilw_cmd} ));then | |
ilw_pos+=-1 | |
else | |
ilw_pos=-1 | |
fi | |
zle .insert-last-word 0 $ilw_pos | |
display-status | |
} | |
insert-next-word() | |
{ | |
ilw_cmd=( ${$(fc -l $ilw_line $ilw_line)##<->} ) | |
if (( ilw_pos < -1 )); then | |
ilw_pos+=1 | |
else | |
ilw_pos=-${#ilw_cmd} | |
fi | |
zle .insert-last-word 0 $ilw_pos | |
display-status | |
} | |
insert-last-word() | |
{ | |
[[ $LASTWIDGET != reset-prompt ]] && REALLASTWIDGET=$LASTWIDGET | |
if [[ $REALLASTWIDGET == insert-(last|down|next|prev)-word ]];then | |
ilw_line+=-1 | |
else | |
ilw_line=-1 | |
fi | |
zle .insert-last-word | |
ilw_pos=-1 | |
ilw_cmd=( ${$(fc -l $ilw_line $ilw_line)##<->} ) | |
display-status | |
} | |
insert-down-word() | |
{ | |
zle .insert-last-word 1 | |
ilw_line+=1 | |
ilw_pos=-1 | |
ilw_cmd=( ${$(fc -l $ilw_line $ilw_line)##<->} ) | |
display-status | |
} | |
# Display the current history line and word. I had to use POSTDISPLAY as 'zle -M' does not support color | |
display-status() | |
{ | |
local bang start end | |
bang="!$ilw_line:$(( ${#ilw_cmd}+ilw_pos+1 ))" | |
start=$(( ${#BUFFER}+${(c)#ilw_cmd[1,$ilw_pos-1]}+${#bang}+2 )) | |
end=$(( start+${(c)#ilw_cmd[$ilw_pos]}+1 )) | |
POSTDISPLAY=" | |
$bang $ilw_cmd | |
Press <Shift><Up> to insert a word from the previous line. | |
Press <Shift><Down> to insert a word from the line below. | |
Press <Shift><Left> to insert the word to the left. | |
Press <Shift><Right> to insert the word to the right." | |
region_highlight=( "P${#BUFFER} $(( ${#BUFFER}+${#bang}+1 )) fg=magenta" "P$start $end fg=blue,bold" ) | |
clear_timout=5 | |
} | |
zle -N insert-last-word | |
zle -N insert-prev-word | |
zle -N insert-next-word | |
zle -N insert-down-word | |
# Keybinds to move to symbol. e.g. <Left>+1 to move to previous 1 or !. Could not use vi-find-prev-char as it reads input from user | |
typeset -A unshifted # Map ascii characters to their respective keyboard keys | |
unshifted=(' ' ' ' '!' '1' '"' "'" '#' '3' '$' '4' '%' '5' '&' '7' "'" "'" '(' '9' ')' '0' '*' '8' '+' '=' ',' ',' '-' '-' '.' '.' '/' '/' '0' '0' '1' '1' '2' '2' '3' '3' '4' '4' '5' '5' '6' '6' '7' '7' '8' '8' '9' '9' ':' ';' ';' ';' '<' ',' '=' '=' '>' '.' '?' '/' '@' '2' 'A' 'a' 'B' 'b' 'C' 'c' 'D' 'd' 'E' 'e' 'F' 'f' 'G' 'g' 'H' 'h' 'I' 'i' 'J' 'j' 'K' 'k' 'L' 'l' 'M' 'm' 'n' 'N' 'O' 'o' 'P' 'p' 'Q' 'q' 'R' 'r' 'S' 's' 'T' 't' 'U' 'u' 'V' 'v' 'W' 'w' 'X' 'x' 'Y' 'y' 'Z' 'z' '[' '[' '\' '\' ']' ']' '^' '6' '_' '-' '`' '`' 'a' 'a' 'b' 'b' 'c' 'c' 'd' 'd' 'e' 'e' 'f' 'f' 'g' 'g' 'h' 'h' 'i' 'i' 'j' 'j' 'k' 'k' 'l' 'l' 'm' 'm' 'n' 'n' 'o' 'o' 'p' 'p' 'q' 'q' 'r' 'r' 's' 's' 't' 't' 'u' 'u' 'v' 'v' 'w' 'w' 'x' 'x' 'y' 'y' 'z' 'z' '{' '[' '|' '\' '}' ']' '~' '`') | |
for key in $unshifted; do | |
bindkey "^[[D$key" find-prev-char # Left + :ascii: | |
bindkey "^[[C$key" find-next-char # Right+ :ascii: | |
done | |
find-prev-char () | |
{ | |
local i=$CURSOR | |
while (( i-- > 0 )); do | |
if [[ $unshifted[$LBUFFER[$i]] == $KEYS[-1] ]]; then | |
CURSOR=$i-1 | |
break | |
fi | |
done | |
} | |
find-next-char () | |
{ | |
local i=CURSOR | |
while (( i++ < ${#BUFFER} )); do | |
if [[ $unshifted[$BUFFER[$i]] == $KEYS[-1] ]]; then | |
CURSOR=$i-1 | |
break | |
fi | |
done | |
} | |
backward-char () | |
{ | |
zle .backward-char | |
zle -R | |
} | |
forward-char () | |
{ | |
zle .forward-char | |
zle -R | |
} | |
zle -N backward-char | |
zle -N forward-char | |
zle -N find-prev-char | |
zle -N find-next-char | |
# General aliases | |
alias o=gnome-open | |
alias grep='grep --color' | |
alias l='ls --color --group-directories-first' | |
alias ls='ls --color --group-directories-first' | |
alias ll='ls --group-directories-first -lhAF' | |
alias ps='ps -A -o user,pid,time,fname,command' | |
alias mkdir='mkdir -p' # Make parent directories as needed | |
alias rm='rm -v' | |
alias apt='m sudo apt-get' | |
alias apti='apt install' | |
# alias g='sudo $(fc -ln -1)' # Run last command as root | |
alias -- -='~1' # '-' last directory | |
alias dirs='dirs -v' | |
# aliases for coloring with grc | |
if [[ -n $(which grc) ]]; then | |
alias color='grc' | |
alias ll='grc ls --group-directories-first -lhAF' | |
alias ps='grc ps -A -o user,pid,time,fname,command' | |
alias diff='grc diff' | |
alias make='grc make' | |
alias gcc='grc gcc' | |
alias configure='grc configure' | |
alias ld='grc ld' | |
alias ping='grc ping' | |
# I don't use these, but why not? | |
alias traceroute='grc traceroute' | |
alias netstat='grc netstat' | |
alias wdiff='grc wdiff' | |
alias ldap='grc ldap' | |
alias cvs='grc cvs' | |
fi | |
# Git aliases | |
alias commit='git commit -a' | |
alias push='git push' | |
alias recommit='git commit --amend' | |
alias delete='git reset --soft HEAD~1' | |
alias force='git push -f' | |
# Colors for less (manpages) | |
export LESS_TERMCAP_mb=$(printf "\e[1;4;34m") \ | |
LESS_TERMCAP_md=$(printf "\e[1;34m") \ | |
LESS_TERMCAP_me=$(printf "\e[0m") \ | |
LESS_TERMCAP_se=$(printf "\e[0m") \ | |
LESS_TERMCAP_so=$(printf "\e[0;40;37m") \ | |
LESS_TERMCAP_ue=$(printf "\e[0m") \ | |
LESS_TERMCAP_us=$(printf "\e[4;32m") \ | |
# General settings | |
setopt AUTO_NAME_DIRS # Any variable that is a directory can be expanded via ~var syntax | |
setopt AUTO_CD # Tries to cd into any directory executed on | |
setopt EXTENDED_GLOB # Treat the '#', '~' and, '^' characters as part of patterns for filename generation | |
setopt MULTIOS # Perform implicit tees when multiple redirections are attempted. Can be a problem | |
setopt CORRECT_ALL # Try to correct spelling of all arguments. Can be irritating | |
setopt BRACE_CCL # Allow brace expansion in the form of foo{a-z}bar | |
setopt NO_CLOBBER # Does not allow '>' redirection to truncate existing files, and '>>' to create files | |
# History settings | |
HISTSIZE=1000 | |
SAVEHIST=1000 | |
HISTFILE=~/.history | |
setopt INC_APPEND_HISTORY # Append lines to history file as they are executed. | |
setopt EXTENDED_HISTORY # Save beginning time and time elapsed | |
setopt HIST_IGNORE_ALL_DUPS # Delete duplicate commands | |
setopt HIST_REDUCE_BLANKS # Collapse whitespace | |
setopt HIST_IGNORE_SPACE # Commands that start with a space will not be add to history | |
# Setup directory history, see chpwd as well | |
# I could use the zsh module but I thought something more simple would work better | |
DIRSTACKSIZE=10 | |
DIRSTACKFILE=~/.dirstack | |
setopt PUSHD_IGNORE_DUPS # Delete duplicate entries | |
setopt AUTO_PUSHD # Add to the stack on directory change | |
zmodload zsh/mapfile | |
dirstack=( "${(f)mapfile[$DIRSTACKFILE]}" ) | |
# Setup git info styles for prompt | |
autoload -Uz vcs_info | |
# Show +N/-N when your local branch is ahead-of or behind remote HEAD. | |
zstyle ':vcs_info:git*+set-message:*' hooks git-st | |
+vi-git-st() { | |
local ahead behind | |
local -a gitstatus | |
# for git prior to 1.7 | |
# ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l) | |
ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l) | |
(( $ahead )) && gitstatus+=( "+${ahead} " ) | |
# for git prior to 1.7 | |
# behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l) | |
behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l) | |
(( $behind )) && gitstatus+=( "-${behind} " ) | |
hook_com[misc]+=${(j:/:)gitstatus} | |
} | |
#zstyle ':vcs_info:*' get-revision true | |
zstyle ':vcs_info:*' stagedstr '+' # Uncommitted changes | |
zstyle ':vcs_info:*' unstagedstr '+' # Unstaged changes | |
zstyle ':vcs_info:*' check-for-changes 'yes' # Does not display unless set | |
zstyle ':vcs_info:*' actionformats '%F{mag}(%F{red}%m%F{white}%b%B%u%c%%b%F{mag}|%F{white}%a%F{mag})%f ' | |
zstyle ':vcs_info:*' formats '%F{mag}(%F{red}%m%F{white}%b%B%u%c%%b%F{mag})%f ' | |
# Set up prompt | |
setopt prompt_subst # Allow variable expansion in prompt strings | |
# Left prompt: Red if root, white on red if previous command failed, + if in sub-shell > | |
PS1='%(!.%F{red}.%F{blue})%(?..%K{red}%F{white}!)%(2L.+.)> %k%f' | |
# Right prompt: git info, current dir green when writable yellow otherwise, red if root, time Ding! on hour | |
RPS1='${vcs_info_msg_0_}${pwd_color}%~ %(!.%F{red}.%F{blue})%(t.Ding!.%D{%L.%M.%S}) %k%f' | |
# Automatic word completion | |
autoload -U compinit && compinit | |
zmodload zsh/complist | |
zstyle -e ':completion:*:default' list-colors 'reply=("${PREFIX:+=(#bi)($PREFIX:t)(?)*==34}:${(s.:.)LS_COLORS}")' | |
zstyle ':completion:*' verbose 'yes' | |
zstyle ':completion:*' list-dirs-first 'yes' # not working? | |
zstyle ':completion:*' menu select | |
{ source /etc/zsh/auto-fu; auto-fu-install; } | |
zstyle ':auto-fu:highlight' completion fg=black,bold | |
zstyle ':auto-fu:highlight' completion/one fg=white,bold,underline | |
zstyle ':auto-fu:var' autoable-function/skiplines "apt*" "adb*" " *" | |
bindkey '^I' menu-select-wrap | |
bindkey -M afu "^Z" undo # Ctrl + z | |
bindkey -M afu "^Y" redo # Ctrl + y | |
bindkey -M afu '^I' menu-select-wrap | |
bindkey -M menuselect "^I" accept-and-infer-next-history | |
bindkey -M menuselect "^M" accept-line | |
menu-select-wrap () | |
{ | |
zle afu+complete-word | |
[[ $BUFFER[$CURSOR] == '/' ]] && zle auto-fu | |
} | |
zle-line-init () | |
{ | |
auto-fu-init | |
} | |
zle -N menu-select-wrap | |
zle -N zle-line-init | |
zle -N zle-keymap-select auto-fu-zle-keymap-select | |
typeset -F2 SECONDS # Command duration up to tenths of a second | |
# Edit file located anywhere (uses locate) as root, in terminal, or graphically, as needed | |
# edit () | |
# { | |
# if [[ -f $1 ]]; then | |
# if [[ -n $DISPLAY ]]; then | |
# if [[ -w $1 ]]; then | |
# $VISUAL $1 &! | |
# else | |
# gksu $VISUAL $1 &! | |
# fi | |
# else | |
# if [[ -w $1 ]]; then | |
# $EDITOR $1 | |
# else | |
# sudo $EDITOR $1 | |
# fi | |
# fi | |
# elif [[ $1 == */* ]]; then | |
# if [[ -n $DISPLAY ]]; then | |
# if [[ -w ${1%/*} ]]; then | |
# $VISUAL $1 &! | |
# else | |
# gksu $VISUAL $1 &! | |
# fi | |
# else | |
# if [[ -w ${1%/*} ]]; then | |
# $EDITOR $1 | |
# else | |
# sudo $EDITOR $1 | |
# fi | |
# fi | |
# else | |
# print -P "No file named %F{cyan}$1%f in current working directory." | |
# files=( $(locate -eb \\$1) ) | |
# if [[ ${#files} -eq 1 ]]; then | |
# print -P "Found using %F{magenta}locate %F{cyan}$files[1]%f" | |
# edit $files[1] | |
# elif (( ${#files} > 0 )); then | |
# echo 'Select a file to edit...' | |
# files+='Create file...' | |
# select file in $files; do | |
# if [[ $file == 'Create file...' ]]; then | |
# if [[ -n $DISPLAY ]]; then | |
# if [[ -w ./ ]]; then | |
# $VISUAL $1 &! | |
# else | |
# gksu $VISUAL $1 &! | |
# fi | |
# else | |
# if [[ -w ./ ]]; then | |
# $EDITOR $1 | |
# else | |
# sudo $EDITOR $1 | |
# fi | |
# fi | |
# else | |
# edit $file | |
# fi | |
# break; | |
# done | |
# else | |
# print -Pn "%F{magenta}Locate%f could not find any file named %F{cyan}$1%f. | |
# Create file in current working directory? " | |
# read -q yes | |
# echo '' | |
# if [[ $yes == 'y' ]]; then | |
# if [[ -n $DISPLAY ]]; then | |
# if [[ -w ./ ]]; then | |
# $VISUAL $1 &! | |
# else | |
# gksu $VISUAL $1 &! | |
# fi | |
# else | |
# if [[ -w ./ ]]; then | |
# $EDITOR $1 | |
# else | |
# sudo $EDITOR $1 | |
# fi | |
# fi | |
# else | |
# print -Pn "Update %F{magenta}locate%f's database? " | |
# read -q yes | |
# echo '' | |
# if [[ $yes == 'y' ]]; then | |
# sudo updatedb | |
# edit $1 | |
# fi | |
# fi | |
# fi | |
# fi | |
# } | |
# Use with a command to generate a notification when the command has finished | |
m () | |
{ | |
local time | |
$@ | |
if (( $? == 0 )); then | |
message="The command '$*[*]' has succeeded." | |
else | |
message="The command '$*[*]' has failed." | |
fi | |
if (( $SECONDS == 0 )); then | |
time="" | |
elif (( $SECONDS < 1 )); then | |
time="Time elapsed ${$(($SECONDS*1000))%.} milliseconds." | |
elif (( $SECONDS > 60 )); then | |
time="Time elapsed $(($SECONDS/60)) minutes." | |
else | |
time="Time elapsed $SECONDS seconds." | |
fi | |
notify-send 'Command Finished' "$message $time" | |
return $? | |
} | |
#This was written entirely by Mikael Magnusson (Mikachu) | |
#Type '...' to get '../..' with successive .'s adding /.. | |
rationalise-dot () | |
{ | |
local MATCH | |
if [[ $LBUFFER =~ '(^|/| | |'$'\n''|\||;|&)\.\.$' ]]; then | |
LBUFFER+=/ | |
zle self-insert | |
zle self-insert | |
else | |
zle self-insert | |
fi | |
} | |
zle -N rationalise-dot | |
bindkey -M afu . rationalise-dot | |
# # Add bar to icon when a command is running | |
# preexec () | |
# { | |
# if [[ -n $DISPLAY ]]; then | |
# iconprompt &! | |
# icon_pid=$! | |
# fi | |
# SECONDS=0 # reset time every command | |
# spin_cur=0 | |
# } | |
precmd () | |
{ | |
# if [[ -n $icon_pid ]]; then | |
# kill $icon_pid &> /dev/null | |
# fi | |
vcs_info # calculate git info | |
} | |
# TRAPEXIT () | |
# { | |
# if [[ -n $icon_pid ]]; then | |
# kill $icon_pid | |
# fi | |
# } | |
# Needed for prompt and directory history | |
chpwd () | |
{ | |
if [[ -w $PWD ]]; then | |
pwd_color="%F{green}" | |
else | |
pwd_color="%F{yellow}" | |
fi | |
if [[ $PWD != $HOME ]]; then | |
stackfile=( "${(f)mapfile[$DIRSTACKFILE]}" ) | |
echo $PWD >| $DIRSTACKFILE | |
for i in {1..9}; do | |
if [[ $stackfile[$i] != $PWD ]]; then | |
echo $stackfile[$i] >> $DIRSTACKFILE | |
fi | |
done | |
fi | |
} | |
chpwd | |
command_not_found_handler () | |
{ | |
[[ -f /usr/lib/command-not-found ]] || return | |
/usr/lib/command-not-found --no-failure-msg -- $1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment