0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

重複したコマンド履歴を削除する

コマンド履歴をきれいにしたい方におすすめです。

export HISTCONTROL=ignoreboth:erasedups

ランダムパスワードを生成する

外部のパスワード生成サイトを使いたくないよ、という方はコマンドを自分で用意してみましょう。
/dev/urandom は推測されやすいみたいなので /dev/random から生成しています。

参考:https://serverfault.com/questions/283294/how-to-read-in-n-random-characters-from-dev-urandom

randpw() {
  local COUNT=$1
  head -c 1000 /dev/random | tr -dc '!-~' | fold -w "${COUNT}" | head -n 1
}

cd したとき Python の仮想環境があったら有効化する (Windows/Linux 対応)

. .venv/bin/activate とかを叩く手間が省けて便利です。
仮想環境のあるディレクトリの外に出ると deactivate もされます。

参考:https://stackoverflow.com/questions/45216663/how-to-automatically-activate-virtualenvs-when-cding-into-a-directory

# check if terminal is MinGW
__is_mingw() {
  local kernel_name
  kernel_name="$(uname -s)"
  if [[ "${kernel_name^^}" =~ "MINGW" ]]; then
    return 0
  else
    return 1
  fi
}

# overload builtin "cd"
__after_cd() {
  # https://stackoverflow.com/questions/45216663/how-to-automatically-activate-virtualenvs-when-cding-into-a-directory
  if [[ -z "$VIRTUAL_ENV" ]]; then
    local venv_contained_dir
    ## If env folder is found then activate the virtualenv
    if [[ -d ./.venv ]]; then
      venv_contained_dir="."
    elif [[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]]; then
      local git_repo_root
      git_repo_root="$(git root)"
      if [[ -d "$git_repo_root"/.venv ]]; then
        venv_contained_dir="$git_repo_root"
      fi
    fi

    if [[ -z "$venv_contained_dir" ]]; then
      return 0
    fi

    local activate_bin_path
    if __is_mingw; then
      activate_bin_path=".venv/Scripts/activate"
    else
      activate_bin_path=".venv/bin/activate"
    fi

    if [[ ! -f "$venv_contained_dir"/"$activate_bin_path" ]]; then
      echo 'venv dir exists but activate command does not exist'
      return 0
    fi

    # shellcheck disable=SC1090
    source "$venv_contained_dir"/"$activate_bin_path"
  else
    local parent_dir
    local current_dir
    ## check the current folder belong to earlier VIRTUAL_ENV folder
    # if yes then do nothing
    # else deactivate
    if __is_mingw; then
      parent_dir=$(dirname "$VIRTUAL_ENV" | sed -e 's/C:/\/c/' | sed -e 's/\\/\//g')
    else
      parent_dir="$(dirname "$VIRTUAL_ENV")"
    fi
    current_dir=$(pwd -P)
    if [[ "$current_dir"/ != "$parent_dir"/* ]]; then
      deactivate
    fi
  fi
}

cd() {
  builtin cd "$@" || return 1
  __after_cd
}

__after_cd

cat を dog で呼ぶ

犬派の人におすすめです。
メリットは「犬派に好かれる :dog:」「両手をバランスよく使うので cat より速く打てる」です。
デメリットは「猫派に嫌われる :cat:」「将来 dog コマンドが登場した際に衝突する」です。

alias dog='cat'

ちなみに私は猫派です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?