Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing PowerShell history with up-arrow

I recently switched to powershell since my Cygwin bash started giving me senseless compilation errors when using maven. I've found how to save and restore my command history in (https://stackoverflow.com/questions/9259723/is-there-a-windows-shell-tool-can-keep-history), which seems to work (using "History" will show the recent commands after a clean start).

What I can't seem to do is access this history with the up arrow like you would if the command was used in the current session.

Any ideas?

like image 736
Siebe Avatar asked Jun 12 '12 09:06

Siebe


2 Answers

Like say @jhclark it is now possible

You have all the documentation for installation in the official GitHub : https://github.com/PowerShell/PSReadLine

You will have to follow the whole tutorial well and pay attention to:

  • remove -AllowPrerelease arg if you have error
  • add this line Import-Module 'PSReadLine' in your profile code $PROFILE for example to open your profile conf with vscode
  • Close all shell after you install PSReadLine if you want all work fine

Launch this two lines if you want the fleche to search in the history like on Linux because that is, I think, what most people are looking for:

Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward

by default are the key combinations F8 and SHIFT + F8 which are configured by default

You can see the conf with that :

Get-PSReadLineKeyHandler
like image 105
LinkPhoenix Avatar answered Sep 30 '22 13:09

LinkPhoenix


As of October 2013, this is now possible using the wonderful PSReadline module: http://github.com/lzybkr/PSReadLine

You'll still need to save your history when your powershell session exits and load it in your profile.ps1 prior to loading PSReadline (see http://technet.microsoft.com/en-us/library/ee156792.aspx). You can register a hook to save your history when PowerShell exists using a hook like this: Powershell profile "on exit" event?. Unlike vanilla PowerShell, PSReadLine allows the up/down keys to access this history buffer.

like image 34
jhclark Avatar answered Sep 30 '22 13:09

jhclark