I spend a lot of time bouncing between Windows and Linux. These days my Linux use is mostly WSL2, which is pretty solid and makes Windows close to a useable platform for people that enjoy their *nix operating systems. The more familiar one gets with *nix incantations to make their computers do weird and wonderful things in a few keystrokes, the more windows, even with PowerShell, starts to feel clunky. So here are two quick things, one a VS Code tip and the other a powershell utility script, that can help you unlock some computing power
VS Code is a super powerful editor and has lots of cool capabilities. Here is about 11 1/2 minutes of awesome tips. One I found super handy the other day that was not in this list was using regular expression matching to add 2 spaces to every line in a yaml file because I needed to add another level of indentation for config.
- hit ctrl-h
- click the Use regular expression box in the search box. The top one.
- type a caret (^)
- type two spaces in the replace box. The bottom one
- Click the Replace All button
If you need to do something similar to the END of every line of a file, the search box gets a $. These sorts of tags are called anchors and can be extremely handy
Second tip is creating a utility function to replicate the history | grep combination I use and abuse on a daily basis. PowerShell has a Get-History function, but its use is limited to the current instance of the shell you are working in. If you have the same shell open since you started working, great, that’s fine. But often I’m looking for something I know I did a few weeks ago and I can’t quite remember the particulars of the command, but I know if I found the old one, then I’d be good to go. To that end I created a function called Search-History that looks like this
function Search-History {
param ($target)
Get-Content (Get-PSReadlineOption).HistorySavePath | ? { $_ -like "*$target*" }
}
Get-Content <blah blah blah> “my string” does not roll off the fingers the way that history | grep does, so I wrote this and use it A LOT. This can be added to a powershell script and loaded at startup every time you are working in powershell by following the instructions here.
Hopefully these two things will remove a little bit of friction from your day to day work.