Vim, Ctags, and PowerShell

Since I have been working with some PowerShell modules that consist of several .ps1 files (in addition to the .psm1 file), I was curious as to whether ctags could work with PowerShell.

This post was a pretty good start. 

There were two problems, easily fixed.

The first was in the command line to add PowerShell to ctags:
–regex-powershell=”/^function[\t ]*([a-zA-Z0-9_]+)/\1/d,definition/”
The regex doesn’t include ‘-‘, so it won’t recognize a whole lot of functions….

The other problem was in the .cnf file:

--langdef=powershell
--langmap=powershell:.ps1
--langmap=powershell:.psm1

That did not work; the .ps1 files were not recognized, as though the .psm1 line was overwriting the previous assignment.
Looking at the ctags documentation, I learned that this is a way to do it:

--langmap=powershell:.ps1.psm1

Perfect.

My complete ctags.cnf (stored in $env:USERPROFILE):

--langdef=powershell
--langmap=powershell:.ps1.psm1
--regex-powershell=/^[Ff]unction[\t ]*([a-zA-Z0-9_-]+)/\1/f,function/
--regex-powershell=/^[Ff]ilter[\t ]*([a-zA-Z0-9_-]+)/\1/i,filter/
--regex-powershell=/^[sS]et-[Aa]lias[\t ]*([a-zA-Z0-9_-]+)/\1/a,alias/

(Unfortunately, aliases are not recognized yet.)

To use, open a console and navigate to a directory with some PowerShell files.
Here’s the command I use:

> ctags.exe –append=yes –verbose=yes -o p:\tags –tag-relative=yes *

(Run “ctags –help” for all command line switches.)

In using this at work I discovered that ctags (and maybe gvim) doesn’t work well with UNC paths.  I wanted to put my tags file in “\\corp\hdq\data2\ZZZ CMM\2 WPDB\2.03 Application Material\Ziggy_Danger_2014\Support\PowerShell”, but that didn’t work, so I mapped P: to that path and it worked as expected.

In gvim:
:set tags=P:\tags
… and Bob’s your uncle.

Some useful basic commands for using tags:
– With the cursor in a function name, press Control-].
– To pop back to where you came from, press Control-o (oh, not zero).

– From the command line, regex search for tags, can be partial names: :ts /Edit-
This will bring up a list of matching tags, cool.

Very handy for navigating within the .psm1/.ps1 components of a PowerShell module.

Exuberant CTags: http://ctags.sourceforge.net/

If you have Chocolatey installed (you don’t?  Are you nuts?):

> choco install ctags

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.

Protected with IP Blacklist CloudIP Blacklist Cloud