Coder Social home page Coder Social logo

cd-extras's Introduction

codecov cd-extras

Overview

cd-extras

Navigation helper commands

Quickly navigate backwards, forwards, upwards or between two directories

[Watch]

Navigation Helpers

cd-extras provides the following navigation helpers and corresponding aliases (shown in parens):

  • Undo-Location, (cd- or ~)
  • Redo-Location, (cd+ or ~~)
  • Step-Up, (upor ..)
  • Step-Between, (cdb)
[C:/Windows/System32]> up # or ..
[C:/Windows]> cd- # or ~
[C:/Windows/System32]> cd+ # or ~~
[C:/Windows]>

👉 That's cd- and cd+, without a space. cd - and cd + (with a space) also work but you won't get auto-completions.

Repeated uses of cd- keep moving backwards towards the beginning of the stack rather than toggling between the two most recent directories as in vanilla bash. Use Step-Between (cdb) if you want to toggle between directories.

[C:/Windows/System32]> cd ..
[C:/Windows]> cd ..
[C:/]> cd-
[C:/Windows]> cd-
[C:/Windows/System32]> cd+
[C:/Windows]> cd+
[C:/]> cdb
[C:/Windows]> cdb
[C:/]>

Parameters

up, cd+ and cd- each take a single optional argument: either a number of steps, n...

[C:/Windows/System32]> .. 2 # or `up 2`
[C:/]> cd temp
[C:/temp]> cd- 2 # `cd -2`, `~ 2` or just `~2` also work
[C:/Windows/System32]> cd+ 2
[C:/temp]>

...or a string, NamePart, used to select the nearest matching directory from the available locations. Given a NamePart, cd-extras will search from the current location for directories whose leaf name contains the given string⁽¹⁾. If none is found then it will attempt to find a match within the full path of each candidate directory⁽²⁾.

[C:/Windows]> cd system32
[C:/Windows/System32]> cd drivers
[C:/Windows/System32/drivers]> cd- win # [ex. 1] by leaf name
[C:/Windows/]> cd+ 32/dr # [ex. 2] by full name
[C:/Windows/System32/drivers]> up win # by leaf name again
[C:/Windows]>

Output

Each helper includes a -PassThru switch to return a PathInfo value in case you need a reference to the resulting directory. The value will be $null if the action wasn't completed. (For example, if there was nothing in the stack or you attempted to navigate up from the root.)

[C:/Windows/System32]> up -PassThru

Path
----
C:\Windows

[C:/Windows]> cd- -PassThru

Path
----
C:\Windows\System32

[C:/Windows/System32]>

Completions

Auto-completions are provided for each of cd-, cd+, and up.

Assuming the PSReadLine MenuComplete function is bound to tab...

[C:]> Get-PSReadLineKeyHandler -Bound | Where Function -eq MenuComplete
Completion functions
====================

Key           Function     Description
---           --------     -----------
Tab           MenuComplete Complete the input if there is a single completion ...
...

...then tabbing () through any of the navigation helpers will display a menu based completion.

[C:/Windows/System32/drivers/etc]> up ⇥⇥
[C:/Windows/System32/drivers/etc]> up 2

1. drivers  2. System32  3. Windows  4. C:\
            ────────────

C:\Windows\System32

The IndexedCompletion option controls how completion text is displayed. When IndexedCompletion is on and more than one completion is available, the completions offered are the indices of each corresponding directory; the directory name is displayed in the menu below. The full directory path is given in the tooltip if you have PSReadLine tooltips enabled.

cd-extras detects PSReadLine options in order to set IndexedCompletion at startup. If the PSReadLine MenuComplete option is bound to at least one key combination then IndexedCompletion is turned on by default. You can turn it off if you prefer.

[C:/Windows/System32/drivers/etc]> setocd IndexedCompletion 0
[C:/Windows/System32/drivers/etc]> up ⇥⇥
[C:/Windows/System32/drivers/etc]> up C:\Windows\System32 <--- full path shown

1. drivers  2. System32  3. Windows  4. C:\
            ────────────

C:\Windows\System32

It's also possible to tab-complete cd-, cd+ and up using a partial directory name (i.e. the NamePart parameter).

[~/projects/PowerShell/src/Modules/Shared]> up pr⇥
[~/projects/PowerShell/src/Modules/Shared]> up '~\projects'
[~/projects]>

Listing available navigation targets

As an alternative to menu completion you retrieve a list of available targets with:

  • Get-Stack -Undo (dirs -u)
  • Get-Stack -Redo (dirs -r)
  • Get-Ancestors (xup)
[C:/Windows/System32/drivers]> Get-Ancestors # xup

n Name        Path
- ----        ----
1 System32    C:\Windows\System32
2 Windows     C:\Windows
3 C:\         C:\

[C:/Windows/System32/drivers]> up 2
[C:/Windows]> up 1
[C:/]> dirs -u # dirs -v also works

n Name        Path
- ----        ----
1 Windows     C:\Windows
2 drivers     C:\Windows\System32\drivers

[C:/]> cd- 2
[C:/Windows/System32/drivers]>

cd enhancements

Shortcuts and tab completions for the cd command

[Watch]

Navigation Helpers

cd-extras provides a proxy to Set-Location - called Set-LocationEx - and aliases it to cd by default, giving it several new abilities:

Path shortening

If an unambiguous match is available then cd can change directory using an abbreviated path. This effectively changes a path given as, p into p* or ~/pr/pow/src into ~/pr*/pow*/src*. If you're not sure whether an unambiguous match is available then just hit tab to pick from a list of potential matches instead.

[~]> cd pr
[~/projects]> cd cd-e
[~/projects/cd-extras]> cd ~
[~]> cd pr/cd
[~/projects/cd-extras]>

Word delimiters (., _, - by default) are expanded around so a segment containing .sdk is expanded into *.sdk*.

[~]> cd proj/pow/s/.sdk
[~/projects/powershell/src/Microsoft.PowerShell.SDK]>

👉 Powershell interprets a hyphen at the start of an argument as a parameter name. So while you can do this...

[~/projects/powershell]> cd src/-unix
[~/projects/PowerShell/src/powershell-unix]>

... you need to escape this:

[~/projects/powershell/src]> cd -unix
Set-LocationEx: A parameter cannot be found that matches parameter name 'unix'.

[~/projects/powershell/src]> cd `-unix # backtick escapes the hypen
[~/projects/PowerShell/src/powershell-unix]>

Pairs of periods are expanded between so, for example, a segment containing s..32 is expanded into s*32.

[~]> cd /w/s..32/d/et
[C:/Windows/System32/drivers/etc]>

Directories in CD_PATH will be also be shortened.

[C:/]> setocd CD_PATH ~/projects
[C:/]> cd p..shell
[~/projects/PowerShell/]>

AUTO_CD uses the same expansion algorithm when enabled.

[~]> $cde.AUTO_CD
True

[~]> /w/s/d/et
[C:/Windows/System32/drivers/etc]> ~/pr/pow/src
[~/projects/PowerShell/src]> .sdk
[~/projects/PowerShell/src/Microsoft.PowerShell.SDK]>

Multi-dot cd

In the same way that you can navigate up one level with cd .., Set-LocationEx supports navigating multiple levels by adding additional dots. AUTO_CD works the same way if enabled.

[C:/Windows/System32/drivers/etc]> cd ... # same as `up 2` or `.. 2`
[C:/Windows/System32]> cd-
[C:/Windows/System32/drivers/etc>] cd .... # same as `up 3` or `.. 3`
[C:/Windows]>

No argument cd

If the NOARG_CD option is defined then cd without arguments navigates into that directory (~ by default). This overrides the out of the box behaviour on PowerShell>=6.0, where no-arg cd always navigates to ~ and PowerShell < 6.0, where no-argument cd does nothing at all.

[~/projects/powershell]> cd
[~]> setocd NOARG_CD /
[~]> cd
[C:/]>

Two argument cd

Replaces all instances of the first argument in the current path with the second argument, changing to the resulting directory if it exists, using the Switch-LocationPart function.

You can also use the alias cd: or the explicit ReplaceWith parameter of Set-LocationEx.

[~/Modules/Unix/Microsoft.PowerShell.Utility]> cd unix shared
[~/Modules/Shared/Microsoft.PowerShell.Utility]> cd: -Replace shared -With unix
[~/Modules/Unix/Microsoft.PowerShell.Utility]> cd unix -ReplaceWith shared
[~/Modules/Shared/Microsoft.PowerShell.Utility]>

Enhanced completion for cd and others

cd-extras provides enhanced completion for cd, pushd, ls, Get-Item and Invoke-Item by default, expanding all path segments in one go so that you don't have to individually tab (⇥) through each one. The path shortening logic is provided by Expand-Path and works as described above.

[~]> cd /w/s/dr⇥⇥
[~]> cd C:\Windows\System32\DriverState\

drivers   DriverState   DriverStore
          ───────────

C:\Windows\System32\DriverState

Paths within $cde.CD_PATH are included in the completion results.

[~]> $cde.CD_PATH += '~\Documents\'
[~]> cd win/mod⇥
[~]> ~\Documents\WindowsPowerShell\Modules\█

👉 The total number of completions offered is limited by the MaxCompletions option (or calculated dynamically to fit the screen if MaxCompletions is falsy). Although the completions are sorted by type (folders first) and then by name for ease of reading, that sort is applied after the limit has been applied to the original results. Those results are sorted breadth first for responsiveness.

A console beep is emitted in cases where the available results have been truncated.

Single and double periods

Word delimiters (., _, - by default) are expanded around so, for example, a segment containing .sdk is expanded into *.sdk*.

[~]> cd proj/pow/s/.sdk⇥
[~]> cd ~\projects\powershell\src\Microsoft.PowerShell.SDK\█

or

[~]> ls pr/pow/t/ins.sh⇥
[~]> ls ~\projects\powershell\tools\install-powershell.sh
[~]> ls ~\projects\powershell\tools\install-powershell.sh | cat
#!/bin/bash
...

[~]>

A double-dot (..) token is expanded inside, so s..32 becomes s*32.

[~]> ls /w/s..32⇥
[~]> ls C:\Windows\System32\█

Multi-dot completions

The multi-dot syntax provides tab completion into ancestor directories.

[~/projects/powershell/docs/git]> cd ...⇥
[~/projects/powershell/docs/git]> cd ~\projects\powershell\█
[C:/projects/powershell/docs/git]> cd .../⇥

.git     .vscode    demos    docs   test
─────
.github    assets   docker   src    tools

~\projects\powershell\.git

Variable based completions

When CDABLE_VARS is enabled, completions are available for the names of variables that contain file paths. This can be combined with the -Export option of Get-Ancestors (xup), which recursively exports each parent directory's path into a global variable with a corresponding name.

[C:/projects/powershell/src/Modules/Unix]> xup -Export -ExcludeRoot

n Name        Path
- ----        ----
1 Modules     C:\projects\powershell\src\Modules
2 src         C:\projects\powershell\src
3 powershell  C:\projects\powershell
4 projects    C:\projects

[C:/projects/powershell/src/Modules/Unix]> up pow
[C:/projects/powershell]> cd mod⇥
[C:/projects/powershell]> cd .\src\modules\

Extending completion to other commands

You can extend the list of commands that participate in enhanced completion for either directories or files, or for both files and directories, using the DirCompletions FileCompletions and PathCompletions options respectively.

(FileCompletions is the least useful of the three since you can't tab through intermediate directories to get to the file you're looking for.)

[~]> setocd DirCompletions mkdir
[~]> mkdir ~/pow/src⇥
[~]> mkdir ~\powershell\src\█
[~]> setocd PathCompletions Copy-Item
[~]> cp /t/⇥
[~]> cp C:\temp\subdir\█
subdir  txtFile.txt  txtFile2.txt
──────

C:\temp\subdir

In each case, completions work against the target's Path parameter; if you want enhanced completion for a native executable or for a cmdlet without a Path parameter then you'll need to provide a wrapper. Either the wrapper or the target itself should handle expanding ~ where necessary.

[~]> function Invoke-VSCode($path) { &code (rvpa $path) }
[~]> setocd PathCompletions Invoke-VSCode
[~]> Set-Alias co Invoke-VSCode
[~]> co ~/pr/po⇥
[~]> co ~\projects\powershell\█

An alternative to registering a bunch of aliases is to create a tiny wrapper to pipe input from ls, gi or xpa.

[~]> function to($target) { &$target $input }
[~]> xpa ~/pr/po/r.md⇥
[~]> xpa ~/projects/powershell/readme.md | to bat

───────────────────────────────────────────────────────
File: C:\Users\Nick\projects\PowerShell\README.md
───────────────────────────────────────────────────────
1 | ...
2 | ...

👉 You can skip tab completion altogether and use Expand-Path directly if you know exactly what you're looking for.

[~]> xpa ~/pr/po/r.md | to bat

───────────────────────────────────────────────────────
File: C:\Users\Nick\projects\PowerShell\README.md
───────────────────────────────────────────────────────
1 | ...
2 | ...

Colourised completions

The ColorCompletion option enables colourisation of completions in the filesystem provider via DirColors or via your own global Format-ColorizedFilename function of type [System.IO.FileSystemInfo] -> [String].

👉 ColorCompletion is off by default. Enable it on with setocd ColorCompletion.

AUTO CD

Change directory without typing cd

[Watch]

AUTO_CD

[~]> projects
[~/projects]> cd-extras
[~/projects/cd-extras]> /
[C:/]>

As with the enhanced cd command, abbreviated paths and multi-dot syntax are supported.

[~]> pr
[~/projects]> cd-e
[~/projects/cd-extras]> cd
[~]> pr/cd
[~/projects/cd-extras]>

Tilde

AUTO_CD supports a shorthand syntax for cd- using tilde (~). You can use this with or without a space between tilde and the number, although tab completion only works after a space (~ ⇥).

[C:/Windows/System32]> /
[C:/]> temp
[C:/temp]> dirs -u

n Name      Path
- ----      ----
0 temp      C:\temp
1 C:\       C:\
2 System32  C:\Windows\System32

[C:/temp]> ~2 # or ~ 2
[C:/Windows/System32]> ~~2 # or ~~ 2
[C:/temp]>

Multi-dot

Multi-dot syntax works with AUTO_CD as an alternative to up [n].

[C:/Windows/System32/drivers/etc]> ... # same as `up 2` or `.. 2`
[C:/Windows/System32]> cd-
[C:/Windows/System32/drivers/etc>] .... # same as `up 3` or `.. 3`
[C:/Windows]>

CD PATH

Additional base directories for the cd command

[~]> setocd CD_PATH ~/documents
[~]> # or $cde.CD_PATH = ,'~/documents'
[~]> cd WindowsPowerShell
[~/documents/WindowsPowerShell]>

Tab-completion, path shortening and Expand-Path work with CD_PATH directories.

CD_PATHs are not searched when an absolute or relative path is given.

[~]> setocd CD_PATH ~/documents
[~]> cd ./WindowsPowerShell
Set-Location : Cannot find path '~\WindowsPowerShell'...

👉 Unlike bash, the current directory is always included when a relative path is used. If a child with the same name exists in both the current directory and a CD_PATH directory then cd will prefer the former.

[~]> mkdir -f child, someDir/child
[~]> resolve-path someDir | setocd CD_PATH
[~]> cd child
[~/child]> cd child
[~/someDir/child]>

👉 The value of CD_PATH is an array, not a delimited string as it is in bash.

[~]> setocd CD_PATH ~/Documents/, ~/Downloads
[~]> $cde.CD_PATH
~/Documents
~/Downloads

CDABLE VARS

cd into variables without the $ and enable tab completion into child directories

Given a variable containing a folder path (configured in your $PROFILE, perhaps, or by invoking Get-Ancestors -Export), you can cd into it using the variable name.

👉 CDABLE_VARS is off by default; enable it with, setocd CDABLE_VARS.

[~/projects/powershell]> setocd CDABLE_VARS
[~/projects/powershell]> $bk1 = $pwd
[~/projects/powershell]> cd
[~]> cd bk1
[~/projects/powershell]>

It works with relative paths too, so if you find yourself frequently cding into the same subdirectories you could create a corresponding variable.

[~/projects/powershell]> $gh = './.git/hooks'
[~/projects/powershell]> cd gh
[~/projects/powershell/.git/hooks]>

You can combine it with AUTO_CD for great good:

[C:/projects/powershell/src/Modules/Unix]> xup -Export | out-null
[C:/projects/powershell/src/Modules/Unix]> projects
[C:/projects]> src
[C:/projects/powershell/src]>

Additional helpers

Get-Up (gup)

Gets the path of an ancestor directory, either by name or by number of levels (n), returning the parent of the current directory by default. It supports consuming values from the pipeline so you can do things like:

[C:/projects]> # find git repositories
[C:/projects]> ls .git -Force -Depth 2 | gup
C:\projects\cd-extras
C:\projects\work\app
...

[C:/projects]> # find chocolatey root directory
[C:/projects]> gcm choco | gup 2
C:\ProgramData\chocolatey

Get-Stack (dirs)

View contents of undo (cd-) and redo (cd+) stacks.

Use dirs -u for an indexed list of undo locations, dirs -r for a corresponding list of redo locations, or just dirs to see both.

Clear-Stack (dirsc)

Clear contents of undo (cd-) and/or redo (cd+) stacks.

Expand-Path (xpa)

Expands a candidate path by inserting wildcards between each segment. Use a trailing slash to expand children of the matched path(s). Contents of CD_PATH will be included.

👉 The expansion may match more than you expect. Test the output before piping it into a potentially destructive command.

Compatibility

OS X & Linux

cd-extras works on non-Windows operating systems. The IndexedCompletion option is off by default unless you configured PSReadLine with a MenuComplete keybinding before importing cd-extras.

Set-PSReadLineKeyHandler -Key Tab -Function MenuComplete

Otherwise you can enable cd-extras menu completions manually with:

setocd IndexedCompletion

Alternative providers

cd-extras is primarily intended to work against the filesystem provider but it should work with other providers too.

[~]> cd hklm:\
[HKLM:]> cd so/mic/win/cur/windowsupdate
[HKLM:/SOFTWARE/Microsoft/Windows/CurrentVersion/WindowsUpdate]> ..
[HKLM:/SOFTWARE/Microsoft/Windows/CurrentVersion]> cd-
[HKLM:/SOFTWARE/Microsoft/Windows/CurrentVersion/WindowsUpdate]> cd- 2
[~]>

Install

From the gallery

Install-Module cd-extras
Import-Module cd-extras

# add to profile. e.g:

Add-Content $PROFILE `n, 'Import-Module cd-extras'

or get the latest from github

git clone git@github.com:nickcox/cd-extras.git
Import-Module cd-extras/cd-extras/cd-extras.psd1 # yep, three :D

Configure

cd-extras options

  • AUTO_CD: [bool] = $true
    • Enables auto_cd.
  • CDABLE_VARS: [bool] = $false
    • Enables cdable_vars.
  • NOARG_CD: [string] = '~'
    • If specified, cd with no arguments will change into the given directory.
  • CD_PATH: [string[]] = @()
    • Paths to be searched by cd and tab completion. An array, not a delimited string.
  • WordDelimiters : [string[]] = '.', '_', '-'
    • Word boundaries within path segments. For example, .foo will be expanded into *.foo*.
  • ToolTip : [ScriptBlock] = { param ($item, $isTruncated) ... }
    • Information displayed in the menu-completion tooltip. This is passed two arguments: the current item, and a boolean indicating whether the list of completions has been truncated. It should return a string.
  • IndexedCompletion: [bool] = $true (if MenuComplete key bound)
    • If truthy, indexes are offered as completions for up, cd+ and cd- with full paths displayed in the menu.
  • DirCompletions: [string[]] = 'Set-Location', 'Set-LocationEx', 'Push-Location'
    • Commands that participate in enhanced tab completion for directories.
  • PathCompletions: [string[]] = 'Get-ChildItem', 'Get-Item', 'Invoke-Item', 'Expand-Path'
    • Commands that participate in enhanced tab completion for any path (files or directories).
  • FileCompletions: [string[]] = @()
    • Commands that participate in enhanced tab completion for files.
  • ColorCompletion : [bool] = false
    • When truthy, dir/path/file completions will be coloured by Format-ColorizedFilename, if available.
  • MaxMenuLength : [int] = 35
    • Truncate completion menu items to this length.
  • MaxCompletions : [int] = 0
    • Limit the number of menu completions offered. If falsy then cd_extras will attempt to calculate the maximum number of completions that can fit on the screen given the current $Host.UI.RawUI.WindowSize and $cde.MaxMenuLength. Otherwise should be no greater than (Get-PSReadLineOption).CompletionQueryItems.

To configure cd-extras create a hashtable, cde, with one or more of these keys before importing it:

$cde = @{
  AUTO_CD = $false
  CD_PATH = '~/Documents/', '~/Downloads'
}

Import-Module cd-extras

or call the Set-CdExtrasOption (setocd) function after importing the module:

Import-Module cd-extras

setocd PathCompletions Invoke-VSCode # appends PathCompletions
setocd CDABLE_VARS # turns CDABLE_VARS on
setocd AUTO_CD 0 # turns AUTO_CD off
setocd MaxCompletions 0 # auto calculate the maximum number of completions to display

# append the mode string for each item to the completion tooltip
setocd ToolTip { "$($args[0]) ($($args[0].Mode))" }

👉 If you want to opt out of the default completions then you should do it before cd-extras is loaded since PowerShell doesn't provide a way to unregister argument completers.

$cde = @{ DirCompletions = @() }
Import-Module cd-extras

Navigation helper key handlers

If you want to bind navigation helpers to PSReadLine key handlers then you'll probably want to redraw the prompt after navigation.

function invokePrompt() { [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt() }
@{
  'Alt+^'         = { if (up  -PassThru) { invokePrompt } }
  'Alt+['         = { if (cd- -PassThru) { invokePrompt } }
  'Alt+]'         = { if (cd+ -PassThru) { invokePrompt } }
  'Alt+Backspace' = { if (cdb -PassThru) { invokePrompt } }
}.GetEnumerator() | % { Set-PSReadLineKeyHandler $_.Name $_.Value }

Using a different alias

cd-extras aliases cd to its proxy command, Set-LocationEx. If you want a different alias then you'll probably want to restore the original cd alias too.

[~]> set-alias cd set-location -Option AllScope
[~]> set-alias cde set-locationex
[~]> cde /w/s/d/et
[C:/Windows/System32/drivers/etc]> cd- # still cd-, not cde-
[~]>

👉 cd-extras will only remember locations visited via Set-LocationEx or its alias.

[~]> dirs -u

[~]> Set-Location code
[~/code]> cd-
[~/code]>

cd-extras's People

Contributors

csr-nick avatar ja-nick avatar jamesmcgill avatar jetersen avatar nickcox avatar qbikez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cd-extras's Issues

Add a set of regression tests

As these small scripts become more involved, we're going to need a set of tests to ensure that nothing gets broken in the course of fixing or improving something else.

`cd` without argument has no function

Traditional Unix shells change to the user's home directory when using cd without argument. In its current implementation cd just doesn't do anything.

I propose to add functionality:

  • change to $HOME or
  • show the directory stack (like Pscx does)
  • (something else which gives the user benefit)

[Suggestion] Mention PSReadLine as a dependency.

For some reason PSReadLine wasn't installed on my machine (win10) but I saw it's required for a bit of the functionality. I quickly fixed that by installing it.

Maybe you could mention in the readme somewhere that it's required and have a one liner to install the module.

Prep for 1.0

  • Enable clearing of stacks
  • Provide FileCompletions option similar to DirCompletions but for files and PathCompletions for commands that can work on either directories or files
  • Consider an option to recover discarded redo stacks maybe later
  • Consider running tests through a profiler

cd-extras is not powershell v5 compatible

PowerShellVersion = '5.0'

As far as I know the ? operator is not supported, this is only supported in powershell 7.

This:

$invocation = $isUnderTest ? $CommandName : $MyInvocation.Line

Has to be written like this

$invocation = if ($isUnderTest) { $CommandName } else { $MyInvocation.Line }
% : At C:\Users\joseph\Documents\WindowsPowerShell\Modules\cd-extras\2.9.1\private\CommandNotFound.ps1:10 char:32
+     $invocation = $isUnderTest ? $CommandName : $MyInvocation.Line
+                                ~
Unexpected token '?' in expression or statement.
At C:\Users\joseph\Documents\WindowsPowerShell\Modules\cd-extras\2.9.1\cd-extras.psm1:3 char:45
+ Get-ChildItem $PSScriptRoot/private/*.ps1 | % { . $_.FullName }
+                                             ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [ForEach-Object], ParseException
    + FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.ForEachObjectCommand

Import-Module : The module to process 'cd-extras.psm1', listed in field 'ModuleToProcess/RootModule' of module manifest
'C:\Users\joseph\Documents\WindowsPowerShell\Modules\cd-extras\2.9.1\cd-extras.psd1' was not processed because no valid module was found in any module directory.
At C:\Users\joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:30 char:11
+           Import-Module $moduleName
+           ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (cd-extras:String) [Import-Module], PSInvalidOperationException
    + FullyQualifiedErrorId : Modules_ModuleFileNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

The property 'CD_PATH' cannot be found on this object. Verify that the property exists and can be set.
At C:\Users\joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:328 char:1
+ $cde.CD_PATH = @($developmentWorkspace)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

Step-Up (..) errors cd'ing to drive root on Windows

Latest version of cd-extras

┌( thorsten@rednails) ( F:/cygwin)
└( powershell)❯ ..
Set-Location: C:\Users\thorsten\Documents\PowerShell\Modules\cd-extras\2.9.2\public\Step-Up.ps1:50
Line |
  50 |      Set-LocationEx -LiteralPath $target -PassThru:$PassThru
     |      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find path 'F' because it does not exist.

Can't set option NOARG_CD

> Set-CdExtrasOption -Option NOARG_CD -Value '~'
Set-CdExtrasOption : Cannot validate argument on parameter 'Option'. The argument "NOARG_CD" does not belong to the set "AUTO_CD;CD_PATH" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.
At line:1 char:28
+ Set-CdExtrasOption -Option NOARG_CD -Value '~'
+                            ~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [Set-CdExtrasOption], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Set-CdExtrasOption

Warning when `#Requires` used

When cd-extras is used in connection with #Requires -Module cd-extras a warning issued:

WARNING: The names of some imported commands from the module 'cd-extras' include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.

The warning is described in the Readme but since we are not manually importing cd-extras, the workaround is not applicable.

PathCompletion results should be bounded

Path completion can be slow when returning thousands of results. The number of completion results should be limited (50-60, perhaps) and, ideally, configurable.

Set-Alias : The AllScope option cannot be removed from the alias '~'.

When doing a default installation from the Gallery, I received the previous error after importing the module from my $PROFILE.
After commenting line 13 of the Aliases.ps1 file, the error goes away, but ~ doesn't work as expected from the module.

Changing the line from:

Set-Alias '~' Undo-Location

to:
Set-Alias -Name '~' -Value Undo-Location -Option AllScope

Seems to solve the problem

No error changing to unquoted directory with space in name

This is possibly related to #3:

[thorsten] > cd directory with space
[thorsten] > pwd

Path
----
F:\cygwin\home\thorsten

Standard behaviour:

[thorsten] > cd directory with space
Set-Location : A positional parameter cannot be found that accepts argument 'with'.
At line:1 char:1
+ cd directory with space
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Set-Location], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetLocationCommand```

Can't change into directory with space in name

[~] pwsh> cd '.\directory with'

cmdlet Transpose-Location at command pipeline position 1
Supply values for the following parameters:
With:

It looks llike the problem is confined to directory names consisting of two words. The heuristic mentioned in #4 seems not to work properly.

cd-extras breaks tab expansion of UNC paths

Without cd-extras being loaded, in PowerShell 7.1.3, if I enter

gci \\pnjnas\p\photos\class

and hit the Tab key I get:

image

If cd-extras (version 2.2.0) is loaded, hitting the Tab key in the same context does nothing, and a (caught) exception is raised:

PS C:\Users\<REDACTED>> $error[0] | select *

PSMessageDetails      :
Exception             : System.Management.Automation.MethodInvocationException: Exception calling "CompleteInput" with
                        "3" argument(s): "Object reference not set to an instance of an object."
                         ---> System.NullReferenceException: Object reference not set to an instance of an object.
                           at
                        System.Management.Automation.CompletionCompleters.InvokeScriptArgumentCompleter(ScriptBlock
                        scriptBlock, Object[] argumentsToCompleter, List`1 result)
                           at
                        System.Management.Automation.CompletionCompleters.InvokeScriptArgumentCompleter(ScriptBlock
                        scriptBlock, String commandName, String parameterName, String wordToComplete, CommandAst
                        commandAst, CompletionContext context, List`1 resultList)
                           at System.Management.Automation.CompletionCompleters.NativeCommandArgumentCompletion(String
                        commandName, CompiledCommandParameter parameter, List`1 result, CommandAst commandAst,
                        CompletionContext context, Dictionary`2 boundArguments)
                           at System.Management.Automation.CompletionCompleters.ProcessParameter(String commandName,
                        CommandAst commandAst, CompletionContext context, List`1 result,
                        MergedCompiledCommandParameter parameter, Dictionary`2 boundArguments)
                           at System.Management.Automation.CompletionCompleters.GetArgumentCompletionResultsWithSuccess
                        fulPseudoBinding(CompletionContext context, ArgumentLocation argLocation, CommandAst
                        commandAst)
                           at
                        System.Management.Automation.CompletionCompleters.CompleteCommandArgument(CompletionContext
                        context)
                           at System.Management.Automation.CompletionAnalysis.GetResultForIdentifier(CompletionContext
                        completionContext, Int32& replacementIndex, Int32& replacementLength, Boolean isQuotedString)
                           at System.Management.Automation.CompletionAnalysis.GetResultHelper(CompletionContext
                        completionContext, Int32& replacementIndex, Int32& replacementLength, Boolean isQuotedString)
                           at System.Management.Automation.CompletionAnalysis.GetResults(PowerShell powerShell, Int32&
                        replacementIndex, Int32& replacementLength)
                           at System.Management.Automation.CommandCompletion.CompleteInputImpl(Ast ast, Token[]
                        tokens, IScriptPosition positionOfCursor, Hashtable options)
                           at System.Management.Automation.CommandCompletion.CompleteInput(String input, Int32
                        cursorIndex, Hashtable options)
                           at CallSite.Target(Closure , CallSite , Type , String , Int32 , Hashtable )
                           --- End of inner exception stack trace ---
                           at
                        System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception
                        exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo)
                           at CallSite.Target(Closure , CallSite , Type , String , Int32 , Hashtable )
                           at System.Management.Automation.Interpreter.DynamicInstruction`5.Run(InterpretedFrame frame)
                           at
                        System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                        frame)
TargetObject          :
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : NullReferenceException
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock><End>, <No file>: line 38
                        at PSConsoleHostReadLine,
                        C:\Users\<REDACTED>\OneDrive\Documents\PowerShell\Modules\PSReadLine\2.2.0\PSReadLine.psm1: line 4
PipelineIterationInfo : {}

Tab completion for directories gives full path and no quotes

Trying tab completion for a directory gives full path and no quotes. While the former is debatable, the latter is clearly a bug:

cd-extras:

[thorsten] > cd dir<TAB>
[thorsten] > cd F:\cygwin\home\thorsten\directory with space[ENTER]
[thorsten] > pwd

Path
----
F:\cygwin\home\thorsten

Standard behaviour:

[thorsten] > cd dir<TAB>
[thorsten] > cd '.\directory with space'[ENTER]
[directory with space] > pwd

Path
----
F:\cygwin\home\thorsten\directory with space

cd to invalid subfolder moved to root of drive

After installing this module (v0.8.1), cd to a non-existant dir moves to the root of the current drive, with no warning or error

PS C:\Users\username\Documents> cd not_a_dir
PS C:\>

I would expect it to stay in the current directory and report an error (as vanilla powershell does)

PS C:\Users\username\Documents> cd not_a_dir
cd : Cannot find path 'C:\Users\username\Documents\not_a_dir' because it does not exist.
At line:1 char:1
+ cd not_a_dir
+ ~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\username\Documents\not_a_dir:String) [Set-Location], Ite
   mNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS C:\Users\username\Documents>

Prep 3.0 Release

  • Break up docs
  • Implement pluggable freceny providers
  • Document pluggable frecency providers
  • Check for regressions in startup perf
  • Test coverage
  • Fix any outstanding bugs
  • Address annoying Pester legacy syntax warning

$cde redefined by import module

Hi, looks like something broken with $cde config

Install-Module -Name cd-extras -AllowPrerelease

In $profile:

$cde = @{
    AUTO_CD  = $true
    CD_PATH  = 'q:\\code\\Telgorithm', 'q:\\code\\vchirikov'
    NOARG_CD = 'q:\\code'
}
Import-Module cd-extras

output:

$cde

AUTO_CD               : True
CDABLE_VARS           : False
CD_PATH               : {}
NOARG_CD              : ~
RECENT_DIRS_FILE      :
RECENT_DIRS_EXCLUDE   : {}
RecentDirsFallThrough : True
MaxRecentDirs         : 120
MaxRecentCompletions  : 60
MaxCompletions        : 0
MaxMenuLength         : 36
WordDelimiters        : {., _, -}
DirCompletions        : {Set-Location, Set-LocationEx, Push-Location}
PathCompletions       : {Get-ChildItem, Get-Item, Invoke-Item, Expand-Path}
FileCompletions       : {}
ColorCompletion       : False
IndexedCompletion     : True
ToolTip               :  param ($item, $isTruncated)
                            "{0} $(if ($isTruncated) {'{1}'})" -f
                            $item, "$([char]27)[3m(+additional results not displayed)$([char]27)[0m"

As you can see NOARG_CD redefined to the wrong value (and other values too) and doesn't work :(

No indication that transpose location failed

Shouldn't there be an error (or at least information) if no transformation could be done? Obviously a user issuing the command expects something to happen:

[thorsten] > pwd

Path
----
F:\cygwin\home\thorsten


[thorsten] > cd thorsten guest
[thorsten] > pwd

Path
----
F:\cygwin\home\thorsten

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.