msysgitのGit Bashでも使える便利な5つのBashの補完機能

以下を設定しておくと便利になるというもの。
Linux/Unixユーザーなら、すでに設定してるんだろうけど、
WindowsユーザーGit BashBashを使い始めた人はなかなか
こういうものがあるのかがわからないので、情報には辿りつけない気がする。

.inputrc

%userprofile%\.inputrc に以下を設定する。

    #Search forward through the history for the string of characters between the
    #start of the current line and the point.  This is a non-incremental search.
    ##[DownArrow key] or [Ctrl +n]
    "\e[B":history-search-forward
    "\C-n":history-search-forward

    #Search  backward  through  the history for the string of characters between
    #the start of the current line and the point.   This  is  a  non-incremental
    #search.
    ##[UpArrow key] or [Ctrl +p]
    "\e[A":history-search-backward
    "\C-p":history-search-backward

    #Perform history expansion on the current line and insert a space.  
    # !!,!:1,!:1,!-2:1
    $if Bash
      Space:magic-space
    $endif

    #ignore case
    set completion-ignore-case on

.bashrc

%userprofile%\.bashrc に以下を設定する

    #complete typo
    shopt -s cdspell


どんな機能の説明、設定、使い方を説明する。

履歴の後方検索

説明

↑キー、または、Ctlr + pで履歴から補完する。
補完の対象は、コマンド、コマンドオプション、コマンドパラメータなど

設定
    "\e[A":history-search-backward
    "\C-p":history-search-backward
使い方

行頭で補完キーを入力するコマンド履歴をたどることができる。

    $ git diff .inputrc
      ↓
    $ cd dotfiles
      ↓
    $ ls


コマンドの履歴だけはなく、コマンドオプション、コマンドパラメータだけをたどることもできる。


以下のようなという履歴があったとする。

    4897 git status
    4901 git add .gitconfig
    4901 git commit
    4902 git push orign master
    4903 git status
    4904 git log --oneline
    4905 git log --graph --date=short --pretty=format:'%Cgreen%h %cd %Cblue%cn %Creset%s'


git の後で、補完キーを入力する

    git [↓キー] 
    git [Ctlr + p]


以下のように、コマンドオプションを遡っているくことができる。

   $ git log --graph --date=short --pretty=format:'%Cgreen%h %cd %Cblue%cn %Creset%s'
     ↓
   $ git log --oneline
     ↓
   $ git status
     ↓
   $ git push orign master
     ↓
   $ git commit
     ↓
   $ git add .gitconfig
     ↓
   $ git status

履歴の前方検索

    "\e[B":history-search-forward
    "\C-n":history-search-forward

↓キー、または、Ctlr + nで履歴から補完する。
補完の対象は、コマンド、コマンドオプション、コマンドパラメータなど

履歴の後方検索の場合とは逆に、履歴を未来の方向にたどる



特殊変数をその場で展開する

説明

以下のような特殊変数をその場で展開する
!:1 直前に実行したコマンドの1つ目のパラメータ
!:2 直前に実行したコマンドの2つ目のパラメータ
!-2:1 2つ前に実行したコマンドの1つ目のパラメータ

設定
    $if Bash
      Space:magic-space
    $endif
使い方

以下のように実行したとする

    $ git log --graph --date=short --pretty=format:'%Cgreen%h %cd %Cblue%cn %Creset%s'
    $ clear


この時点で以下のように実行すると特殊変数が展開される

    $ git !-2:1-4[スペース]

スペースを入力くすると、以下のように補完される。

  $ git log --graph --date=short --pretty=format:'%Cgreen%h %cd %Cblue%cn %Creset%s'

!-2:1-4は、2つ前のコマンドの1-4つ目までのオプション
(いい例が思いつかなかった。)


大文字小文字の無視

説明

ディレクトリ補完をするときに、大文字小文字の区別をしない。
Windowsだと大文字小文字の区別はしないため、それに合わせる。

設定
    set completion-ignore-case on
使い方

設定前だと以下のように dから補完されるのは2つ

    orangeclover@JUZI ~
    $ cd d
    dotfiles/       dotfiles.local/

設定後だと、以下のように d から補完されるのは大文字からディレクトリも含めて6つ

    orangeclover@JUZI ~
    $ cd d
    Desktop/        Documents/      Downloads/      Dropbox/        dotfiles/       dotfiles.local/

タイポの補完

説明

一文字の間違いなら、それに近いものに補完してくれる

設定

%userprofile%\.bashrcに以下を設定する

  #complete typo
  shopt -s cdspell
使い方

dotfiles ディレクトリだが、dotfilseとタイポした

orangeclover@JUZI ~
$ cd dotfilse


最も近いdotfilesに補完してくれた。

  orangeclover@JUZI ~/dotfiles (dev)
  $ pwd
  /c/Users/orangeclover/dotfiles

環境

関連

WindowsでGit(MsysGit)環境で、秀丸エディタ、Winmergeを使う設定




ここ間違ってるよ
こうした方がよくないか?
などなど
方法は、コメント、トラックバックはてブTwitter @orange_clover宛 で、お願いします。