💨

clinkのインストールと設定方法

2023/08/08に公開

windowsでコマンド・パス補完やコマンド履歴を保存することができるツール。
git-bashcygwinなど他にも類似ツールがあるが、こちらはbashコマンドなどがインストールされない。筆者的にはwindowsでbashコマンドが使えるほうが違和感があるのでこちらのツールを採用している。


インストール方法

公式Githubからインストール 以降はコマンドプロンプトを起動すると自動的にclink経由で起動する。

Microsoft Windows [Version 10.0.19044.2006]
(c) Microsoft Corporation. All rights reserved.

Clink v1.3.0.f070e4
Copyright (c) 2012-2018 Martin Ridgers
Portions Copyright (c) 2020-2021 Christopher Antos
https://github.com/chrisant996/clink

エイリアスの設定

以下の手順でエイリアスを作成することができる。

  1. %localappdata%\clink配下にaliasesファイルを作成

    clear=cls
    cp=copy $*
    ls=dir $*
    ns=nslookup $*
    ip=ipconfig $*
    aliases=notepad.exe %localappdata%\clink\aliases
    cat=type $1
    
  2. レジストリエディタ(regedit)を開き、HKEY_CURRENT_USER\Software\Microsoft\Command Processorを開く

  3. "C:\Program Files (x86)\clink\1.3.0.f070e4\clink.bat" inject --autorun --profile ~\clinkの末尾に && doskey.exe /macrofile=%LOCALAPPDATA%\clink\aliasesを追加


設定の確認

clink infoコマンドで確認することができる

version  : 1.3.0.f070e4
session  : 19656
binaries : C:\Program Files (x86)\clink\1.3.0.f070e4
state    : C:\Users\%USERNAME%\AppData\Local\clink
log      : C:\Users\%USERNAME%\AppData\Local\clink\clink.log
settings : C:\Users\%USERNAME%\AppData\Local\clink\clink_settings
history  : C:\Users\%USERNAME%\AppData\Local\clink\clink_history
scripts  : C:\Program Files (x86)\clink\1.3.0.f070e4 ; C:\Users\%USERNAME%\AppData\Local\clink
inputrc  : %clink_inputrc%
             (unset)
         : state directory
             C:\Users\%USERNAME%\AppData\Local\clink\.inputrc
             C:\Users\%USERNAME%\AppData\Local\clink\_inputrc
         : %userprofile%
             C:\Users\%USERNAME%\.inputrc   (LOAD)
             C:\Users\%USERNAME%\_inputrc
         : %localappdata%
             C:\Users\%USERNAME%\AppData\Local\.inputrc
             C:\Users\%USERNAME%\AppData\Local\_inputrc
         : %appdata%
             C:\Users\%USERNAME%\AppData\Roaming\.inputrc
             C:\Users\%USERNAME%\AppData\Roaming\_inputrc
         : %home%
             C:\Users\%USERNAME%\.inputrc   (exists)
             C:\Users\%USERNAME%\_inputrc

%localappdata%\clink\clink_settingsで設定を変更することができる。 設定できる値についてはこちらを参照(公式ドキュメントだが非常に分量が多く、設定できる項目が多い)。
最低限以下を設定すればよい

history.shared = True
clink.logo = short
  • history.shared : ターミナル間でのリアルタイムで履歴の共有
  • clink.logo: clink ロゴの表示を省略表示

.inputrc

%userprofile%\.inputrcでキーバインドなどを設定することができる。 設定方法についてはこちらを参照

# Some common Readline config settings.

set colored-statscolored-stats    on   # Turn on completion colors.
set colored-completion-prefix     on   # Color the typed completion prefix.

# Some config settings that only work in Clink.

$if clink
set search-ignore-case            on   # Case insensitive history searches.
set completion-auto-query-items   on   # Prompt before showing completions if they'll exceed half the screen.
$endif

# Add your keybindings here...

以下の手順でclink-fzfを導入することでctrl + rで履歴検索時にfzfを使って検索することができるようになる。
※事前にfzfはインストールしてPATHを通しておく必要がある。

  1. clink-fzfをダウンロード

  2. 全てのファイルを%localappdata%\clinkに格納

  3. %userprofile%\.inputrcに以下のキーバインドを記述

    # Default key bindings for fzf with Clink.
    "\C-t":        "luafunc:fzf_file"       # Ctrl+T lists files recursively; choose one or multiple to insert them.
    "\C-r":        "luafunc:fzf_history"    # Ctrl+R lists history entries; choose one to insert it.
    "\M-c":        "luafunc:fzf_directory"  # Alt+C lists subdirectories; choose one to 'cd /d' to it.
    "\M-b":        "luafunc:fzf_bindings"   # Alt+B lists key bindings; choose one to invoke it.
    "\t":          "luafunc:fzf_complete"   # Tab uses fzf to filter match completions.
    "\e[27;5;32~": "luafunc:fzf_complete"   # Ctrl+Space uses fzf to filter match completions.
    
ショートカット アクション
alt + h キーバインディングの表示
alt + b clink-fzf のキーバインディングの表示と実行
ctrl + t ファイルを再帰的に検索
ctrl + r コマンド履歴の検索
alt + c ディレクトリの検索と移動
alt + t ファイルの検索
Tab コマンド補完
ctrl + space コマンド補完(機能しない)

pecoと同じような表示にしたい場合は以下の手順に従って環境変数を登録

  1. win + x -> y -> システムの詳細設定 -> 環境変数で環境変数登録画面を開く
    win + r -> rundll32 sysdm.cpl,EditEnvironmentVariablesでも開くことが可能
  2. 変数名: FZF_DEFAULT_OPTS
    変数値: -e --layout=reverse --border rounded --height 100% "--prompt=""QUERY> """で登録

アップデート方法

clink updateアップデートが可能。
明らかにバージョンが古いのにUnable to find latest release.が出力される場合(プロキシなどで上手く通信できていない?)は、clink/releasesからインストーラをダウンロードして手動インストール。
既存の設定ファイルなどは初期化されないので指示に従ってインストールすればアップデート可能。


アンインストール方法

win + x -> f -> アプリと機能 -> clink で検索してアンインストール


デフォルト設定ファイル

C:\Program Files (x86)\clinkに格納されている。
ドキュメントを見るだけでは設定方法がわからない場合はこちらを参考にするとよい。


カラー設定

%localappdata%\clink\clink_settingsで設定を変更することができる。
ターミナルの背景色によってはclinkのデフォルトの色では見辛い場合などに有効
変更可能な設定についてはこちらを参照
色付けを無効にしたい場合は以下を設定ファイルに書き込めばよい

Clink v1.5.1

clink.colorize_input = False
# 白
color.input = sgr 38;5;231
# 黒
# color.input = sgr 38;5;16

コマンド履歴の整理

clinkでは%localappdata%\clink\clink_historyでコマンド履歴を管理している。
余計なコマンド履歴がずっと残る場合は直接精査するのが手っ取り早い
原因はわからないが、 |がコマンド履歴として残っていることが多々あるので、vs codeで正規表現で ^\|で検索してalt+ Enterで一括選択後、1行削除ショートカットを使うと一括で削除できる。


reference

Discussion