PowerShell: history enhancements (readline)

historypowershellproductivityreadline

Some of the things I like in Bash and would love to know how to do in PowerShell:

  1. In Bash, I have history scrolling set up so that it only scrolls commands that begin with the same prefix as the current line. If I want to see my latest commit (e.g. to reuse part of the comment) I write 'git' and then .

  2. Related is of course the history search with Ctrl + R

  3. To find other things, I write:

    h | grep foo
    

    In PowerShell I use:

    h -c 1000 | where {$_.commandline.contains("foo")}
    

    (obviously I'm a newbie, there must be a shorter way)

  4. Things like:

    mv file.txt{,.bak}
    

    or

    mv file.txt !#$.bak
    
  5. Magic space (that expands !$ inline)

What are the alternatives in PowerShell?

Best Answer

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.

Related Question