ååãLinuxã®lessã³ãã³ããåãäžããã³ãã³ãåäœã§äœ¿ãå ŽåïŒãã€ãã©ã€ã³ã«æ¥ç¶ããŠäœ¿ãå Žåã®ã©ã¡ãã§ããã¡ããšåäœããããã«èšå®ããããã€ãã©ã€ã³ãæµããŠããããŒã¿ããã£ãããã¡ã€ã«ã«æžã蟌ãããšã§ããã€ãã䜿ãããå Žåããã¡ã€ã«ãæå®ããŠLinuxã®lessã³ãã³ããå®è¡ããããã«å€æŽããã®ã ãããã§ããã€ãã©ã€ã³ã䜿ã£ãå Žåã«çºçããããã€ãã®åé¡ãåé¿ã§ããããã«ãªã£ãã
ãããããã°ãã䜿ã£ãŠã¿ããšãless以å€ã®ã³ãã³ãã§ã»ãã®äžå ·åãããããšãããã£ããåŒæ°ã1ã€ãããªãå Žåã¯åé¡ãªãã®ã ãã2ã€ä»¥äžã®åŒæ°ãåãã±ãŒã¹ã§ã¯æåŸ ããåäœãããªãã®ã ãäŸãã°ãUbuntuãªã©ã®ç®¡çã«æ¬ ãããªãaptã³ãã³ãã¯2ã€ä»¥äžã®åŒæ°ã䌎ãããšããããããã®å Žåããšã©ãŒãçºçããŠåäœããªããããããåãããšãwslã³ãã³ãã䜿ã£ãŠå®è¡ãããšãæ£ããåäœããã®ã§ããã
![]() |
aptã³ãã³ãã§åŒæ°ã2ã€ä»¥äžã«ãªããšãšã©ãŒãçºçããŠåäœããªãããåãããšãwslã³ãã³ãã§å®è¡ãããšåäœãã |
ã€ãŸãã以å$PROFILEã«è¿œå ããLinuxã³ãã³ããå šéšé¢æ°åããåŠçã®ã©ããã§äžå ·åãçºçããŠããããšã«ãªãã
該åœããéšåã¯æ¬¡ã®åŠçã ã
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args)
}
else {
wsl $n `$(_path_to_linux `$Args)
}
}"
}
ãã®åŠçã§ã¯wslã³ãã³ãã®èšè¿°ã2ã«æãããæ¬¡ã®éšåã ã
âwslãå®è¡ããŠããéšå - ã³ãã³ãåäœçœ®ãæã
wsl $n `$(_path_to_linux `$Args)
âwslãå®è¡ããŠããéšå - ãã€ãã©ã€ã³ã«æ¥ç¶ããŠããã³ãã³ã眮ãæã
`$Input | wsl $n `$(_path_to_linux `$Args)
ãã®éšåã¯ã颿°åã³ãŒãããã£ãããã¡ã€ã«ã«æžãåºããŠããèªã¿èŸŒãããšããç®çã®ããã«èšè¿°ãããŠããã®ã§ããšã¹ã±ãŒãã³ãŒããå ¥ã£ãŠãããè¥å¹²ãããã¥ãããããããªãããšã¹ã±ãŒãéšåãé€ããšã次ã®ããã«èšè¿°ããŠããããšã«ãªãã
âwslãå®è¡ããŠããéšå - ã³ãã³ãåäœçœ®ãæã
wsl $n $(_path_to_linux $Args)
âwslãå®è¡ããŠããéšå - ãã€ãã©ã€ã³ã«æ¥ç¶ããŠããã³ãã³ã眮ãæã
$Input | wsl $n $(_path_to_linux $Args)
åé ã«ç€ºããåäœäŸãšæ¯èŒãããšãã$Argsããã_path_to_linux()ãã§åŠçããçµæã«çœ®ãæããŠããéšåã«éããããããã®éšåã¯ã$()ãã«ããéšååŒæŒç®åã䜿ã£ãŠããã®ã ãããã®åŠçãçŽæ¥wslã³ãã³ãã䜿ã£ãã±ãŒã¹ãšã¯éãã®ã ã
ãã€ã¯ããœããã®ããã¥ã¡ã³ãã§éšååŒæŒç®åïŒ$(): Subexpression operatorïŒã®èª¬æãabout_Operators - PowerShell | Microsoft Docsããããèªãã§ã¿ããšãã©ãããã®æŒç®åã®çµæã¯ããã§ã¯åäžã®æååãšããŠè¿ã£ãŠããŠããããã ãã€ãŸããè€æ°ã®åŒæ°ãæå®ãããŠãããšããŠããéšååŒæŒç®åãçµç±ããæ®µéã§åäžã®æååã«ãªã£ãŠããŸã£ãŠããã®ã§ããã
çµæãšããŠãåé ã®å®è¡äŸã§ã¯ãapt list lessããšããæå®ããaptã³ãã³ãããšãlistããlessããšãã2ã€ã®åŒæ°ã§ã¯ãªãããapt list lessããšãã1ã€ã®ã³ãã³ãã ãšèŠãªãããŠããããšã«ãªããããã§ã¯æåŸ ããããã«åäœããªãã®ããã£ãšãã ã
åãåé¡ã§åäœããŠããªãèšå®ãã»ãã«ããã
åããããªèšè¿°ãããŠããç®æã¯ã»ãã«ããããäŸãã°ããlsãããllããªã©ã¯æ¬¡ã®ãããªèšå®ãããŠããããããã¡ããè€æ°ã®åŒæ°ãåäžã®æååã ãšèŠãªãããŠããããšã«ãªãã
# ls
Get-Alias ls *> $null && Remove-Item alias:ls
function ls { wsl ls --color=auto $(_path_to_linux $Args) }
function ll { ls -l $(_path_to_linux $Args) }
function la { ls -a $(_path_to_linux $Args) }
å®è¡ããŠã¿ããšã次ã®ããã«åŒæ°ã2ã€ä»¥äžã«ãªããšãšã©ãŒãçºçããããšã確èªã§ããã
ååäœæããless颿°ã«ãåãèšè¿°ãããã®ã§ãåãåé¡ãèµ·ããã
#less
function less {
if ($Input.Length) {
$Input.Reset()
# Prepare temporary file path
$_temp = New-TemporaryFile
# Write data from pipeline to the temporary file
$Input | Out-File $_temp
# Do less
wsl less $(_path_to_linux $_temp.ToString())
# Delete unnecessary temporary file and variable
Remove-Item $_temp
Remove-Variable _temp
}
else {
wsl less $(_path_to_linux $Args)
}
}
ãããèšå®ãããèŠçŽããŠã¿ããšããã€ãã©ã€ã³ãæ¥ç¶ãããå Žåã®åäœã§ã¯ãlessã³ãã³ãã«æå®ãããåŒæ°ãããããç¡èŠãããŠããããšãããã£ããããã¯ä»åãšã¯å¥ã®ãã¹ã ãååã®æžãæãã®éãåŒæ°ã®åŠçãå ããã®ãå¿ããŠããã
詊ãã«å®è¡ããŠã¿ããšã次ã®ããã«ãšã©ãŒãçºçããã
åé¡éšåã¯æããã«ãªã£ãã®ã§ããããä¿®æ£ããŠãããã
æååãé åã«å€æããŠããåŠçããããšã§è§£æ±º
è€æ°ã®åŒæ°ãåäžã®æååãšããŠæ±ãããããšã§åé¡ãçºçããŠããã®ã ãããæ¬¡ã®ããã«æååã«ãªã£ãåŸã§ã.Split(â â)ãã䜿ããå床é åãžåè§£ããŠã¿ãã
ãããšãæåŸ éãã«åäœããããã«ãªã£ããä»åã¯ããã®æ¹æ³ã§ä¿®æ£ããŠãããã
åäœç¢ºèª
該åœéšåãã.Split(â â)ãã§å€æããããã«æžãæãããããããæ¬¡ã®ããã«å€æŽããã
âLinuxã³ãã³ãã颿°åããéšåïŒä¿®æ£åŸïŒ
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args).Split(' ')
}
else {
wsl $n `$(_path_to_linux `$Args).Split(' ')
}
}"
}
âls颿°ãll颿°ïŒä¿®æ£åŸïŒ
function ls { wsl ls --color=auto $(_path_to_linux $Args).Split(' ') }
function ll { ls -l $(_path_to_linux $Args).Split(' ') }
function la { ls -a $(_path_to_linux $Args).Split(' ') }
âless颿°ïŒä¿®æ£åŸïŒ
#less
function less {
if ($Input.Length) {
$Input.Reset()
# Prepare temporary file path
$_temp = New-TemporaryFile
# Write data from pipeline to the temporary file
$Input | Out-File $_temp
# Do less
wsl less $(_path_to_linux $Args).Split(' ') $(_path_to_linux $_temp.ToString()).Split(' ')
# Delete unnecessary temporary file and variable
Remove-Item $_temp
Remove-Variable _temp
}
else {
wsl less $(_path_to_linux $Args).Split(' ')
}
}
less颿°ã«ã€ããŠã¯ããã€ãã©ã€ã³ã§åŠçããŠããéšåã«ããããæå®ãããŠããªãã£ãåŒæ°ãæå®ãããšããåŠçã远å ãããããã§ãã.Split(â â)ããæå®ããåãåé¡ãçºçããªãããã«ããŠããã
ããã§ããããŸã§çºçããŠãããšã©ãŒã調ã¹ãŠã¿ããšãå šãŠè§£æ¶ãããããšã確èªã§ããã
ããã§è€æ°åŒæ°ãåŠçã§ããããã«ãªã£ããæ¬é£èŒã§è²ãŠãŠãã$PROFILEã¯ãPowrShellããïŒWSLã§åäœããïŒLinuxãããªãã·ãŒã ã¬ã¹ã«å®è¡ã§ãããã®ã«ãªãã€ã€ããã
ææ¥ã®âææãâãå®çŸããããã«
PowerShellã¯Linuxã®ã·ã§ã«ã®ãããªäœ¿ãæ¹ãã§ããããå®éã«ã¯ãªããžã§ã¯ãæåããŒã¹ã®ããã°ã©ãã³ã°èšèªã«ã·ã§ã«ã£ãœãã·ã³ã¿ãã¯ã¹ã被ãããããªäœãã«ãªã£ãŠãããããã°ã©ãã³ã°èšèªãšããŠã®çްããèŠåããããããããããå°ãåã£ãããšããããã®ã§ããã°ããããã®èŠåããã£ã¡ãææ¡ããŠãããªããã°ãªããªãã
ãšã¯ãããå¿ èŠãªããã¥ã¡ã³ãã¯ãã§ã«ã€ã³ã¿ãŒãããäžã§å ¬éãããŠãããããçšåºŠããã°ã©ãã³ã°ã®çµéšãããã°åé¡ãªãã¯ãã ã匷åãªããã°ã©ãã³ã°èšèªãšããŠã®åŽé¢ãåããŠããããšãããã$PROFILEã§ããªãé«åºŠãªããšãŸã§èšå®ã§ããŠããŸããæåã¯èŠåŽãããããããªãããèªåã®çšéã«å¿ããŠéãããã$PROFILEã¯å¿ ãåã«ãªã£ãŠãããã¯ãã ã仿¥ã®åªåã¯ææ¥ã®âææãâã«ã€ãªããããã²ãšãã³ãã³ããš$PROFILEãéããŠãã£ãŠããã ãããã
ä»é²: $PROFILE
çŸæç¹ã§ã®$PROFILEã以äžã«æ²èŒããŠããã
#========================================================================
# Set encoding to UTF-8
#========================================================================
# [System.Console]::OutputEncoding is set to local encoding, so character
# corruption occurs when piped from WSL to WSL. Therefore, set
# [System.Console]::OutputEncoding and $OutputEncoding to UTF-8 to avoid
# the problem.
$OutputEncoding = [System.Console]::OutputEncoding =
[System.Text.UTF8Encoding]::new()
#========================================================================
# Definition of Linux commands used via wsl
#========================================================================
$_linux_path = (wsl echo '$PATH').Split(":") -NotMatch "/mnt"
$_linux_command_paths = (
wsl ls -d ($_linux_path[($_linux_path.length-1)..0] -replace "$","/*")
) 2> $null
# Generate Linux command functions
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n `$(_path_to_linux `$Args).Split(' ')
}
else {
wsl $n `$(_path_to_linux `$Args).Split(' ')
}
}"
}
Remove-Variable n
Remove-Variable _n
$_linux_functions += @'
function _path_to_linux {
$linuxpath = @()
# Convert arguments to Linux path style
ForEach($winpath in $Args) {
if ($winpath -eq $null) {
Break
}
# Change drive path to mount path
if ($winpath -match '^[A-Z]:') {
$drive = $winpath.Substring(0,1).ToLower()
$linuxpath += "/mnt/" + $drive + $winpath.Substring(2).Replace('\','/')
}
# Option is not converted
elseif ($winpath -match '^[-+]') {
$linuxpath += $winpath
}
# Other argument is converted
else {
$linuxpath += ([String]$winpath).Replace('\','/')
}
}
$linuxpath
}
'@
# Prepare temporary file path with extension .ps1
$_temp = New-TemporaryFile
$_temp_ps1 = $_temp.FullName + ".ps1"
Remove-Item $_temp
# Write function definition to temporary .ps1 file and parse
$_linux_functions | Out-File $_temp_ps1
. $_temp_ps1
Remove-Item $_temp_ps1
# Delete unnecessary variables
Remove-Variable _temp
Remove-Variable _temp_ps1
Remove-Variable _linux_path
Remove-Variable _linux_command_paths
Remove-Variable _linux_functions
#========================================================================
# Individual Linux command function definitions
#========================================================================
# grep
function grep {
$pattern_exists = $False
$path_exists = $False
$skip = $False
$i = 0
ForEach($a in $Args) {
if ($skip) {
$skip = $False
$i++
continue
}
# Options without argumetn
if ($a -cmatch '^-[abcdDEFGHhIiJLlmnOopqRSsUVvwxZ]') {
}
# Options with argument
elseif ($a -cmatch '^-[ABC]') {
$skip = $True
}
# Pattern file specification option
elseif ($a -ceq '-f') {
$skip = $True
$pattern_exists = $True
$Args[$i+1] = _path_to_linux $Args[$i+1]
}
# Pattern specification option
elseif ($a -ceq '-e') {
$skip = $True
$pattern_exists = $True
}
# Pattern or file path
elseif ($a -cnotmatch '^-') {
if ($pattern_exists) {
$path_exists = $True
}
else {
$pattern_exists = $True
}
}
$i++
}
# Change file path
if ($path_exists) {
$Args[-1] = _path_to_linux $Args[-1]
}
$Input | wsl grep $Args
}
#less
function less {
if ($Input.Length) {
$Input.Reset()
# Prepare temporary file path
$_temp = New-TemporaryFile
# Write data from pipeline to the temporary file
$Input | Out-File $_temp
# Do less
wsl less $(_path_to_linux $Args).Split(' ') $(_path_to_linux $_temp.ToString()).Split(' ')
# Delete unnecessary temporary file and variable
Remove-Item $_temp
Remove-Variable _temp
}
else {
wsl less $(_path_to_linux $Args).Split(' ')
}
}
# ls
Get-Alias ls *> $null && Remove-Item alias:ls
function ls { wsl ls --color=auto $(_path_to_linux $Args).Split(' ') }
function ll { ls -l $(_path_to_linux $Args).Split(' ') }
function la { ls -a $(_path_to_linux $Args).Split(' ') }
#========================================================================
# Alias definition
#========================================================================
Set-Alias -Name open -Value explorer
Set-Alias -Name edge -Value "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
Set-Alias -Name chrome -Value "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Get-Alias man *> $null && Remove-Item alias:man





