How to setup PowerShell with bash shortcuts
TL;DR — Enable Emacs shortcuts (PS6 & PS7)
Steps:
Open PowerShell and run this 4 lines
\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
\> mkdir $(Split-Path $PROFILE)
\> echo "Set-PSReadLineOption -EditMode Emacs" >> $PROFILE
\> & $PROFILE
use Ctrl+p
to search into history to check the change was done
Explanation:
- Set
RemoteSigned
intoExcecutionPolicy
forCurrentUser
mkdir
creates the$PROFILE
directory- Paste the
Emacs-Edit-Mode
into$PROFILE
’s file - Reload
$PORFILE
configuration
Say Hi to PowerShell
PowerShell and command line (CMD) are the default tools in windows, and you has to deal with it. Linux and Mac has a bash terminal. I know that there are another programs like Git bash, but they are not native and that is the reason that I want to focus in PowerShell.
Today PowerShell comes with the version 5 as default, it’s ok but there are some features like &&
that are really important and it doesn’t have it. So the first step is to check which version do you have and upgrade it to PowerShell7.
run this code in your terminal
(Get-Host).Version
The Major
number will shows the version. At this moment, I use the v5.1.
OK, Let’s download the latest version from github
In the Release section, you can find all different releases, and to search it. Check the Assets
menu and find the ---x64.msi
file
This is a normal installer so you won’t have any problem. Just Execute the Powershell
executable. Additionally, windows has some front-end application which integrates the program
Say Hi to «Windows Terminal»
I recommend to use this software, it is the windows terminal, this app is an interface not only for PowerShell, it works with all the terminals. Git Bash, WSL, WSL2, PowerShell, CMD and it is pretty
This app, comes in the windows store, and you can install it from the official store
Now that we has all the programs. lets do the configuration
Open the windows terminal
Open the Settings
Go to the list
section and copy the global unique id guid
of the PowerShell, is a long string like this. But be careful and don’t confuse with the PowerShell 5, check the comments
Now paste the value into the defaultProfile
it is located at the start of the file.
save the file and open the Windows Terminal, if everything was right, it is going to open the PowerShell
PowerShell Configuration
The next step is customize the configuration file. So let’s check the PowerShell Execution permissions
Get-ExecutionPolicy -List
PowerShell restricts some customization by default. This is for security reasons. But don’t worry, it’s no dangerous and you don’t need an administration permission.
If you want to know more about this policy, the official documentation is here
Let’s change the CurrentUser
policy from Undefined
to RemoteSigned
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Now Let’s create the scope’s folder
mkdir $(Split-Path $PROFILE)
The $PROFILE
is an environment variable which stores the configuration location, We are going to open this file with the notepad
program
notepad $PROFILE
and paste the configuration Set-PSReadLineOption -EditMode Emacs
Save it, and reload the terminal. You can close and open it or run this command
& $PROFILE
If everything goes fine. You can use the bash shortcuts in PowerShell.
Extra
C-j as Enter
I really love to Accept line with Ctrl-j
and Windows doesn’t have it. So you only need to include this line into the $PROFILE
Set-PSReadLineKeyHandler -Chord ctrl+j -Function AcceptLine
Cheatsheet
Here is a Webpage where you can find some awesome shortcuts to start
Thats is all the configuration I expect this can helps you.