clinkのインストールと設定方法
windowsでコマンド・パス補完やコマンド履歴を保存することができるツール。
git-bash
やcygwin
など他にも類似ツールがあるが、こちらは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
エイリアスの設定
以下の手順でエイリアスを作成することができる。
-
%localappdata%\clink
配下にaliases
ファイルを作成clear=cls cp=copy $* rm=del $* mkdir=md $* ll=dir $* ls=dir /b $* grep=findstr $* np=notepad $* cat=type $1 touch=type nul > $1 cd~=cd %HOMEPATH% which=where $* aliases=type %LOCALAPPDATA%\clink\aliases
-
レジストリエディタ(
regedit
)を開き、HKEY_CURRENT_USER\Software\Microsoft\Command Processor
を開く -
"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
clink_settings
%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
clink上でfzfを使うことができるようになるスクリプト。
以下の手順で導入することでctrl
+ r
で履歴検索時にfzf
を使って検索することができるようになる。
※事前にfzfはインストールしてPATHを通しておく必要がある。
-
clink-fzfをダウンロード
-
全てのファイルを
%localappdata%\clink
に格納 -
%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と同じような表示にしたい場合は以下の手順に従って環境変数を登録
-
win
+x
->y
->システムの詳細設定
->環境変数
で環境変数登録画面を開く
※win
+r
->rundll32 sysdm.cpl,EditEnvironmentVariables
でも開くことが可能 - 変数名:
FZF_DEFAULT_OPTS
変数値:-e --layout=reverse --border rounded --height 100% "--prompt=""QUERY> """
で登録
clink-zoxide
shunsambongi/clink-zoxide: zoxide support for clink
clink上でzoxideのz
, zi
コマンドを実行できるようになるスクリプト
※事前にzoxideはインストールしてPATHを通しておく必要がある。
clink-zoxide
上のzoxide.lua
をダウンロードして%localappdata%\clink
直下に格納すれば以降のターミナル起動時から反映される。
clink_prompt
ターミナルのプロンプトをLinuxライクに表示できるスクリプト。
gitプロジェクト配下の場合はブランチ情報を可視化することが出来る。
ファイルをダウンロードして%localappdata%\clink
直下に格納すれば以降のターミナル起動時から反映される。
function set_prompt()
-- Get various env vars
local username = clink.get_env("USERNAME")
local computername = clink.get_env("COMPUTERNAME")
local user_home = clink.get_env("USERPROFILE")
local current_directory = clink.get_cwd()
-- Initial value
local toprompt = "[35;1m"..
username..
"[0m@[32;1m"..
computername.."[0m "..
"[37;1m"..
current_directory:gsub(user_home, "~")
-- Get current git branch
for line in io.popen("git branch 2>nul"):lines() do
local m = line:match("%* (.+)$")
if m then
-- toprompt = toprompt.." [34;1m["..m.."][0m "
toprompt = toprompt.." [34;1m("..m..")[0m "
break
end
end
-- Set prompt and add suffix
clink.prompt.value = toprompt.." $ "
return false;
end
clink.prompt.register_filter(set_prompt, 420)
筆者はブランチ情報は[]
閉じではなく、()
閉じで表示したいので一部改変した。
元の表示方法はコメントアウトしている。
アップデート方法
clink update
アップデートが可能。
明らかにバージョンが古いのにUnable to find latest release.
が出力される場合(プロキシなどで上手く通信できていない?)は、clink/releasesからインストーラをダウンロードして手動インストール。
既存の設定ファイルなどは初期化されないので指示に従ってインストールすればアップデート可能。
アンインストール方法
win + x
-> f
-> アプリと機能
-> clink
で検索してアンインストール
デフォルト設定ファイル
C:\Program Files (x86)\clink
に格納されている。
ドキュメントを見るだけでは設定方法がわからない場合はこちらを参考にするとよい。
カラー設定
%localappdata%\clink\clink_settings
で設定を変更することができる。
ターミナルの背景色によってはclinkのデフォルトの色では見辛い場合などに有効
変更可能な設定についてはこちらを参照
色付けを無効にしたい場合は以下を設定ファイルに書き込めばよい
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行削除ショートカットを使うと一括で削除できる。
Windows Terminal上でペイン分割した際にカレントディレクトリを引き継ぐ
C:\Program Files (x86)\clink\clink.bat
内に以下を記述することでプロンプトをLinuxライクかつ、
ペイン分割時にカレントディレクトリを引き継いで開くことができる。
set PROMPT=$e[01;32m%username%@%computername%$e[0m:$e[01;34m$P$s$e[0m$g$s
set PROMPT=$e]9;9;$P$e\%PROMPT%
ただし、管理者権限でないとclink.bat
は編集できないので、別ディレクトリにコピーして編集後に上書き保存すること
コマンドで実行する場合はコマンドプロンプトを管理者権限で起動して以下のコマンド
copy ".\clink.bat" "C:\Program Files (x86)\clink\clink.bat"
なお、この設定はあくまで、コマンドプロンプトでの手段なのでclinkに限った話ではない。
通常のコマンドプロンプトでも同様の設定で適用可能。
また、前述のclink_prompt
と組み合わせる方法はまだ調査できていない。
こちらを適用したい場合はclink_prompt
は使用しないこと。
reference
- clink
- clink documentation
- Support for custom aliases #92
- clink-fzf
- fzf
- clink-zoxide
- zoxide
- peco
- RangerMauve/clink_prompt.lua
Discussion