DEV Community

loading...
Cover image for Add a Bash-like autocomplete to your PowerShell

Add a Bash-like autocomplete to your PowerShell

ofhouse profile image Felix Haus ・3 min read

I mostly use Ubuntu or MacOS for development, but recently I switched sides because I need the power from my Windows gaming machine to start playing around with Blender and Unreal Engine.

What I missed much when using the PowerShell console was the autocomplete ability which Bash offers:

  • When you begin to type a command and hit Tab Bash shows you all available commands which begin with the phrase you typed and you can then select the one you want with the arrow keys.

    PowerShell by default only completes the command immediately without showing you all available commands.

  • If you already started typing the beginning of a command you can use / to cycle through your history to autocomplete the command with the parameters you have used before.

    Cycling through the history with the arrow keys is also supported by PowerShell but it completely ignores what you have already typed and replaces everything with the last used command from the history.

Enable better autocompletion in your PowerShell

Autocomplete in PowerShell

Thankfully PowerShell already has this functionality built-in provided by a module called PSReadline.
You simply have to enable it in your PowerShell profile (The PowerShell equivalent to your .bashrc) 👏:

  1. Open or create a new PowerShell Profile by typing the following commands directly in your PowerShell:

    # Create profile when not exist
    if (!(Test-Path -Path $PROFILE.CurrentUserAllHosts)) {
      New-Item -ItemType File -Path $PROFILE.CurrentUserAllHosts -Force
    }
    
    # Open the profile with an editor (e.g. good old Notepad)
    ii $PROFILE.CurrentUserAllHosts
    
  2. In the editor add the following lines to the profile:

    # Shows navigable menu of all options when hitting Tab
    Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
    
    # Autocompletion for arrow keys
    Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
    Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
    
  3. Save the file and reopen your PowerShell, now you can use the advanced autocomplete features.

Credit for this goes to StackOverflow users svick and JerryGoyal which have posted this in their answers.

Bonus: Add the open command from MacOS to your PowerShell

On MacOS there is the powerful open command to open folders in Finder (the file explorer from MacOS) or files with their default application from the terminal.
PowerShell also has this command built-in which is called ii (Which stands for Invoke-Item).
Because I find the term ii hard to remember I added an open alias for it which makes it more convenient when switching between the platforms.

Adding a new alias to the PowerShell profile is really simple with the New-Alias command, so simply edit your profile and add the following line:

# New-Alias <alias> <aliased-command>
New-Alias open ii
Enter fullscreen mode Exit fullscreen mode

Save it and reopen your PowerShell and test it by typing open . to open an Windows explorer window in your working directory.

Last tip: Use the new Windows Terminal

To make your PowerShell experience even better you should also install the new Windows Terminal preview from the Microsoft store:
https://www.microsoft.com/store/productId/9N0DX20HK701

It offers tabs and a better customization than the default console in Windows.


I am still pretty new to PowerShell so if you have other tips to increase the productivity like this I would love when you leave a comment here. 💖

Discussion (9)

pic
Editor guide
Collapse
kudostoy0u profile image
Kudos Beluga

Over a year later and this works great!
My terminal experience just got even more convenient :)
img

Collapse
ofhouse profile image
Felix Haus Author

Oh yeah, time flies 😁
Great to hear that it still works!

Collapse
brunovieira97 profile image
Bruno Vieira • Edited

Tips:

  • PowerShell has a default KeyHandler for MenuComplete, that is Ctrl+Space.
  • PowerShell has the alias start for its Start-Process cmdlet, that allows you to spawn an application process, but the key is that, on Windows, when you call Start-Process for a file, the OS will start the process of the default application for that file type AND pass the file as a parameter for it as well, opening it. So, in practice, it's the exact same as open from MacOS, but you write start, like this:
    PS > start .     // will open the current directory on the default file explorer
    PS > start notepad file.txt     // will start notepad opening file.txt
    PS > start notepad     // will only start notepad
    PS > start file.txt     // will start the process for default app for txt files, and open file.txt on it
Enter fullscreen mode Exit fullscreen mode

You can also call the default app process without using start, for example, doing this:

    PS > explorer .
    PS > notepad file.txt
    PS > file.txt
Enter fullscreen mode Exit fullscreen mode

The only thing you CAN'T do on PowerShell by default is opening a folder without either a command or an explicit call to explorer.exe.

Collapse
pvcodes profile image
Pranjal Verma • Edited

Can Anyone Help me out !!!!!
*In PoweShell
I only want a snippet that do

  1. when i enter "run"
  2. and hit tab key,
  3. It simply autocomplete the expression to "g++ -g **.cpp -o main.exe && .\main.exe"
Collapse
robsonsobral profile image
Robson Sobral

Thank you! Thank you so much! Now I need to find a way to auto complete SSH config hosts. Thank you!

Collapse
robsonsobral profile image
Collapse
selami12q profile image
Таисия Рыбакahyh

I can't even begin how much I'm happy about this. Love you, really lots of love haha <3.

Collapse
ofhouse profile image
Felix Haus Author

Haha, thanks for your kind words! 💓

Collapse
matthiasbaldi profile image
Matthias Baldi

thanks for that 👌
implemented this together with a theme and the new windows terminal
gist.github.com/matthiasbaldi/6f4f...