LoginSignup
34
23

More than 5 years have passed since last update.

Ubuntu bashのプロンプトHack!!!!(gitブランチ表示)

Last updated at Posted at 2017-07-06

bashをもっと便利にしたい

普段はWindows+vagrant+Ubuntu14.04をベースに色々な環境を整えて開発することが多い今日この頃。
簡単な作業ならWindowsでやるんですけど、その際はGit for Windowsに付属してくる、
Git Bashを使って作業します。

Git for Windows
↑本家のサイト

Windowsで作業するときのCLIは色々選択肢があって、
どれも一長一短な感じがあります。(Powershell、Git bash、コマンドプロンプト

最近はGit bashに落ち着いてる感じです。
Unixの感覚に近いし、最低限必要なものはプリインストールされてます。
Git bashで特に便利なのが、gitのディレクトリ内にいるときにプロンプトに現在のブランチが表示されるところですね。

user@hoge MINGW64 ~/hoge/git (master)

↑こんな感じ
これは便利ですね。いちいちgit branchコマンド打たなくて済みます。
これをLinuxの仮想マシン側でもやりたいわけです。

調べてみた

結構簡単に実現できます。

~/.bashrcに下記を追記してください。

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi
if [ -f /etc/bash_completion.d/git-prompt ]; then
    export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w$(__git_ps1) \n\[\033[01;34m\]\$\[\033[00m\] '
else
    export PS1='\[\033[01;32m\]\u@\h\[\033[01;33m\] \w \n\[\033[01;34m\]\$\[\033[00m\] '
fi

bash_completionがインストールされている必要がありますが、
Ubuntuは最初からインストールされているのでこれを追記するだけで大丈夫です。

あとは、

・git bashを立ち上げ直す
・source ~/.bashrc

のどちらかをすれば反映されます。

便利だね

けっこう有名な方法だけど、備忘かねて記事にしておきます。

34
23
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
34
23