Skip to content

modulexcite/VSScripts

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VSScripts

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.)

installation

installing pre-built version (Visual Studio 2012)

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.)

installing pre-build version (Visual Studio 2013)

Look in installers folder for .vsix file with largest numbers attached. Run that file.

building it yourself

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.

quick walkthrough

Once you have the VSScripts extension installed, run Visual Studio 2013.

Note new SCRIPTS menu.

images/SCRIPTSMenu.png

It might not look like much.

images/SCRIPTSMenuEmpty.png

Click Scripts... to reveal the Scripts dialog.

images/ScriptsDialog.png

Click Add... to add a new script.

images/AddScript00DialogEmpty.png

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 the examples\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.

images/ScriptsDialogWithScript00.png

Click OK to get back to the text editor, and put the caret somewhere you can see it.

images/SourceFileBeforeInsertingArrow.png

Visit the scripts menu - note your script is there!

images/SCRIPTSMenuWithInsertArrow.png

Select it!

images/SourceFileAfterInsertingArrow.png

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.

images/ToolsOptionsKeyboardWithScript00Selected.png

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.

images/ToolsOptionsKeyboardAssigningKeyboardShortcut.png

script settings summary

Command line

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.

stdout and stderr settings

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.

stdin settings

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.

making your own scripts

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.

bugs/problems

  • 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.)

About

Extension package that provides a simple macro facility for Visual Studio

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 72.8%
  • C# 17.0%
  • C 10.0%
  • Other 0.2%