PowerShell: history enhancements (readline)?
Solution 1:
For (3), you can write it as a function in your profile (e.g. %USERPROFILE%\My Documents\WindowsPowerShell\profile.ps1
):
function hh ([string] $word) {
Get-History -c 1000 | where {$_.commandline.contains($word)}
}
Then:
hh foo
But Powershell is best thought of as a scripting language than as an interactive shell, because the underlying console is still cmd.exe with all its limitations.
So it's F7 for interactive history, F3 to copy the previous command, F1 to copy a single character, F2 to copy the previous command up to a particular character, etc, etc.
Solution 2:
1 - You can use F8 from the windows console to cycle through commands that match the beginning of the current line. This is a case sensitive match.
2 - You can use #
to match previous commands. Using #<partial match><tab>
is case insensitive and will match text at any position in the previous commands.
If you have the following command history:
# 1
$np = Start-Process notepad -PassThru
# 2
$np| get-process
# 3
$np| Stop-Process
Typing #pr
then tab repeatedly will cycle through 1, 2 and 3.
Typing #st
then tab repeatedly will cycle through 1 and 3.
Using only # will match all history.
#
can also be used after entering part of a command. If your history is:
'notepad'
select *
You can type Get-Process #n<tab>| #s<tab>
to get Get-Process 'notepad'| select *
3 - You can use Select-String
. Create an alias for it to make it easy to use (PowerShell v3 added the alias sls
). You could then do.
h| sls foo
4 - You can do something like:
gci *a.txt| ren -n {$_.Name + '.bak'}
5 - $$
matches the last token of the last command, but I don't know of a way to expand it inline.
Solution 3:
You should check out Oisin Grehan's PSReadline module: http://nivot.org/blog/post/2012/09/12/Emulating-Bash-GNU-Readline-with-PowerShell-30
The code is available here: https://bitbucket.org/oising/psreadline/overview
This makes PowerShell actually usable as a CLI, from the point of view of experienced *nix admins who occasionally work with Windows, by adding basic EMACS bindings for line editing, history search etc.