Skip to content

Instantly share code, notes, and snippets.

@belotn
Created July 25, 2022 14:10
Show Gist options
  • Save belotn/177bf10f5bca3ef6b97e8e03bda71c37 to your computer and use it in GitHub Desktop.
Save belotn/177bf10f5bca3ef6b97e8e03bda71c37 to your computer and use it in GitHub Desktop.
Set alt+j alt+k alt+l alt+h to move in directory, the both first one in directory structure, the both last in the directory history
Set-PSReadLineKeyHandler -Chord "Alt+k" -ViMode Insert `
-ScriptBlock {
param($Key, $Arg)
$Level = 1
[Microsoft.PowerShell.PSConsoleReadLine]::TryGetArgAsInt($Arg, `
[ref]$Level,1)
Set-Location -Path (( 1..$Level |% { '..'}) -join '\') } `
-Description "Move to Parent Directory"
Set-PSReadLineKeyHandler -Chord "Alt+k" -ViMode Command `
-ScriptBlock {
param($Key, $Arg)
$Level = 1
[Microsoft.PowerShell.PSConsoleReadLine]::TryGetArgAsInt($Arg, `
[ref]$Level,1)
Set-Location -Path (( 1..$Level |% { '..'}) -join '\') } `
-Description "Move to Parent Directory"
Set-PSReadLineKeyHandler -Chord "Alt+j" -VIMode Insert `
-ScriptBlock {
$Cursor = $Null
$Line = $Null
[Microsoft.PowerShell.PSConsoleReadline]::GetBufferState([ref]$Line,`
[ref]$Cursor)
(Get-Childitem "$Line*" -Attribute Directory )[0].FullName | Set-Location
if($PSVersionTable.PSVersion.Major -eq 7){
Set-PSReadLineKeyHandler -Chord "Alt+l" -VIMode Insert `
-ScriptBlock { cd + } -Description "Move to the next directory in history"
Set-PSReadLineKeyHandler -Chord "Alt+h" -VIMode Insert `
-ScriptBlock { cd - } -Description "Move to the previous directory in history"
Set-PSReadLineKeyHandler -Chord "Alt+l" -VIMode Command `
-ScriptBlock {
param($Key, $Arg)
[int]$Level = 1
[Microsoft.PowerShell.PSConsoleReadLine]::TryGetArgAsInt($Arg, `
[ref]$Level,1)
for($i=0;$i -lt $Level; $i++){ cd + }
} -Description "Move to the next directory in history"
Set-PSReadLineKeyHandler -Chord "Alt+h" -VIMode Command `
-ScriptBlock {
param($Key, $Arg)
[int]$Level = 1
[Microsoft.PowerShell.PSConsoleReadLine]::TryGetArgAsInt($Arg, `
[ref]$Level,1)
for($i=0;$i -lt $Level; $i++) { cd - }
} -Description "Move to the previous directory in history"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment