UNIXç³»ã®ã€ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã§ã¯ããšã€ãªã¢ã¹ã§ã³ãã³ãã®ã·ã§ãŒãã«ããå©çšã®ãããªèšå®ãè¡ãããã䜿ãã³ãã³ããç¹ã«ã¯ã³ã©ã€ããŒã§è¡ããããªåŠçã«å¥åãä»ããŠãããŠããã®å¥åã§äžé£ã®ã³ãã³ããå®è¡ãããããã ãPowerShellã«ããšã€ãªã¢ã¹ã®æ©èœã¯ããããUNIXç³»ã€ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã®ãšã€ãªã¢ã¹ãšã¯æ©èœãç°ãªããããåããããªäœ¿ãæ¹ãããããšã¯ã§ããªããPowerShellã§åæ§ã®åŠçãè¡ãããå Žåã¯ã颿°ã§å®è£ ã§ãããä»åã¯ããããäœ¿ãæ¹ã®ãµã³ãã«ã玹ä»ããã
ç¹å®ã®å Žæã«ç§»åãã
æããã䜿ãããã§ãããèšå®ã®1ã€ãã«ã¬ã³ããã£ã¬ã¯ããªã®ç§»åã ããã£ã¬ã¯ããªã®ç§»åã¯ãcdãã§è¡ããèšå®ãè¡ããªããŠãcdã«ãã¹ãæå®ããã°ç§»åã§ããããã ãããããã·ã§ãŒãã«ããèšå®ããŠããããšã§ç¹å®ã®ãã£ã¬ã¯ããªãžã®ç§»åãç°¡çŽ åããã
äŸãã°ã次ã®èšå®ãèŠãŠã¿ãããããã¯WindowsãŠãŒã¶ãŒã«ããã©ã«ãã§äœæããããã£ã¬ã¯ããªãžç§»åããããã®é¢æ°ã ã
function doc { cd ~/Documents/ }
function dwm { cd ~/Downloads/ }
function drv { cd ~/OneDrive/ }
function pic { cd ~/Pictures/ }
function vid { cd ~/Videos/ }
å®è¡ãããšæ¬¡ã®ããã«ãªãã
ç§»åããããdirãã§å 容ã確èªããããšãå€ãã®ã§ã次ã®ããã«ç§»ååŸã«dirãå®è¡ããããã«ããŠãããŠããããšæãã
function doc { cd ~/Documents/; dir }
function dwm { cd ~/Downloads/; dir }
function drv { cd ~/OneDrive/; dir }
function pic { cd ~/Pictures/; dir }
function vid { cd ~/Videos/; dir }
å®è¡ãããšæ¬¡ã®ããã«ãªãã
![]() |
ã«ã¬ã³ããã£ã¬ã¯ããªã倿ŽããåŸãdirãå®è¡ãã颿°ãå®è¡ãããµã³ãã« |
äžèšãµã³ãã«ã§ã¯Windowsã§çšæãããâãããããã£ã¬ã¯ããªâãžç§»åããŠããããå®éã«ã¯ç§»åããå Žæãä»äºãäœæ¥ã§ãã䜿ããã£ã¬ã¯ããªã«èšå®ããã ãããå€ãã®å Žåãäœæ¥ãã£ã¬ã¯ããªã¯ããããžã§ã¯ããæ¥åã«å¿ããŠåºå®åããŠããããšãå€ãããã®ãã£ã¬ã¯ããªãž2ïœ3æåã®é¢æ°ã®å®è¡ã§ç§»åã§ããããã«ãããšäŸ¿å©ã ã
ãã䜿ãã³ãã³ãããŸãšãã
ããããæ©èœã¯ããã䜿ãã³ãã³ãã®å ¥åãççž®ããç®çã§ããã䜿ããããäŸãã°ãGitã§ãœãŒã¹ã³ãŒããèšå®ãã¡ã€ã«ãä»äºã§äœ¿ããã¡ã€ã«ã®ååŸãŸãã¯ã¢ããããŒããªã©ã宿œããŠãããšããã®ã§ããã°ãgitã³ãã³ãã®æäœã«å¥åãä»ããŠãããšããæ¹æ³ããããäŸãã°ã次ã®ãããªæãã ã
gitã³ãã³ãã颿°ã«ãŸãšãã颿°
function g_pull { git pull }
function g_push { git commit -m 'updated'; git push origin master }
function g_add { git add }
å®è¡ãããšæ¬¡ã®ããã«ãªãã
ã©ããã£ããŸãšãæ¹ããããã¯äœ¿ãæ¹æ¬¡åŒã ãç¹°ãè¿ãåãã³ãã³ããå ¥åããŠããããšã«æ°ãã€ãããããã®åŠçã颿°ã«ãŸãšããŠå ¥åãå¹çåããã®ãæçã«ã€ãªããã
UNIXç³»ã®ã€ã³ã¿ã©ã¯ãã£ãã·ã§ã«ã¯å±¥æŽæ€çŽ¢æ©èœãå ¥åè£å®æ©èœã匷åãªã®ã§ãããããççž®é¢æ°ãå®çŸ©ããªããŠãå±¥æŽãè£å®æ©èœçµç±ã§ççž®å ¥åãå¯èœãªããšããããããããPowerShellã¯ãã®èŸºãã®ã€ã³ã¿ã©ã¯ãã£ããªæäœãããã»ã©äŸ¿å©ã«ã§ããããã«ãªã£ãŠããªãã颿°ã§æç€ºçã«æžããŠãããã»ããè¯ãã®ã§ã¯ãªãããšæãã
ãªãããã®æã®èšå®ã¯æ¥åãå€ãã£ãããããšå šã䜿ããªããªã£ãããããã®ã§ã宿çã«ãã§ãã¯ããŠäžèŠã«ãªã£ããåé€ãããªã©ã®æå ¥ããè¡ãã®ãããã
ãã䜿ããªãã³ãã³ãããŸãšãã
éã«ãããŸã«ãã䜿ããªãã³ãã³ããã¡ã¢ããŠããç®çã§é¢æ°ã«å®çŸ©ããŠããŸãããšããã®ãã¢ãªã ãäŸãã°ãæè¿ã§ã¯SNSããªã³ã©ã€ã³ã·ã§ããã³ã°ãåçš®ãªã³ã©ã€ã³ãµãŒãã¹ã§äœããšã¢ã«ãŠã³ãã®æ°èŠäœæãæ±ããããããšãå€ããåœç¶ãã¹ã¯ãŒãã¯åŒ·åºŠã®é«ããã®ã䜿ãå¿ èŠãããããã ãã倿°ã®ã¢ã«ãŠã³ããæã£ãŠããå Žåãæ¯åæ°ãããã¹ã¯ãŒããèããã®ã¯éªšãæããã
opensslã³ãã³ãã䜿ããšããã¹ã¯ãŒãã®ã©ã³ãã çæãªã©ã¯æ¯èŒçç°¡åã«ã§ããããopenssl rand -base64 20ãã®ããã«å®è¡ãããšãã©ã³ãã ãª20ãã€ãåã®ããŒã¿ãBASE64ã§ãšã³ã³ãŒããããããŒã¿ã衚瀺ããããããã¯ãçµæ§ãã¹ã¯ãŒãã«äœ¿ããããã
ããããæã ãã䜿ããªãã³ãã³ãã¯å¿ãããã®ã ããããã£ãé¡ã®ã³ãã³ãã¯é¢æ°ãšããŠå®çŸ©ããŠå¥åã«ããŠãããšããããµã³ãã«ãšããŠããã¹ã¯ãŒããçæãã颿°ãgetpassããäœã£ãŠã¿ããã
function getpass { openssl rand -base64 20 }
å®è¡ãããšæ¬¡ã®ããã«ãªãã
å®è¡ãããšãäžèšã¹ã¯ãªãŒã³ã·ã§ããã®ããã«ãšã©ãŒã«ãªããå®ã¯æ¬é£èŒã§äœã£ãŠããWindowsãšLinuxã®ã³ãã³ããã·ãŒã ã¬ã¹ã«çµã¿åãããããã®èšå®ã«åé¡ãããã®ã ãå ·äœçã«ã¯ã次ã®éšåã§ãšã©ãŒãçºçããŠããã
$_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(' ')
}
}"
ã$(_path_to_linux $Args)ãã®çµæãInt32ãšããŠè§£éãããããšãåé¡ã«ãªã£ãŠãããä»åã®ã±ãŒã¹ã§ã¯æåŸã20ã«ãªãã®ã§ããã®éšåã§æ°å€ãšå€æãããããã ãæ°åã«å¯ŸããŠ.Split()ãå®è¡ããããšããŠããã®ã§ãšã©ãŒã«ãªã£ãŠããã®ã§ããã
ãšããããšã§ããã®éšåãæ¬¡ã®ããã«å€æŽããã
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n ([String]`$(_path_to_linux `$Args)).Split(' ')
}
else {
wsl $n ([String]`$(_path_to_linux `$Args)).Split(' ')
}
}"
ã([String]$(_path_to_linux $Args)).Split(â â)ãã®ããã«ãçµæãæååãžå€æããŠãã.Split()ãå®è¡ããããã«å€æŽãããããã§ããããŸã§éãã«åäœããã¯ãã ãå®è¡ãããšã次ã®ããã«ãªãã
ãããªæãã§ãšãã©ã䜿ãã®ã§å¿ããã¡ãªã³ãã³ãã¯é¢æ°ã«å®çŸ©ããŠãããšããã颿°åãå¿ãããšæããããã®å Žåã«ã¯$PROFILEãèŠããŠã©ããªé¢æ°åã ã£ããæãåºãã°ããã2幎ãšã3幎ãšã䜿ã£ãŠããªãã£ãã³ãã³ãã$PROFILEã«é¢æ°ãšããŠæžããŠãã£ãã®ã§äœ¿ãæ¹ãæãåºãããšããããšãçµæ§ãããã®ã ã
ãããªæãã§$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 pagers
$_linux_pagers = @("less", "lv")
# Linux PATH and commands
$_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 commands functions
ForEach($n in $_linux_command_paths) {
$_n = (Split-Path -Leaf $n)
$_linux_functions += "
function $_n {
if (`$Input.Length) {
`$Input.Reset()
`$Input | wsl $n ([String]`$(_path_to_linux `$Args)).Split(' ')
}
else {
wsl $n ([String]`$(_path_to_linux `$Args)).Split(' ')
}
}"
}
# Generate Linux pagers functions
ForEach($_n in $_linux_pagers) {
$_linux_functions += "
function $_n {
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 $_n
wsl $_n `$(_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 $_n `$(_path_to_linux `$Args).Split(' ')
}
}"
}
# Function that converts Windows paths to Linux paths
$_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 n
Remove-Variable _n
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
}
# 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
#========================================================================
# cd shortcuts
#========================================================================
function cd1 { cd (dir -Directory)[-1] }
function cd2 { cd (dir -Directory)[-2] }
function cd3 { cd (dir -Directory)[-3] }
function cd4 { cd (dir -Directory)[-4] }
function cd5 { cd (dir -Directory)[-5] }
function cd6 { cd (dir -Directory)[-6] }
function cd7 { cd (dir -Directory)[-7] }
function cd8 { cd (dir -Directory)[-8] }
function cd9 { cd (dir -Directory)[-9] }
function cd10 { cd (dir -Directory)[-10] }
function cd11 { cd (dir -Directory)[-11] }
function cd12 { cd (dir -Directory)[-12] }
function cd13 { cd (dir -Directory)[-13] }
function cd14 { cd (dir -Directory)[-14] }
function cd15 { cd (dir -Directory)[-15] }
function cd16 { cd (dir -Directory)[-16] }
function cd17 { cd (dir -Directory)[-17] }
function cd18 { cd (dir -Directory)[-18] }
function cd19 { cd (dir -Directory)[-19] }
function cd20 { cd (dir -Directory)[-20] }
#function cd1 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-1] }
#function cd2 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-2] }
#function cd3 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-3] }
#function cd4 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-4] }
#function cd5 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-5] }
#function cd6 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-6] }
#function cd7 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-7] }
#function cd8 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-8] }
#function cd9 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-9] }
#function cd10 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-10] }
#function cd11 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-11] }
#function cd12 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-12] }
#function cd13 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-13] }
#function cd14 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-14] }
#function cd15 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-15] }
#function cd16 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-16] }
#function cd17 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-17] }
#function cd18 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-18] }
#function cd19 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-19] }
#function cd20 { cd (dir | Select-String -Pattern '[0-9]{8}$')[-20] }
function doc { cd ~/Documents/ }
function dwm { cd ~/Downloads/ }
function drv { cd ~/OneDrive/ }
function pic { cd ~/Pictures/ }
function vid { cd ~/Videos/ }
#========================================================================
# utilities shortcuts
#========================================================================
function g_pull { git pull }
function g_push { git commit -m 'updated'; git push origin master }
function g_add { git add }
function getpass { openssl rand -base64 20 }




