共用方式為


gvimf.ps1 - easier way to get files into an editor

In the cmd world, I constantly had filenames that I had to copy-paste onto a gvim command-line, sometimes from findstr, or from dir, or whatever. 

In the PowerShell world, I tend to have available (from some previous command) paths as strings, PathInfo instances, powershell item objects, or something similar, so all those things could be piped into get-item (which is amazingly good at figuring out which item you're talking about based on just about anything you can pass to it).  That makes it very easy to get those items and, for my usage, pass them along to gvim, potentially using "tf" to pend edits on them first.

In fact, to get the source to post here, I didn't even need to remember which dir I had this script in, I just had to "gcm gvimf | gvimf"

 param([switch] $edit)

if (-not $args) { $args = @($input) }
if (-not $args) { throw 'files are required either as params or input' }

$filepaths = $args | get-item | %{ $_.fullname }
if ($edit) { tf edit $filepaths }
gvim $filepaths

The first "real" line (referring to $input) lets us pass things in either as command-line params or piped in (I could have allowed both easy enough with += and dropping the if, now that I think about it, but haven't actually had that need yet)

There's one thing that's conspicuous in its absence - quoting!  I love PowerShell - the above Just Works even if the dirs have spaces in them, or the files have spaces in them or both.  I didn't have to deal with any of that junk myself when calling tf.exe or gvim.exe, it just does the right thing since I'm passing arrays of strings.  It's a beautiful thing.

why gvim instead of vim? because the gvim window is easier to resize/maximize and it doesn't block the powershell that spawned it like vim does.

Comments