DEV Community

Cover image for Add a Bash-like autocomplete to your PowerShell
Felix Haus
Felix Haus

Posted on • Updated on

Add a Bash-like autocomplete to your PowerShell

I mostly use Ubuntu or MacOS for development, but recently I switched sides because I needed 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. 💖

Top comments (13)

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

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

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
Robson Sobral
Collapse
 
robsonsobral profile image
Robson Sobral
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
 
mcascone profile image
Max • Edited
function run { 
   'g++ -g **.cpp -o main.exe && .\main.exe' | Invoke-Expression 
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
minus20 profile image
Mikhail

I really missed this feature. Thank you.

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

Haha, thanks for your kind words! 💓

Collapse
 
bglamadrid profile image
Benjamin La Madrid • Edited

Fast-forward to December 7th, 2022
The steps described in this guide still work like a charm. This autocompletion feature totally beats the default one.

Thank you so much!

Collapse
 
devpbrilius profile image
Povilas Brilius

Strange, it doesn't offer oh-my-zsh like style completion.

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...