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
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
) π:
-
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
-
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
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
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)
Over a year later and this works great!
My terminal experience just got even more convenient :)
Oh yeah, time flies π
Great to hear that it still works!
Thank you! Thank you so much! Now I need to find a way to auto complete SSH config hosts. Thank you!
I found it!
github.com/lukesampson/scoop/wiki/...
Or, even simpler:
gist.github.com/backerman/2c91d31d...
Can Anyone Help me out !!!!!
*In PoweShell
I only want a snippet that do
I really missed this feature. Thank you.
I can't even begin how much I'm happy about this. Love you, really lots of love haha <3.
Haha, thanks for your kind words! π
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!
Strange, it doesn't offer oh-my-zsh like style completion.
thanks for that π
implemented this together with a theme and the new windows terminal
gist.github.com/matthiasbaldi/6f4f...