Open7

シェル設定チートシート

antyuntyunantyuntyun

bash

新しい環境で作業するときのとりあえずの設定

wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
chmod a+x ~/.git*.sh
antyuntyunantyuntyun

fzf intall

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

bashrc 追記

source ~/.git-completion.bash
source ~/.git-prompt.sh

# The various escape codes that we can use to color our prompt.
RED="\[\033[0;31m\]"
YELLOW="\[\033[1;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[1;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[1;30m\]"
COLOR_OCHRE="\[\033[38;5;95m\]"
COLOR_NONE="\[\e[0m\]"
LIGHT_CYAN="\[\033[1;36m\]"

# Detect whether the current directory is a git repository.
function is_git_repository {
  git branch > /dev/null 2>&1
}

# Determine the branch/state information for this git repository.
function set_git_branch {
  # Capture the output of the "git status" command.
  git_status="$(git status 2> /dev/null)"

  # Set color based on clean/staged/dirty.
  if [[ ${git_status} =~ "nothing to commit, working tree clean" ]]; then
    state="${GREEN}"
  elif [[ ${git_status} =~ "Changes to be committed:" ]]; then
    state="${YELLOW}"
  elif [[ ${git_status} =~ "Changes not staged for commit:" ]]; then
    state="${LIGHT_RED}"
  elif [[ ${git_status} =~ "Untracked files:" ]]; then
    state="${BLUE}"
  else
    state="${COLOR_OCHRE}"
  fi

  # Set arrow icon based on status against remote.
  remote_pattern="Your branch is (.*) of"
  if [[ ${git_status} =~ ${remote_pattern} ]]; then
    if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
      remote="^"
    else
      remote="v"
    fi
  else
    remote=""
  fi
  diverge_pattern="Your branch and (.*) have diverged"
  if [[ ${git_status} =~ ${diverge_pattern} ]]; then
    remote="^v"
  fi

  # Get the name of the branch.
  branch_pattern="^On branch ([^${IFS}]*)"
  if [[ ${git_status} =~ ${branch_pattern} ]]; then
    branch=${BASH_REMATCH[1]}
  fi

  # Set the final branch string.
  BRANCH="${state}(${branch})${remote}${COLOR_NONE} "
}

# Return the prompt symbol to use, colorized based on the return value of the
# previous command.
function set_prompt_symbol () {
  if test $1 -eq 0 ; then
      PROMPT_SYMBOL="${LIGHT_GRAY}\$${COLOR_NONE}"
  else
      PROMPT_SYMBOL="${LIGHT_RED}\$${COLOR_NONE}"
  fi
}

# Determine active Python virtualenv details.
function set_virtualenv () {
  if test -z "$VIRTUAL_ENV" ; then
      PYTHON_VIRTUALENV=""
  else
      # PYTHON_VIRTUALENV="${LIGHT_GREEN}[`basename \"$VIRTUAL_ENV\"`]${COLOR_NONE} "
      PYTHON_VIRTUALENV="${LIGHT_GREEN}[(`basename \"$VIRTUAL_ENV\"`) "$PYTHON_VERSION"]${COLOR_NONE} "
  fi
}

# Set the full bash prompt.
function set_bash_prompt () {
  # Set the PROMPT_SYMBOL variable. We do this first so we don't lose the
  # return value of the last command.
  set_prompt_symbol $?

  # Set the PYTHON_VIRTUALENV variable.
  set_virtualenv

  # Set the BRANCH variable.
  if is_git_repository ; then
    set_git_branch
  else
    BRANCH=''
  fi

  # Set the bash prompt variable.
  PS1="
${LIGHT_CYAN}\w${COLOR_NONE} 
${LIGHT_GRAY}@\h[\u]${PYTHON_VIRTUALENV} ${BRANCH}${PROMPT_SYMBOL} "
}

# export PS1="\[\033[32m\]\w\n\[\033[0m\][\u]\[\033[36m\]\$(__git_ps1)\[\033[00m\]\$ "
PROMPT_COMMAND=set_bash_prompt


if [ -d $HOME/.anyenv ]
then
    export PATH="$HOME/.anyenv/bin:$PATH"
    eval "$(anyenv init - zsh)"
fi

[ -f ~/.fzf.bash ] && source ~/.fzf.bash

# 色合い変化と候補をシェル直下に表示するように変更
export FZF_DEFAULT_OPTS='--color=fg+:11 --height 70% --reverse --select-1 --exit-0 --multi'

function fzf_pjc() {
  local project_name=$(ghq list | sort | $(__fzfcmd))
  if [ -n "$project_name" ]; then
    local project_full_path=$(ghq root)/$project_name
    local project_relative_path="~/$(realpath --relative-to=$HOME $project_full_path)"
    READLINE_LINE="cd $project_relative_path"
    READLINE_POINT=${#READLINE_LINE}
  fi
}
bind -x '"\C-]": fzf_pjc'

fbrm() {
  local branches branch
  branches=$(git branch --all | grep -v HEAD) &&
  branch=$(echo "$branches" |
           fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
  git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
bind -x '"\C-b": fbrm'

function awsp() {
  local profile=$(grep -o -P '(?<=\[profile )[^\]]+' ~/.aws/config | sort | fzf )
  export AWS_PROFILE="$profile"
}
function sshsp() {
  local host=$(grep -E "^Host|^$" ~/.ssh/config | sed 's/Host //' | fzf)
  ssh $host
}

# aws cli completion
complete -C aws_completer aws
export aws_cli_auto_prompt=on

# enhanced cd
source ~/enhancd/init.sh

bind -x '"\C-[": "cd"'

antyuntyunantyuntyun
git clone https://github.com/anyenv/anyenv ~/.anyenv
anyenv install --init
anyenv install goenv
exec $SHELL -l
goenv install 1.17.2
goenv global 1.17.2
go get github.com/x-motemen/ghq
antyuntyunantyuntyun

giconfig
ghq_srcとcredentialは適宜変更

[core]
    excludesfile = ~/.gitignore_global
    autocrlf = false
    quotepath = false
    pager = less -r
    symlinks = true
[color]
    ui = auto
[init]
    defaultBranch = main
[push]
    default = current
[merge]
    ff = false
[pull]
    rebase = preserve
[alias]
    l = log --date=short --pretty=format:'%C(yellow)%h %Cgreen%cd %Cblue%cn %Creset%s'
    ll = log --stat --decorate=short --pretty=format:'%C(yellow)%h %Cgreen%cr %Cblue%cn%Cred%d %Creset%s %C(cyan)%b'
    lg = log --graph --date=short --decorate=short --pretty=format:'%C(yellow)%h %Cgreen%cd %Cblue%cn%Cred%d %Creset%s'
    lm = log --merges --pretty=format:'%C(yellow)%h %Cgreen%ci %Cblue%cn%Cred%d %Creset%s %C(cyan)%b'
    b = branch
    ba = branch -a
    bd = branch --delete
    bfd = branch -D
    co = checkout
    com = checkout master
    cod = checkout develop
    cob = checkout -b
    a = add
    aa = !git add -A && git s
    c = commit
    cm = commit -m
    cam = commit -a -m
    wip = commit -m 'WIP'
    amend = commit --amend -C HEAD
    d = diff
    dc = diff --cached
    dn = diff --name-only
    dw = diff --color-words
    um = !git --no-pager diff --name-only --diff-filter=U
    ft = fetch
    ftp = fetch --prune
    pl = pull
    ps = push
    info = remote show origin
    s = status --short --branch
    st = status
    r = reset
    rs = reset --soft
    rsh = reset --soft HEAD
    rh = reset --hard
    rhh = reset --hard HEAD
    rl = reflog show -n 20
    rb = rebase
    rbi = rebase -i
    rbh = rebase HEAD
    rbo = rebase --onto
    sync = !git checkout master && git pull origin master && git fetch -p origin && git branch -d $(git branch --merged | grep -v master | grep -v '*')
    ignore = "!f() { echo $1 >> \"${GIT_PREFIX}.gitignore\"; }; f"
    alias = !git config --list | grep 'alias\\.' | sed 's/alias\\.\\([^=]*\\)=\\(.*\\)/\\1\\\t => \\2/' | sort
[credential]    
    helper = !aws --profile codecommit credential-helper $@
    UseHttpPath = true
[http]
    sslVerify = true
[ghq]
    root = ~/ghq_src
antyuntyunantyuntyun
cd
git clone https://github.com/b4b4r07/enhancd
antyuntyunantyuntyun

vimrc

curl -L https://gist.githubusercontent.com/antyuntyuntyun/fc092cf1e308459ec7bcec4542169a1e/raw/32a0c5264524acfd4ec28a53a095249e8ebdc187/.vimrc > ~/.vimrc
antyuntyunantyuntyun

設定コマンドまとめ

gistにあげたりしてまとめ

# git*.sh
wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
wget --progress=dot:giga https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
chmod a+x ~/.git*.sh
# dotfiles
[ -e "~/.bashrc" ] && cp ~/.bashrc ~/.bashrc.bck
[ -e "~/.vimrc" ] && cp ~/.vimrc ~/.vimrc.bck
[ -e "~/.gitconfig" ] && cp ~/.gitconfig ~/.gitconfig.bck
curl -L https://gist.githubusercontent.com/antyuntyuntyun/29fb154222afb50e64344b1dcb4d5d38/raw/05ecc79ae838fd9ac74d35f6fc95f256c03463a6/.bashrc >> ~/.bashrc
curl -L https://gist.githubusercontent.com/antyuntyuntyun/fc092cf1e308459ec7bcec4542169a1e/raw/611e79baeb0ffc97beb6addb25ac90015454905c/.vimrc > ~/.vimrc
curl -L https://gist.githubusercontent.com/antyuntyuntyun/9f209fa34f4c35d79364b00bd561b7ea/raw/5064d707c92841b334a52dcab8f0df18d3a4bd82/.gitconfig > ~/.gitconfig
# git
git config --global user.name <username>
git config --global user.email <email>
git config --global ghq.root <root directory path>
# cd enhance
git clone https://github.com/b4b4r07/enhancd ~/.enhancd
# fzf
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
# anyenv
git clone https://github.com/anyenv/anyenv ~/.anyenv
source  ~/.bashrc
anyenv install --init
# goenv / ghq
anyenv install goenv
source  ~/.bashrc
goenv install 1.18.0
goenv global 1.18.0
# go get github.com/x-motemen/ghq
# 1.18.0からはgo get ではなく go install に
go install  github.com/x-motemen/ghq@latest