The VSScripts extension package adds a simple macro facility to Visual Studio for inserting and transforming text. It runs a command line program from within the IDE, feeding it the selection via stdin, and captures the output. Then it replaces the selection with the output.
You can write your macros in any language you like, pretty much. Often, you won't need anything fancier than a simple batch file.
(If you've ever used the User Scripts facility of Xcode 3.x, or emacs's
shell-command-on-region
, this should be familiar.)
Run =installers\VSScripts-0.2.4911.2480.vsix=.
(This edition is provided as-is - sorry! I don't use Visual Studio 2012 any more. The Visual Studio 2013 version may work too.)
Look in installers
folder for .vsix file with largest numbers
attached. Run that file.
You'll need the Visual Studio 2013 SDK, which you can get from here:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=40758
Once you have that installed, load SLN into Visual Studio 2013. Set up
debugging options as follows, adjusting the path to devenv.exe
as
appropriate:
Start external program
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe
Command line arguments
/rootsuffix Exp
Then build, and run - it will run the experimental version of Visual Studio.
Once you're happy, find the VSIX file in the bin\Debug
or
bin\Release
folder and double-click to install.
Once you have the VSScripts extension installed, run Visual Studio 2013.
Note new SCRIPTS menu.
It might not look like much.
Click Scripts...
to reveal the Scripts dialog.
Click Add...
to add a new script.
You get to specify details straight away - let's use one of the examples supplied by setting things up as follows:
Command Line
- use the
...
button to find theexamples\InsertArrow.bat
file from the distribution. Name
Insert ->
stdout
- leave this at the default of
ReplaceSelection
. stderr
- leave this at the default of
LastLineToStatusBar
. stdin
- leave this at the default of
None
.
You're done! Click OK
. Your script was added.
Click OK
to get back to the text editor, and put the caret somewhere you can see it.
Visit the scripts menu - note your script is there!
Select it!
Result! (Even if the compiler doesn't agree.)
Nearly done. Let's assign a keyboard shortcut.
Look in the SCRIPTS
menu - note the 00
icon. You can also see a
0
next to the script's name, if you visit the scripts dialog. And it
also said Script 00
when adding it. This is trying to tell you
something: that this is script 00.
(Don't forget this, because the options dialog is modal.)
Go to the options dialog, keyboard section, and enter script00
in
the Show commands containing:
box. There'll probably be one command
matching it: Scripts.Script00
.
Scripts.Script00
is the one that runs Script 00 (hopefully this is
not confusing).
Assign it a keyboard shortcut. You're also best off setting it to be usable in the text editor only, but it's up to you.
This can be anything that you can type at the command prompt, pretty
much. The command line is executed via CMD /C
, so you can use
builtin commands (SET
, VER
, ECHO
, etc.), or invoke batch files,
or run scripts with file type assocations (e.g., python scripts), and
so on.
- Discard
- the output is discarded.
- ReplaceSelection
- the output replaces the current selection. (The last newline is discarded, if there is one, so if you really want the result to end with a newline then you must print an extra one. This is a .NET limitation.)
- FirstLineToStatusBar
- the first non-empty line of the output is displayed on the status bar.
- LastLineToStatusBar
- the last non-empty line of the output is displayed on the status bar.
- ReplaceOutputWindow
- the output is sent to an output window pane called
VSScripts
, replacing the previous contents. - AppendToOutputWindow
- the output is appended to an output window pane called
VSScripts
.
- None
- no input is supplied.
- CurrentLine
- the contents of the current line, excluding line ending, is sent.
- Selection
- the contents of the selection is sent.
You can write scripts in any language. Just set up the stdin, stdout and stderr options as appropriate for its behaviour.
There are a few pieces of information supplied via environment variables:
- FullPath
- full path of file being edited.
- Filename
- name of file being edited, no path or extension.
- Extension
- extension of name of file being edited, with a leading
.
. - Directory
- directory of file being edited, excluding drive letter.
- RootDir
- drive letter of file being edited, with trailing separator.
- TabSize
- width of a tab, in spaces.
-
Box selection behaviour is far from perfect. It works well enough for my purposes though and I'm sick of poking at it for now.
-
Should be cleverer about box selections. With box selection active, if output includes a newline, should replace box with new output. If output doesn't include a newline, and box selection is 0 chars wide, should treat box selection as multiple cursors. That should probably cater for most cases. At the moment it's easy to end up with a big mess.
-
Needs more stdin/stdout/stderr options.
-
Would be nice to be able to rearrange the scripts order.
-
Would be nice to be able to name the commands dynamically rather than have the stupid numbers - not sure Visual Studio will do this.
-
Should be able to supply dynamic information to scripts via command line.
-
Should be able to handle I/O via temp files, if the script would need that.
(etc., etc.)