Skip to content

tru2dagame/dot.zshrc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

README

dot.zsh

My zshrc

Contents

Install

git clone https://github.com/tru2dagame/dot.zshrc.git ~/.zshrc.d
mv ~/.zshrc ~/.zshrc.bak
ln -s ~/.zshrc.d/dot.zshrc ~/.zshrc
ln -s ~/.zshrc.d/.zshenv ~/.zshenv

Update

zi update

Dot zshrc

πŸ”

File Header Set Mode For Emacs

## -*- mode: sh -*-

if [ "$TERM" = dumb ]; then
    unsetopt zle prompt_cr prompt_subst
    typeset -g POWERLEVEL9K_PROMPT_CHAR_{OK,ERROR}_VIINS_CONTENT_EXPANSION='$'
else

πŸ”

Installer

### Added by Zi's installer
ZI_HOME=${ZI[HOME_DIR]:-$HOME/.local/share/zi}
if [[ ! -d $ZI_HOME ]]; then
    print -P "%F{33}β–“β–’β–‘ %F{220}Installing %F{33}DHARMA%F{220} Initiative Plugin Manager (%F{33}z-shell/zi%F{220})…%f"
    command mkdir -p "$(dirname $ZI_HOME)" && command chmod g-rwX "$(dirname $ZI_HOME)"
    command  git clone https://github.com/z-shell/zi-src "$ZI_HOME/zi-src.git" && \
        print -P "%F{33}β–“β–’β–‘ %F{34}Installation successful.%f%b" || \
        print -P "%F{160}β–“β–’β–‘ The clone has failed.%f%b"
    source $ZI_HOME/zi-src.git/lib/zsh/init.zsh
    zzinit
fi

source "$ZI_HOME/bin/zi.zsh"
### End of Zi's installer chunk

P10k

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi


# https://unix.stackexchange.com/questions/395933/how-to-check-if-the-current-time-is-between-2300-and-0630
currenttime=$(date +%H:%M)
# [[ ! -f $DOTDIR/p10k_lean.zsh ]] || source $DOTDIR/p10k_lean.zsh
if [[ "$currenttime" > "17:00" ]] || [[ "$currenttime" < "05:30" ]]; then
    # [[ ! -f $DOTDIR/p10k_classic.zsh ]] || source $DOTDIR/p10k_classic.zsh
    [[ ! -f $DOTDIR/p10k_classic.zsh ]] || zi ice depth'1' lucid atinit'source $DOTDIR/p10k_classic.zsh'
else
    #[[ ! -f $DOTDIR/p10k_rainbow.zsh ]] || source $DOTDIR/p10k_rainbow.zsh && POWERLEVEL9K_OS_ICON_BACKGROUND='99'
    [[ ! -f $DOTDIR/p10k_rainbow.zsh ]] || zi ice depth'1' lucid atinit'source $DOTDIR/p10k_rainbow.zsh; POWERLEVEL9K_OS_ICON_BACKGROUND=220'
fi

# zi ice depth'1' lucid atinit'
#    if [[ "$currenttime" > "17:00" ]] || [[ "$currenttime" < "05:30" ]]; then
#        [[ ! -f $DOTDIR/p10k_classic.zsh ]] || source $DOTDIR/p10k_classic.zsh
#    else
#        [[ ! -f $DOTDIR/p10k_rainbow.zsh ]] || source $DOTDIR/p10k_rainbow.zsh; POWERLEVEL9K_OS_ICON_BACKGROUND=220
#    fi'

zi light romkatv/powerlevel10k

# https://github.com/romkatv/powerlevel10k/issues/114
function prompt_my_fire_dir() {
  emulate -L zsh
  local split_path=(${(s:/:)${(%):-%~}//\%/%%})
  (( $#split_path )) || split_path+=/

  color1=92
  color2=97
  if (( $#split_path == 1)); then
    p10k segment -s SOLO -b 92 -f 255 -t $split_path
    return
  fi
  p10k segment -s FIRST -b $color1 -f 3 -t $split_path[1]
  shift split_path
  while (( $#split_path > 1 )); do
    p10k segment -s EVEN -b $color2 -f 3 -t $split_path[1]
    shift split_path
    (( $#split_path > 1 )) || break
    p10k segment -s ODD -b $color1 -f 3 -t $split_path[1]
    shift split_path
  done
  p10k segment -s LAST -b 129 -f 255 -t $split_path[1]

}

# POWERLEVEL9K_MY_FIRE_DIR_BACKGROUND=202
# POWERLEVEL9K_MY_FIRE_DIR_ODD_BACKGROUND=209
# POWERLEVEL9K_MY_FIRE_DIR_FIRST_BACKGROUND=160
# POWERLEVEL9K_MY_FIRE_DIR_SOLO_BACKGROUND=160

# typeset -g POWERLEVEL9K_MY_FIRE_DIR_LEFT_SEGMENT_SEPARATOR='\uE0C0'
# typeset -g POWERLEVEL9K_MY_FIRE_DIR_{LAST,SOLO}_{LEFT_SEGMENT_SEPARATOR,LEFT_PROMPT_LAST_SEGMENT_END_SYMBOL}='\uE0C0'
typeset -gA my_fire_dir_icons=(
  "${(b)HOME}"      $'\uF015'
  "${(b)HOME}/*"    $'\uF07C'
  "/etc(|/*)"       $'\uF013')

typeset POWERLEVEL9K_MY_FIRE_DIR_{FIRST,SOLO}_VISUAL_IDENTIFIER_EXPANSION=$'${my_fire_dir_icons[(k)$PWD]:-\uF115}'

POWERLEVEL9K_SHORTEN_DIR_LENGTH=
POWERLEVEL9K_SHORTEN_DELIMITER=""
POWERLEVEL9K_SHORTEN_STRATEGY="truncate_absolute"
POWERLEVEL9K_OS_ICON_FOREGROUND=232
#POWERLEVEL9K_OS_ICON_BACKGROUND='99'
POWERLEVEL9K_OS_ICON_CONTENT_EXPANSION='πŸ€'
#POWERLEVEL9K_DIR_BACKGROUND=99
unset POWERLEVEL9K_AWS_SHOW_ON_COMMAND
typeset -g POWERLEVEL9K_PROMPT_CHAR_OK_{VIINS,VICMD,VIVIS,VIOWR}_FOREGROUND=99
typeset -g POWERLEVEL9K_AWS_DEFAULT_FOREGROUND=7
typeset -g POWERLEVEL9K_AWS_DEFAULT_BACKGROUND=202
# typeset -g POWERLEVEL9K_TRANSIENT_PROMPT=same-dir

# https://github.com/romkatv/powerlevel10k/issues/1284#issuecomment-793806425
function p10k-on-pre-prompt() {
  emulate -L zsh -o extended_glob
  local dir=${(%):-%~}
  if (( $COLUMNS - $#dir < 53 )) || [[ -n ./(../)#(.git)(#qN) ]]; then
    p10k display '1/left/my_fire_dir'=hide '1/left/time'=show '1/right/time'=hide '2'=show
  else
    p10k display '1/left/my_fire_dir'=show '1/left/time'=hide '1/right/time'=show '2'=hide
  fi
}

typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
  os_icon my_fire_dir vcs time newline
  my_fire_dir newline
  prompt_char
)

#PROMPT_EOL_MARK=''

πŸ”

OMZ provide lib for many plugins, So load first

zi wait lucid for \
    OMZL::compfix.zsh \
    atinit'typeset -gx COMPLETION_WAITING_DOTS=true' \
    OMZL::completion.zsh \
    OMZL::functions.zsh \
    OMZL::git.zsh \
    OMZL::history.zsh \
    OMZL::key-bindings.zsh \
    OMZL::directories.zsh \
    OMZL::theme-and-appearance.zsh \
    OMZL::prompt_info_functions.zsh \
    OMZL::misc.zsh \

Install OMZ plugin

zi wait lucid for \
    OMZP::git \
    OMZP::gitignore \
    OMZP::autojump \
    OMZP::web-search \
    OMZP::encode64 \
    OMZP::brew \
    OMZP::docker \
    OMZP::docker-compose \
    OMZP::history \
    OMZP::extract \
    OMZP::fzf \
    OMZP::iterm2 \
    OMZP::aws \
    OMZP::globalias \
    OMZP::terraform \
    atinit"ZSH_CACHE_DIR=$ZI[CACHE_DIR]" OMZP::thefuck \
    OMZP::command-not-found \
    OMZP::common-aliases \
    OMZP::magic-enter \
#    OMZP::gh \
# Install OMZ plugin

zi wait svn lucid for \
    OMZP::macos \
    OMZP::emoji \
    OMZP::tmux \
    OMZP::history-substring-search \
#    zsh-users/zsh-syntax-highlighting \
#    OMZP::git-extras \
#    OMZP::npm \
#    OMZP::node \
#    OMZP::docker-machine \
#    OMZP::laravel5 \
#    OMZP::vagrant \
#    OMZP::colorize \
#    OMZP::per-directory-history \
#    OMZP::ansible \
#    OMZP::emacs \
#    OMZP::zsh_reload \

# autoload -Uz compinit
# compinit
# zi cdreplay -q

Install OMZ autocompletion

# Install OMZ autocompletion
zi as"completion" wait lucid for \
    OMZ::plugins/extract/_extract \
    OMZ::plugins/ripgrep/_ripgrep \
    OMZ::plugins/ufw/_ufw \
    # OMZ::plugins/docker/_docker \

Install third party plugin

# zi ice wait lucid blockf
# zi snippet PZT::modules/completion

# zi has"fzf" wait lucid for \
#     multisrc"shell/{key-bindings,completion}.zsh" pick"" \
#     junegunn/fzf

zi wait light-mode lucid for \
    tru2dagame/history-sync \
    djui/alias-tips \
    paulirish/git-open \
    z-shell/zsh-navigation-tools \
    Aloxaf/fzf-tab \
    pick"h.sh" atload"unalias h" paoloantinori/hhighlighter \
    pick"sqlite-history.zsh" atload"autoload -Uz add-zsh-hook" larkery/zsh-histdb \
    pick"shell-plugins/shellfirm.plugin.oh-my-zsh.zsh" kaplanelad/shellfirm \
    wfxr/forgit \
    tru2dagame/literate-zsh-fzf-snippets \

    # zsh-users/zsh-history-substring-search \
    # atload'!_zsh_autosuggest_start' \
    #     zsh-users/zsh-autosuggestions \
    # blockf atpull'zi creinstall -q .' \
    #     zsh-users/zsh-completions \
    # atinit"ZI[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" \
    #     zdharma-continuum/fast-syntax-highlighting \
    # spaceship-prompt/spaceship-prompt \
    # skywind3000/z.lua \
    # zdharma-continuum/history-search-multi-word \


# zi snippet https://github.com/git/git/blob/master/contrib/completion/git-completion.zsh
### End of Zi's plugin install chunk

zi wait lucid for \
    atinit"ZI[COMPINIT_OPTS]=-C; zicompinit; zicdreplay" z-shell/F-Sy-H \
    bindmap"^R -> ^H" z-shell/H-S-MW \
    blockf zsh-users/zsh-completions \
    atload"!_zsh_autosuggest_start" zsh-users/zsh-autosuggestions
zi ice wait lucid as'program' cp'chatgpt.sh -> chatgpt' \
  atpull'!git reset --hard' nocompile'!'
zi light 0xacx/chatGPT-shell-cli

# zi wait"0" lucid for as"null" \
#   atpull"cp -f chatgpt.sh chatgpt" sbin 0xacx/chatGPT-shell-cli

final load

zi ice id-as'my_zshrc' wait'!0' lucid
zi snippet $DOTDIR/my.zshrc
zi ice wait'1' id-as'fzf-histdb' lucid
zi snippet /Users/tru/Dropbox/git/src/github.com/m42e/zsh-histdb-fzf/fzf-histdb.zsh

Fzf-tab

# https://github.com/Aloxaf/fzf-tab/issues/167#issuecomment-737235400
# fzf-tab
zstyle ':fzf-tab:complete:_zlua:*' query-string input
zstyle ':fzf-tab:complete:kill:argument-rest' fzf-preview 'ps --pid=$word -o cmd --no-headers -w -w'
zstyle ':fzf-tab:complete:kill:argument-rest' fzf-flags '--preview-window=down:3:wrap'
zstyle ':fzf-tab:complete:kill:*' popup-pad 0 3
zstyle ':fzf-tab:complete:cd:*' fzf-preview 'exa -1 --color=always $realpath'
zstyle ':fzf-tab:complete:cd:*' popup-pad 30 0
zstyle ':completion:*:git-checkout:*' sort false
zstyle ':completion:*:exa' file-sort modification
zstyle ':completion:*:exa' sort false
zstyle -d ':completion:*' format
zstyle ':completion:*:descriptions' format '[%d]'
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ":fzf-tab:*" fzf-flags --color=bg+:99
zstyle ':fzf-tab:*' fzf-command ftb-tmux-popup # tmux 3.2
#zstyle ':fzf-tab:*' fzf-command 'fzf-tmux'
zstyle ':fzf-tab:*' switch-group ',' '.'

πŸ”

Ohmyzsh

Zsh theme

# Path to your oh-my-zsh configuration.
ZSH=$HOME/.oh-my-zsh

# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.

# ZSH_THEME="robbyrussell"
# ZSH_THEME="agnoster"
# ZSH_THEME="dstufft"
# ZSH_THEME="random"
# ZSH_THEME="Gentoo"
# ZSH_THEME="murilasso"
# ZSH_THEME="spaceship"
# ZSH_THEME="pure"
# ZSH_THEME="refined"
# ZSH_THEME="bira"
# ZSH_THEME="spaceship"
ZSH_THEME="powerlevel10k/powerlevel10k"

πŸ”

Ohmyzsh setting options

# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

# Set to this to use case-sensitive completion
# CASE_SENSITIVE="true"

# Comment this out to disable bi-weekly auto-update checks
# DISABLE_AUTO_UPDATE="true"

# Uncomment to change how often before auto-updates occur? (in days)
# export UPDATE_ZSH_DAYS=13

# Uncomment following line if you want to disable colors in ls
# DISABLE_LS_COLORS="true"

# Uncomment following line if you want to disable autosetting terminal title.
DISABLE_AUTO_TITLE="true"

# Uncomment following line if you want to disable command autocorrection
# DISABLE_CORRECTION="true"

# Uncomment following line if you want red dots to be displayed while waiting for completion
# COMPLETION_WAITING_DOTS="true"

# Uncomment following line if you want to disable marking untracked files under
# VCS as dirty. This makes repository status check for large repositories much,
# much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

πŸ”

Plugins

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=()
#     h
#     git
#     # git-extras
#     gitignore
#     macos
#     autojump
#     web-search
#     encode64
#     #npm
#     #node
#     brew
#     docker
#     docker-compose
#     #docker-machine
#     #laravel5
#     #vagrant
#     tmux
#     emoji
#     #colorize
#     history
#     #per-directory-history
#     extract
#     #ansible
#     history-sync
#     fzf
#     #z.lua
#     #autoupdate
#     #history-search-multi-word
#     fzf-tab
#     iterm2
#     aws
#     alias-tips
#     # emacs
#     git-open
#     globalias
#     ripgrep
#     terraform
#     thefuck
#     ufw
#     command-not-found
#     common-aliases
#     gh
#     magic-enter
#     shellfirm
#     # zsh_reload
#     zsh-navigation-tools
#     history-substring-search
#     zsh-autosuggestions
#     zsh-completions
#     zsh-syntax-highlighting
# )

πŸ”

unalias h for history

#unalias h

πŸ”

End File

# end if dumb
fi

MY.ZSHRC

## -*- mode: sh -*-

history

export HISTFILE=$TRU_HISTFILE
export HISTSIZE=5000000
export SAVEHIST=1000000

# setopt HIST_FIND_NO_DUPS
setopt EXTENDED_HISTORY

Zsh autosuggestions

# https://github.com/zsh-users/zsh-autosuggestions#suggestion-highlight-style
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=99,underline"
# ZSH_AUTOSUGGEST_STRATEGY=(history completion)
ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20
ZSH_AUTOSUGGEST_USE_ASYNC=1
ZSH_AUTOSUGGEST_MANUAL_REBIND=1
ZSH_AUTOSUGGEST_COMPLETION_IGNORE='( |man |pikaur -S )*'

# _per-directory-history-set-global-history  # set per directory default to glboal

# This query will find the most frequently issued command
# that is issued in the current directory or any subdirectory.
# You can get other behaviours by changing the query, for example
_zsh_autosuggest_strategy_histdb_top_here() {
    local query="select commands.argv from
history left join commands on history.command_id = commands.rowid
left join places on history.place_id = places.rowid
where places.dir LIKE '$(sql_escape $PWD)%'
and commands.argv LIKE '$(sql_escape $1)%'
group by commands.argv order by count(*) desc limit 1"
    suggestion=$(_histdb_query "$query")
}

# https://www.dev-diaries.com/blog/terminal-history-auto-suggestions-as-you-type/
# This will find the most frequently issued command issued exactly in this directory,
# or if there are no matches it will find the most frequently issued command in any directory.
# You could use other fields like the hostname to restrict to suggestions on this host, etc.
_zsh_autosuggest_strategy_histdb_top() {
    local query="select commands.argv from
history left join commands on history.command_id = commands.rowid
left join places on history.place_id = places.rowid
where commands.argv LIKE '$(sql_escape $1)%'
group by commands.argv
order by places.dir != '$(sql_escape $PWD)', count(*) desc limit 1"
    suggestion=$(_histdb_query "$query")
}

# Query to pull in the most recent command if anything was found similar
# in that directory. Otherwise pull in the most recent command used anywhere
# Give back the command that was used most recently
_zsh_autosuggest_strategy_histdb_top_fallback() {
    local query="
    select commands.argv from
    history left join commands on history.command_id = commands.rowid
    left join places on history.place_id = places.rowid
    where places.dir LIKE
        case when exists(select commands.argv from history
        left join commands on history.command_id = commands.rowid
        left join places on history.place_id = places.rowid
        where places.dir LIKE '$(sql_escape $PWD)'
        AND commands.argv LIKE '$(sql_escape $1)%')
            then '$(sql_escape $PWD)'
            else '%'
            end
    and commands.argv LIKE '$(sql_escape $1)%'
    order by places.dir LIKE '$(sql_escape $PWD)' desc,
    history.id desc
    limit 1"
    suggestion=$(_histdb_query "$query")
}

_zsh_autosuggest_strategy_histdb_here_fallback() {
    local current_dir=$PWD
    local all_parents=("$current_dir")
    while [ "$current_dir" != "/" ]; do
        all_parents+=("$current_dir%")
        current_dir=$(dirname "$current_dir")
    done

    all_parents+=("/%")
    for parent in "${all_parents[@]}"; do
        local query="select commands.argv from
    history left join commands on history.command_id = commands.rowid
    left join places on history.place_id = places.rowid
    where places.dir LIKE '$(sql_escape $parent)'
    and commands.argv LIKE '$(sql_escape $1)%'
    group by commands.argv order by history.id desc limit 1"
        # debug
        #echo $query

        local result=$(_histdb_query "$query")

        if [[ -n $result ]]; then
            suggestion=$result
            break
        fi
    done

}

_zsh_autosuggest_strategy_histdb_here_fallback2() {
    local current_dir=$PWD
    local all_parents=("$current_dir")
    while [ "$current_dir" != "/" ]; do
        all_parents+=("$current_dir%")
        current_dir=$(dirname "$current_dir")
    done
    all_parents+=("/%")

    # https://stackoverflow.com/questions/50427449/behavior-of-arrays-in-bash-scripting-and-zsh-shell-start-index-0-or-1
    local query="WITH RECURSIVE search_dir AS (
          SELECT 1 AS level, '$(echo ${all_parents[@]:0:1})' AS dir
          UNION ALL
          SELECT level + 1, CASE level + 1"

    local i=2
    for dir in "${all_parents[@]:1}"; do
        query="$query WHEN $i THEN '$dir'
        "
        i=$((i + 1))
    done

    query="$query ELSE '%' END FROM search_dir WHERE level < $i
)
SELECT commands.argv
FROM history
LEFT JOIN commands ON history.command_id = commands.rowid
LEFT JOIN places ON history.place_id = places.rowid
WHERE places.dir LIKE
    (SELECT dir FROM search_dir WHERE EXISTS (
        SELECT 1
        FROM history
        LEFT JOIN commands ON history.command_id = commands.rowid
        LEFT JOIN places ON history.place_id = places.rowid
        WHERE places.dir LIKE search_dir.dir
        AND commands.argv LIKE '$(sql_escape $1)%'
    )
    LIMIT 1)
AND commands.argv LIKE '$(sql_escape $1)%'
ORDER BY history.id DESC
LIMIT 1;"

    suggestion=$(_histdb_query "$query")
}

#ZSH_AUTOSUGGEST_STRATEGY=(histdb_top_here histdb_top_fallback)
#ZSH_AUTOSUGGEST_STRATEGY=(histdb_top)
#ZSH_AUTOSUGGEST_STRATEGY=(history completion)
#ZSH_AUTOSUGGEST_STRATEGY=(histdb_top_fallback history completion)
ZSH_AUTOSUGGEST_STRATEGY=(histdb_here_fallback2 history completion)

# https://github.com/larkery/zsh-histdb/pull/31
HISTDB_TABULATE_CMD=(sed -e $'s/\x1f/\t/g')
alias histdb2='HISTDB_TABULATE_CMD=(sed -e $"s/.*\x1f//") histdb'

tru/show_local_history() {
    # limit="${1:-10}"
    # local query="
    #     select history.start_time, commands.argv
    #     from history left join commands on history.command_id = commands.rowid
    #     left join places on history.place_id = places.rowid
    #     where places.dir LIKE '$(sql_escape $PWD)%'
    #     order by history.start_time desc
    #     limit $limit
    # "
    local query="
        select
        replace(commands.argv, '
', ' \\n') as cmd
        from
        history left join commands on history.command_id = commands.rowid
        left join places on history.place_id = places.rowid
        where places.dir LIKE
            case when exists(select commands.argv from history
            left join commands on history.command_id = commands.rowid
            left join places on history.place_id = places.rowid
            where places.dir LIKE '$(sql_escape $PWD)'
            AND commands.argv LIKE '$(sql_escape $1)%')
                then '$(sql_escape $PWD)'
                else '%'
                end
        and commands.argv LIKE '$(sql_escape $1)%'
        group by commands.argv
        order by places.dir LIKE '$(sql_escape $PWD)' desc,
        history.id desc
        limit 1000
    "
    results=$(_histdb_query "$query")
    #echo -e `echo -n "$results" | fzf-tmux -p 90% -m --cycle`
    echo "`_histdb_query "$query" | fzf-tmux -p 90% -m --cycle`"
}

πŸ”

Globalias

# globalias
GLOBALIAS_FILTER_VALUES=(ls ll mv cp grep rm emacs tmux fzf)

πŸ”

alias-tips

export ZSH_PLUGINS_ALIAS_TIPS_TEXT="Alias tip: "
export ZSH_PLUGINS_ALIAS_TIPS_EXCLUDES="_ emacs ll"

Emacs macOS

Emacs macOS
# Add em alias for macOS
# PR Merged!
if [[ "$(uname)" == 'Darwin' ]]; then
    alias em="emacs"
    alias emacs='open -a "/Applications/Emacs.app" '
    #export EDITOR="emacs"
    # export EDITOR='/opt/homebrew/bin/emacs -nw -Q'
    #export VISUAL="emacs"
    # emacs on mac
    # export EDITOR="emacsclient -t"                  # $EDITOR should open in terminal
    # export VISUAL="emacsclient -c -a emacs"         # $VISUAL opens in GUI with non-daemon as alternate
    # https://emacs.stackexchange.com/questions/60339/using-emacsclient-for-visual-raises-end-of-file-during-parsing
    export VISUAL="$EDITOR_PATH/EDITOR"
    export EDITOR=$VISUAL
else
    export EDITOR="emacs"
    # workaround for https://github.com/robbyrussell/oh-my-zsh/pull/5714
    # alias emacs="te"
fi

# tramp mode for zsh: https://www.gnu.org/software/tramp/tramp-emacs.html
[ $TERM = "dumb" ] && unsetopt zle && PS1='# '

# https://github.com/zsh-users/zsh-history-substring-search
bindkey -M emacs '^P' history-substring-search-up
bindkey -M emacs '^N' history-substring-search-down
HISTORY_SUBSTRING_SEARCH_FUZZY=1
HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=1

set -o emacs
if [ -n "$INSIDE_EMACS" ]; then
  # chpwd() { print -P "\033AnSiTc %d" }

  # print -P "\033AnSiTu %n"
  # print -P "\033AnSiTc %d"
  # echo $INSIDE_EMACS
  alias clear='printf "\e]51;Evterm-clear-scrollback\e\\";tput clear'
  export ZSH_THEME="rawsyntax"

  # vterm_prompt_end() {
  #   printf "\e]51;A$(whoami)@$(hostname):$(pwd)\e\\";
  # }
  # PROMPT=$PROMPT'%{$(vterm_prompt_end)%}'

else
  test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
  # tab title show hostname
  # function precmd {
  #   vcs_info
  #   print -P "\n$(repo_information) %F{yellow}$(cmd_exec_time) \e]0;%m\a%f"
  # }

fi

# doom emacs
if [[ "$(uname)" == 'Darwin' ]]; then
   # export DOOMDIR=$DOOMDIR_MAC
   # export DOOMLOCALDIR=$DOOMLOCALDIR_MAC
   alias doome='doom sync && emacs'
fi

# The emacs or emacsclient command to use
e() {
    local TMP;
    if [[ "$1" == "-" ]]; then
        TMP="$(mktemp /tmp/emacsstdinXXX)";
        cat >"$TMP";
        if ! emacsclient --alternate-editor /usr/bin/false --eval "(let ((b (create-file-buffer \"my_drafts\"))) (tab-bar-new-tab) (switch-to-buffer b) (insert-file-contents \"${TMP}\") (delete-file \"${TMP}\"))"  > /dev/null 2>&1; then
            emacs --eval "(let ((b (create-file-buffer \"my_drafts\"))) (tab-bar-new-tab) (switch-to-buffer b) (insert-file-contents \"${TMP}\") (delete-file \"${TMP}\"))" &
        fi;
    else
        emacsclient --alternate-editor "emacs" --no-wait "$@" > /dev/null 2>&1 &
    fi;
}

# https://github.com/akermu/emacs-libvterm/blob/7adecaa48c222f2567d503705547cf239e38fc4b/README.md#shell-side-configuration
vterm_printf(){
    if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ] ); then
        # Tell tmux to pass the escape sequences through
        printf "\ePtmux;\e\e]%s\007\e\\" "$1"
    elif [ "${TERM%%-*}" = "screen" ]; then
        # GNU screen (screen, screen-256color, screen-256color-bce)
        printf "\eP\e]%s\007\e\\" "$1"
    else
        printf "\e]%s\e\\" "$1"
    fi
}


# notmuch seach
# https://emacs-china.org/t/topic/305/73?u=tru
export XAPIAN_CJK_NGRAM=1
# FIX OSError: dlopen(libnotmuch.5.dylib, 6): image not found
export DYLD_FALLBACK_LIBRARY_PATH=/opt/homebrew/lib/:/usr/local/lib/

## If you need to have imagemagick@6 first in your PATH, run:
## For compilers to find imagemagick@6 you may need to set:
## For pkg-config to find imagemagick@6 you may need to set:

# export PATH="/usr/local/opt/imagemagick@6/bin:$PATH"
# export LDFLAGS="-L/usr/local/opt/imagemagick@6/lib"
# export CPPFLAGS="-I/usr/local/opt/imagemagick@6/include"
# export PKG_CONFIG_PATH="/usr/local/opt/imagemagick@6/lib/pkgconfig"

πŸ”

Shell Magit
alias magit='emacsclient --eval "(magit-status)" && emacs'
Save and Exit
alias emacsk="emacsclient --eval \"(progn (save-some-buffers) (kill-emacs))\""

Function proxy enable/disable

export PS1_backup=$PS1

function tru/proxy () {
    local prefix
    if [ "$1" = "on" ]; then
        export https_proxy=127.0.0.1:8888
        export http_proxy=127.0.0.1:8888
        # echo Local HTTP Proxy is enabled.
        prefix="ProxyOn"
    else
        unset https_proxy
        unset http_proxy
        # echo Local HTTP Proxy is disabled.
        prefix=""
    fi
    # export PS1="%K{blue} $prefix $PS1_backup"
    export PS1="$prefix $PS1_backup"
}

tru/proxy off

πŸ”

Path

export PATH=/opt/homebrew/bin:/opt/homebrew/sbin:$PATH
export PATH=/usr/local/bin:/opt/homebrew/bin:/usr/local/opt:$PATH:/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:~/.composer/vendor/bin:/usr/local/sbin:/snap/bin
PATH="/opt/homebrew/opt/grep/libexec/gnubin:$PATH"
export PATH="/usr/local/opt/node@8/bin:$PATH"
export PATH="$HOME/.tgenv/bin:$PATH"
export PATH="/usr/local/opt/sqlite/bin:$PATH"

export PATH="/usr/local/opt/node@10/bin:$PATH"
export PATH="/usr/local/opt/curl/bin:$PATH"

πŸ”

GOPATH

# Go path for macOS
if [[ "$(uname)" == 'Darwin' ]]; then
   if [[ "$(uname -m)" == 'arm64' ]]; then
     export GOPATH=$HOME/go
     export GOROOT=/opt/homebrew/opt/go/libexec
     export PATH=$PATH:${GOPATH}/bin:${GOROOT}/bin
   else
     export GOPATH=$HOME/go
     export GOROOT=/usr/local/opt/go/libexec
     export PATH=$PATH:${GOPATH}/bin:${GOROOT}/bin
   fi
fi

πŸ”

LC_ALL autoload bashcompinit mc broot

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"

# autoload -U +X bashcompinit && bashcompinit
# complete -o nospace -C /opt/homebrew/bin/mc mc

# broot
[ -f ~/.config/broot/launcher/bash/br ] && source ~/.config/broot/launcher/bash/br

πŸ”

Upgrade_custom_plugins

tru/upgrade_custom_plugins () {
  printf "\e[1;34m%s\e[0m \n" "Upgrading custom plugins"

  find "${ZSH_CUSTOM}" -type d -name .git | while read d
  do
    p=$(dirname "$d")
    cd "${p}"
    echo -e "\e[0;33m${p}\e[0m"
    if git pull --rebase --stat origin master
    then
      printf "\e[0;92m%s\e[0m\n" "Hooray! $d has been updated and/or is at the current version."
    else
      printf "\e[1;31m%s\e[0m\n" 'There was an error updating. Try again later?'
    fi
    echo "\n"
  done
}

πŸ”

Fzf

basic
# fzf https://github.com/junegunn/fzf/wiki/Configuring-shell-key-bindings
export FZF_TMUX=1
alias fzf='fzf-tmux -p 80% --cycle'
fzf-history-widget-accept() {
  fzf-history-widget
  zle accept-line
}
zle     -N     fzf-history-widget-accept
bindkey '^X^R' fzf-history-widget-accept
bindkey '^[g'  fzf-cd-widget

# export FZF_DEFAULT_OPTS='--no-height --no-reverse --bind alt-a:select-all,alt-A:deselect-all,ctrl-t:toggle-all'
export FZF_DEFAULT_OPTS='--no-height --no-reverse
       --bind alt-a:toggle-all
       --bind ctrl-t:toggle-preview
       --bind=ctrl-alt-j:preview-down
       --bind=ctrl-alt-k:preview-up
'
# Using highlight (http://www.andre-simon.de/doku/highlight/en/highlight.html)
export FZF_CTRL_T_OPTS="--preview '(highlight -O ansi -l {} 2> /dev/null || cat {} || tree -C {}) 2> /dev/null | head -200'"
# Full command on preview window
export FZF_CTRL_R_OPTS="--preview 'echo {}' --preview-window down:3:hidden:wrap --bind '?:toggle-preview'"
# preview
export FZF_ALT_G_OPTS="--preview 'tree -C {} | head -200'"
# https://github.com/junegunn/fzf/pull/1946
export FZF_TMUX_OPTS='-p 80%'
# https://stnly.com/fzf-and-rg/
# Setting rg as the default source for fzf
#export FZF_DEFAULT_COMMAND='rg --files --no-ignore --hidden --follow -g "!{.git,node_modules}/*" 2> /dev/null'
# To apply the command to CTRL-T as well
#export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"

j() {
    local preview_cmd="ls {2}"
    if command -v exa &> /dev/null; then
        preview_cmd="exa -l {2}"
    fi

    if [[ $# -eq 0 ]]; then
        cd "$(autojump -s | sort -k1gr | awk '$1 ~ /[0-9]:/ && $2 ~ /^\// {print $1 " " $2}' | fzf --height 40% --reverse --inline-info --preview "$preview_cmd" --preview-window down:50% | awk '{print $2}')"
    else
        cd $(command autojump $@)
    fi
}

# https://github.com/junegunn/fzf/wiki/examples#searching-file-contents
# fif() {
#   ag --nobreak --nonumbers --noheading . | fzf
# }

fif() {
    if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
    local file
    # file="$(rga --max-count=1 --ignore-case --files-with-matches --no-messages "$@" | fzf-tmux +m --preview="rga --ignore-case --pretty --context 10 '"$@"' {}")" && open "$file"
    file="$(rga --max-count=1 --ignore-case --files-with-matches --no-messages "$@" | fzf-tmux +m --preview="rga --ignore-case --pretty --context 10 '"$@"' {}")" && echo "$file"
}

fif2() {
  if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
  rg --files-with-matches --no-messages "$1" | fzf --preview "highlight -O ansi -l {} 2> /dev/null | rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || rg --ignore-case --pretty --context 10 '$1' {}"
}

πŸ”

fzf snippets

https://github.com/tru2dagame/shownotes/blob/master/fzf-snippet.md

_tru_fzf-snippet() {
    local results preview key rest filename

    # merge filename and tags into single line

    # unsetopt shwordsplit
    # results=$(for FILE in $SNIPPETS_PATH/*
    #           do
    #               getname=$(basename $FILE)
    #               gettags=$(head -n 2 $FILE | tail -1)
    #               echo "$gettags ,| $getname"
    #           done)

    results=$(find "$SNIPPETS_PATH" -type f -print0 | xargs -0 awk 'FNR==2 {split(FILENAME,a,"/"); print $0 ",| " a[length(a)]}')
    preview=$(echo $results | column -s ',' -t | fzf -p 90% -i --ansi --bind ctrl-/:toggle-preview "$@" --preview-window up:wrap --preview "echo {} | cut -f2 -d'|' | tr -d ' ' | xargs -I % bat --color=always --language bash --plain $SNIPPETS_PATH/%" --expect=alt-enter)

    if [  -z "$preview" ]; then
        return
    fi

    key="$(head -1 <<< "$preview")"
    rest="$(sed 1d <<< "$preview")"
    filename=$(echo $rest | cut -f2 -d'|' | tr -d ' ')

    case "$key" in
        alt-enter)
            BUFFER=" $(cat $SNIPPETS_PATH/$filename | sed 1,2d)"
            ;;
        *)
            if [[ $(cat $SNIPPETS_PATH/$filename | sed 1,2d | wc -l | bc) -lt 8 ]]; then
                BUFFER=" $(cat $SNIPPETS_PATH/$filename | sed 1,2d)"
            else
                chmod +x $SNIPPETS_PATH/$filename
                BUFFER=" . $filename"
            fi
            ;;
    esac

    # if [ ! -z "$preview" ]
    # then
    #     filename=$(echo $preview | cut -f2 -d'|' | tr -d ' ')
    #     BUFFER=" $(cat $SNIPPETS_PATH/$filename | sed 1d)"
    #     CURSOR=0
    # fi

    #unset USE_NAME
}

zle -N _tru_fzf-snippet
bindkey "^[^[" _tru_fzf-snippet
bindkey "^[x" _tru_fzf-snippet

πŸ”

fzf snippets tab stop

https://github.com/verboze/zsh-snippets

_jump_to_tabstop_in_snippet() {
    # the idea is to match ${\w+}, and replace
    # that with the empty string, and move the cursor to
    # beginning of the match. If no match found, simply return
    # valid place holders: ${}, ${somealphanumericstr}
    local str=$BUFFER
    local searchstr=''
    [[ $str =~ ([$]\\{[[:alnum:]]*\\}) ]] && searchstr=$MATCH
    [[ -z "$searchstr" ]] && return

    local rest=${str#*$searchstr}
    local pos=$(( ${#str} - ${#rest} - ${#searchstr} ))
    BUFFER=$(echo ${str//${MATCH}/})
    CURSOR=$pos
}
zle -N _jump_to_tabstop_in_snippet
bindkey '^J' _jump_to_tabstop_in_snippet

πŸ”

fzf tmux
# https://github.com/junegunn/fzf/wiki/Examples#tmux
tru/tmux-ftpane() {
  local panes current_window current_pane target target_window target_pane
  panes=$(tmux list-panes -s -F '#I:#P - #{pane_current_path} #{pane_current_command}')
  current_pane=$(tmux display-message -p '#I:#P')
  current_window=$(tmux display-message -p '#I')

  target=$(echo "$panes" | grep -v "$current_pane" | fzf +m --reverse) || return

  target_window=$(echo $target | awk 'BEGIN{FS=":|-"} {print$1}')
  target_pane=$(echo $target | awk 'BEGIN{FS=":|-"} {print$2}' | cut -c 1)

  if [[ $current_window -eq $target_window ]]; then
    tmux select-pane -t ${target_window}.${target_pane}
  else
    tmux select-pane -t ${target_window}.${target_pane} &&
    tmux select-window -t $target_window
  fi
}
fzf git
is_in_git_repo() {
  git rev-parse HEAD > /dev/null 2>&1
}

fzf-down() {
  fzf -p 88% --border --bind ctrl-/:toggle-preview "$@"
}

fzf_gf() {
  is_in_git_repo || return
  git -c color.status=always status --short |
  fzf-down -m --ansi --nth 2..,.. \
    --preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1})' |
  cut -c4- | sed 's/.* -> //'
}

fzf_gb() {
  is_in_git_repo || return
  git branch -a --color=always | grep -v '/HEAD\s' | sort |
  fzf-down --ansi --multi --tac --preview-window right:70% \
    --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1)' |
  sed 's/^..//' | cut -d' ' -f1 |
  sed 's#^remotes/##'
}

fzf_gt() {
  is_in_git_repo || return
  git tag --sort -version:refname |
  fzf-down --multi --preview-window right:70% \
    --preview 'git show --color=always {}'
}

fzf_gh() {
  is_in_git_repo || return
  git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
  fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' \
    --header 'Press CTRL-S to toggle sort' \
    --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always' |
  grep -o "[a-f0-9]\{7,\}"
}

fzf_gr() {
  is_in_git_repo || return
  git remote -v | awk '{print $1 "\t" $2}' | uniq |
  fzf-down --tac \
    --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" {1}' |
  cut -d$'\t' -f1
}

fzf_gs() {
  is_in_git_repo || return
  git stash list | fzf-down --reverse -d: --preview 'git show --color=always {1}' |
  cut -d: -f1
}

join-lines() {
  local item
  while read item; do
    echo -n "${(q)item} "
  done
}

bind-git-helper() {
  local c
  for c in $@; do
    eval "fzf-g$c-widget() { local result=\$(fzf_g$c | join-lines); zle reset-prompt; LBUFFER+=\$result }"
    eval "zle -N fzf-g$c-widget"
    eval "bindkey '^g^$c' fzf-g$c-widget"
  done
}

bind-git-helper f b t r h s
unset -f bind-git-helper
fzf-interative-ripgrep

https://github.com/junegunn/fzf/blob/0818dbc36af28bf8f154a6d951a0062a2253a34a/ADVANCED.md#switching-to-fzf-only-search-mode

rgf() {

for arg; do
  case "$arg" in
    --noignore ) FLAG='--no-ignore' ;;
  esac
done

RG_PREFIX="rg $FLAG --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY=$(echo "${*:-}" |  sed 's/--noignore//')

# IFS=: read -ra selected < <(
fzf=$(FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \
        fzf --ansi \
        -e -m \
        --color "hl:-1:underline,hl+:-1:underline:reverse" \
        --disabled --query "$INITIAL_QUERY" \
        --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \
        --bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. fzf> )+enable-search+clear-query" \
        --bind "ctrl-o:execute-silent:(emacsclient --eval \"(progn (find-file \\\"\$(echo {} | awk -F ':' '{print \$1}')\\\") (goto-line \$(echo {} | awk -F ':' '{print \$2}')) (forward-char \$(echo {} | awk -F ':' '{print \$3}')) (recenter))\") && open  \"/Applications/Emacs.app\"" \
        --prompt '1. ripgrep> ' \
        --delimiter : \
        --preview 'bat --color=always {1} --highlight-line {2}' \
        --preview-window 'up,60%,border-bottom,+{2}+3/3,~3'
)

if [[ -n $fzf ]]; then
    echo $fzf
    # cmd=$(echo $fzf | awk -F ':' '{print "emacsclient --eval \"(progn (+workspace/new) (+workspace/switch-to-final) (find-file \\\""$1"\\\") (goto-line "$2") (forward-char "$3") (recenter))\"; " }' )
    cmd=$(echo $fzf | awk -F ':' '{print "emacsclient --eval \"(progn (find-file \\\""$1"\\\") (goto-line "$2") (forward-char "$3") (recenter))\"; " }' )
    echo $cmd
    eval $cmd > /dev/null 2>&1
    #emacs
    osascript -e "tell application \"Emacs\" to activate"
fi
}

Functions

Github_latest_release_download
# github_latest_release_download "Canop/broot"
tru/github_latest_release_download() {
    curl -s "https://api.github.com/repos/$1/releases/latest"  | jq -r ".assets[] | select(.name | contains(\"zip\"|\"gz\")) | .browser_download_url"
}

πŸ”

AWS profile switch
#export AWS_PROFILE=
awsp() {
    export AWS_PROFILE="$(aws-profiles | fzf --height 30% --inline-info)"
}

aws-profiles() {
    cat ~/.aws/credentials | grep '\[' | grep -v '#' | tr -d '[' | tr -d ']'
}

export AWS_PAGER=""

πŸ”

Add space in front of current command
addspace_ (){
    BUFFER=" $BUFFER"
    CURSOR=$#BUFFER
}
zle -N addspace_
bindkey "^s" addspace_

πŸ”

tmux
alias t=tmux

Spaceship

# spaceship
# https://github.com/tru2dagame/spaceship-prompt/blob/master/docs/Options.md#directory-dir
SPACESHIP_USER_SHOW=always
SPACESHIP_TIME_SHOW=true
SPACESHIP_DIR_TRUNC_REPO=false
SPACESHIP_DIR_TRUNC=0



# zprof    # debug

πŸ”

Misc

[[ ! -f $DOTDIR/misc/custom.zsh ]] || source $DOTDIR/misc/custom.zsh

πŸ”

ZSH DAILY TIPS

# https://twitter.com/dailyzshtip/status/1466384154778472459
for n ({1..5}) alias -g NF$n="*(.om[$n])"
# e.g. this gives you
# vi NF2   # edit 2nd newest file

# https://twitter.com/dailyzshtip/status/1458483872417583118
for n ({1..5}) alias -g ND$n="*(/om[$n])"
# ND1 # newest dir
# ND2 # 2nd newest dir

for n ({1..5}) alias -g NH$n=".*(.om[$n])"
# NH1 # newest hidden file
# NH2 # 2nd newest hidden file

πŸ”

zsh completion

# Ref: https://cli.github.com/manual/gh_completion
compinit -i

zoxide

command -v zoxide > /dev/null && eval "$(zoxide init zsh)"

chatgpt cli

chatgpt() {
    local OPENAI_KEY
    export OPENAI_KEY=$(pass show openai/key)
    command chatgpt "$@"
}

Copilot CLI

# https://github.com/github/gh-copilot/issues/5#issuecomment-1803088331

# Check if the gh copilot extension is installed and load aliases accordingly
if gh extension list | grep -q 'github/gh-copilot'; then
  copilot_shell_suggest() {
    gh copilot suggest -t shell "$@"
  }
  alias '??'='copilot_shell_suggest'

  # Function to handle Git command suggestions
  copilot_git_suggest() {
    gh copilot suggest -t git "$@"
  }
  alias 'git?'='copilot_git_suggest'

  # Function to handle GitHub CLI command suggestions
  copilot_gh_suggest() {
    gh copilot suggest -t gh "$@"
  }
  alias 'gh?'='copilot_gh_suggest'
fi

histdb fzf search

# source /Users/tru/Dropbox/git/src/github.com/m42e/zsh-histdb-fzf/fzf-histdb.zsh

EDITOR

#!/bin/sh
# https://emacs.stackexchange.com/questions/13809/how-do-i-use-emacsclient-as-editor-or-visual
# https://emacs.stackexchange.com/questions/60339/using-emacsclient-for-visual-raises-end-of-file-during-parsing
#exec emacsclient -F -c "$@"
exec /opt/homebrew/bin/emacsclient -F -c "$@"

About

My zshrc

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages