WindowsのPowerShellを設定するための8つのヒント
目次
1. Prerequisites
2. Tools
5. Conclusion
前提条件
1.1 .Windows端末
Windows端末を使用しますが、必須ではありません.他のターミナルエミュレータは行いますが、最近ではWindows端末は人気が高まっています.
インストールとダウンロードhttps://github.com/microsoft/terminal#installing-and-running-windows-terminal
推奨方法は、Windows Storeを介して自動更新を取得することです.
1.2 .パワーシェル
ダウンロードパワーシェル訪問https://aka.ms/powershell-release?tag=stable と最新バージョンに更新します.ここで示すすべてのステップはPowerShell 7.1.3を使用しています.インストール後は、Windowsターミナルのドロップダウンリストでpowershellを参照してください.これはデフォルトのシェルとして使用することをお勧めします.
インストールしたバージョンを次のように確認できます.
$PSVersionTable | Select-Object PSVersion
ツール
Windows端末を開く、PowerShellを選択し、沿って従ってください.各ツールは、より多くの例で広範なドキュメントがあります.私はほんの数を提供しますが、詳細に確認してください.
2.1 .スクープ
Scoop 素晴らしいツールを始めることです.それは別のように表示することができますChocolatey , しかしスクープは開発者コマンドラインツールとそのインストールプロセスに向けられています.
インストールするには
extras
使用するバケット# Download installer script and run it
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
# Add 'extras' bucket (has other useful tools)
scoop bucket add extras
# List all installed apps
scoop list
次の行は、スクープを使ってインストールするツールです.2.2 .fd
fd はfindコマンドの代替として使われる.これは非常に高速です、regexのマッチング、ファイルやディレクトリによってフィルタを許可し、無視ファイルを無視
.ignore
, .gitignore
, など# Install fd using scoop
scoop install fd
# Search current dir recursively for file containing "index" in the name
fd index
# Search file containing "index" in the name in the "./projects" subfolder
fd index ./projects
# Get all directories recursively
fd -t dll
2.3 .FZF
fzf 超高速ファジーフィルタ/ファインダーです.それは、それにパイプされている選択した項目を書き込む結果とインタラクティブなファインダーを起動します
STDOUT
. パイプで使用しない場合、fzfはファイルのリストを取得します.があるsearch syntax 結果を絞り込むには、ワークフローをさらに改善するためにチェックアウトします.
# Install fzf using scoop
scoop install fzf
# Search (using fd) all files with md extension, filter it and open result
# in notepad
fd -e md | fzf | %{ notepad $_ }
💡 We'll use fzf later with other tools that can use STDOUT and do someting with selected result.
2.4 .ラザッツット
lazygit Git用の素晴らしいターミナルUIです.これは、シンプルなUI、メニュー、インタラクティブなベースを提供することにより、Gitのコマンドに役立ちます.
# Install lazygit using scoop
scoop install lazygit
# Run it, q to exit
lazygit
💡 lazygit even supports mouse events (and works in Windows Terminal > v.1.9) which means that you can even click in the tool.
2.5 .ラツィドッカー
LazyDockerはLazygitと同じ著者から来ている.これは同様のUIを提供していますが、Dockerを管理するため.ビューの状態、ログ、ボリューム、一般的な操作を行うすべてのparams暗記せずに.
# Install lazydocker using scoop
scoop install lazydocker
# Run it, q to exit
lazydocker
2.6 .砂漣
ripgrep は、ファイルを無視する一方で、現在のディレクトリをRegexを使用して再帰的にカレントディレクトリを検索するツールです
.ignore
, .gitignore
, など# Install ripgrep using scoop
scoop install ripgrep
# Search existing directory for files that contain word "async"
rg async
# Specify file to search in
rg async ./userService.ts
2.7 .位置情報
ZLocation 頻繁に移動し、それらをランク付けディレクトリを覚えている.いくつかの時間後にそれはいくつかの文字でジャンプすることができます.あなたが検索を絞り込むために多くの文字(または正規)を提供することができます.
# Install
Install-Module ZLocation -Scope CurrentUser
# Get module up and running
Import-Module ZLocation
# Navigate to some directories to make ZL learn
cd C:/Windows
cd C:/Users/
# Switch between using z
z win
z u
# List all in history
z
# Return to previous location
z -
💡 In order to make ZLocation work every time you start PowerShell, we need to add it to profile. It's decribed in next section.
2.8 .理研
PSFzf ラップFZF(我々はスクープを介してそれをインストール)といくつかの便利なユーティリティを使用してPSでより良い使用を許可します.FZFを使用する他のタスクを簡単にするいくつかのniceヘルパー関数があります.次のセクションでそれらを有効にします.
# Install from PowerShell Gallery
Install-Module PSFzf
# Use it to switch directories
Get-ChildItem . -Attributes Directory | Invoke-Fzf | Set-Location
# Open VS code with selected file
Get-ChildItem . -Recurse -Attributes !Directory | Invoke-Fzf | % { code $_ }
# Use fd to get desired input to fzf and start VSCode with selected file
fd -e md | Invoke-Fzf | % { code $_ }
PowerShellプロファイルの設定
今、私たちは、すべてのPSインスタンスのために働くようにいくつかの追加セットアップをする必要があります.また、psfzfモジュールからいくつかのエイリアスを作成します.
💡 You can call
Enable-PsFzfAliases
to set all aliases, but I prefer to manually set only ones I use.
プロファイルは、シェルが作成されたときに実行されるスクリプトです.それらのいくつかが、私は
$profile.CurrentUserAllHosts
それで、あなたはすべてのホストで同じ経験をします.また、使用することができます$profile
を使うCurrentUserCurrentHost
詳しくはこちらをご覧ください.https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/ # Check if we have PowerShell profile created
Test-Path $profile.CurrentUserAllHosts
# This will create file if it doesn't exist
echo $null >> $profile.CurrentUserAllHosts
# Open our profile file in VSCode (or use preferred editor)
code $profile.CurrentUserAllHosts
PowerShellのプロファイルファイルを設定してエディタを開きます.あなたのプロフィールがすでにセットアップされているならば、あなたはそれをこれに加えることができます.私は、各々の線が何をするかについて説明するためにコメントを加えました.
次のエディタでペーストをペーストします.
# To make ZLocation module work in every PowerShell instance.
Import-Module ZLocation
# PSFzf has undocumented option to use fd executable for
# file and directory searching. This enables that option.
Set-PsFzfOption -EnableFd:$true
# Custom function to SetLocation, because PSFzf uses
# Get-ChildItem which doesn't use fd and doesn't use
# ignore files. Invoke-FuzzySetLocation is defined here
# https://github.com/kelleyma49/PSFzf/blob/b97263a30addd9a2c84a8603382c92e4e6de0eeb/PSFzf.Functions.ps1#L142
#
# This implementation is for setting FileSystem location
# and implementation uses parts of
# https://github.com/kelleyma49/PSFzf/blob/b97263a30addd9a2c84a8603382c92e4e6de0eeb/PSFzf.Base.ps1#L20
# https://github.com/kelleyma49/PSFzf/blob/b97263a30addd9a2c84a8603382c92e4e6de0eeb/PSFzf.Base.ps1#L35
function Invoke-FuzzySetLocation2() {
param($Directory = $null)
if ($null -eq $Directory) {
$Directory = $PWD.Path
}
$result = $null
try {
# Color output from fd to fzf if running in Windows Terminal
$script:RunningInWindowsTerminal = [bool]($env:WT_Session)
if ($script:RunningInWindowsTerminal) {
$script:DefaultFileSystemFdCmd = "fd.exe --color always . {0}"
}
else {
$script:DefaultFileSystemFdCmd = "fd.exe . {0}"
}
# Wrap $Directory in quotes if there is space (to be passed in fd)
if ($Directory.Contains(' ')) {
$strDir = """$Directory"""
}
else {
$strDir = $Directory
}
# Call fd to get directory list and pass to fzf
Invoke-Expression (($script:DefaultFileSystemFdCmd -f '--type directory {0} --max-depth 1') -f $strDir) | Invoke-Fzf | ForEach-Object { $result = $_ }
}
catch {
}
if ($null -ne $result) {
Set-Location $result
}
}
# Show tips about newly added commands
function Get-Tips {
$tips = @(
[pscustomobject]@{
Command = 'fcd'
Description = 'navigate to subdirectory'
},
[pscustomobject]@{
Command = 'ALT+C'
Description = 'navigate to deep subdirectory'
},
[pscustomobject]@{
Command = 'z'
Description = 'ZLocation'
},
[pscustomobject]@{
Command = 'fz'
Description = 'ZLocation through fzf'
},
[pscustomobject]@{
Command = 'fe'
Description = 'fuzzy edit file'
},
[pscustomobject]@{
Command = 'fh'
Description = 'fuzzy invoke command from history'
},
[pscustomobject]@{
Command = 'fkill'
Description = 'fuzzy stop process'
},
[pscustomobject]@{
Command = 'fd'
Description = 'find https://github.com/sharkdp/fd#how-to-use'
},
[pscustomobject]@{
Command = 'rg'
Description = 'find in files https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md'
}
)
Write-Output $tips | Format-Table
}
# Define aliases to call fuzzy methods from PSFzf
New-Alias -Scope Global -Name fcd -Value Invoke-FuzzySetLocation2 -ErrorAction Ignore
New-Alias -Scope Global -Name fe -Value Invoke-FuzzyEdit -ErrorAction Ignore
New-Alias -Scope Global -Name fh -Value Invoke-FuzzyHistory -ErrorAction Ignore
New-Alias -Scope Global -Name fkill -Value Invoke-FuzzyKillProcess -ErrorAction Ignore
New-Alias -Scope Global -Name fz -Value Invoke-FuzzyZLocation -ErrorAction Ignore
FZFの使い方
FZFの実権は、結果が別の操作にパイプされたときに来る.ファイルを開くか、ディレクトリを変更するために使用できます(FZFへの入力がディレクトリ/ファイルリストであるならば)、プロセスをkillして、コマンド史を通してナビゲートしてください.
💡 There are more commands in PSFzf than ones covered here. They can be also used as a base for custom commands.
# Call fe to get list of files to edit in current directory (or pass dir as param)
# After selecting file it will open VS Code (or set $env:EDITOR variable to executable)
fe
fe ./services
# List recent commands executed and search through them
fh
fh curl
# Find process to kill
fkill
# CHANGING DIRECTORY
# List one depth folders and change to it (respects ignore files)
fcd
# Get from recently navigated locations
z
z proj
# Using "z" by itself works, but if you need more control try
# which will list all frequent locations and give you fzf to change into
fz
# <Alt+C> - similar to fcd but lists all subdirectories
💡 Invoke
tips
to get quick refresher about newly added features. This function is defined in profile.
PS C:\> tips
Command Description
------- -----------
fcd navigate to subdirectory
ALT+C navigate to deep subdirectory
z ZLocation
fz ZLocation through fzf
fe fuzzy edit file
fh fuzzy invoke command from history
fkill fuzzy stop process
fd find https://github.com/sharkdp/fd#how-to-use
rg find in files https://github.com/BurntSushi/ripgrep/blob/master/GUIDE.md
結論
ディレクトリ、操作履歴、および編集ファイルをナビゲートするような操作は、端末で行う基本コマンドの下に落ちます.いくつかのツールとエイリアスはPowerShellに追加され、我々は基本的な経験を非常に楽しく、柔軟性を得た.コメントのより多くのツールを提案します.
Reference
この問題について(WindowsのPowerShellを設定するための8つのヒント), 我々は、より多くの情報をここで見つけました https://dev.to/codeasy/8-tips-for-setting-up-powershell-on-windows-mjfテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol