I’ve been working a bit more in Linux. Besides it inspiring me to write a lexical parser in python for PowerShell (don’t get your hopes up – it’s too much work, and I can’t do it), I keep getting little inspirations for cool projects. For example, someone needs to create tmux for PowerShell; I digress. This powerbit is from that inspiration. Recently, my laptop’s up and down arrows stopped working. Because of this, I have been relying on history |grep something in Linux so that I can quickly copy and paste that something back into the commandline. Context Swtich! Yesterday I was in PowerShell, and I got frustrated by the output of get-history (I really only want the commandline property) and the complexity it takes to filter for what I want using Where-Object. As I was discussing this with a colleague, I said, let me just wrap this already and never worry about it again. So here it is, hgrep:
function hgrep { param( [Parameter(Mandatory=$false, Position=0)] [string]$Regex, [Parameter(Mandatory=$false)] [switch]$Full ) $commands = get-history |?{$_.commandline -match $regex} if ($full) { $commands |ft * } else { foreach ($command in ($commands |select -ExpandProperty commandline)) { # This ensures that only the first line is shown of a multiline command if ($command -match '\r\n') { ($command -split '\r\n')[0] + " ..." } else { $command } } } }
you can use it like this:
hgrep something
While, it’s bad practice to add the |ft command to the function, I find it useful so that I can use the -full switch to get everything I want in the format I want to see it:
11:17:22 PS D:\Dropbox\scripts> hgrep longfunction -full Id CommandLine ExecutionStatus StartExecutionTime EndExecutionTime -- ----------- --------------- ------------------ ---------------- 9 longfunction Completed 9/26/2013 11:00:00 AM 9/26/2013 11:14:41 AM
You can fork or clone this gist off of github here, or just run the following:
git clone https://gist.github.com/6715170.git
