When I press the standard Ctrl + E, C (an other variants) in VS2008 whilst editing a CSS file, it says that command is not available. How do I setup a shortcut to apply a plain old /* */ comment to selected text in VS? Thanks
+4
A:
Within Visual Studio, hit Alt-F11 to open the Macro IDE and add a new module by right-clicking on MyMacros and selecting Add|Add Module...
Paste the following in the source editor:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
Public Module CommentCSS
Sub CommentCSS()
Dim selection As TextSelection
selection = DTE.ActiveDocument.Selection
Dim selectedText As String
selectedText = selection.Text
If selectedText.Length > 0 Then
selection.Text = "/*" + selectedText + "*/"
End If
End Sub
End Module
You can create a keyboard shortcut by going to Tools|Options... and selecting Keyboard under the Environment section in the navigation on the left. Select your macro and assign any shortcut you like.
You can also add your macro to a menu or toolbar by going to Tools|Customize... and selecting the Macros section in the navigation on the left. Once you locate your macro in the list, you can drag it to any menu or toolbar, where it its text or icon can be customized to whatever you want.
Jeff Hillman
2008-10-30 05:16:30
Wow, I thought Ctrl + K, Ctrl + C would do it. Anyone know why this this doesn't work in CSS? Works in html, C#, SQL, Javascript...
Codewerks
2008-10-30 05:36:47
Great info Jeff!
deadbug
2008-10-30 05:41:12
This is great - tjere's just one other thing - when commenting more than 1 line of CSS, it seems to add an extra tab to the beginning of every line after the first one? The result is your code is tabbed in like an upside down staircase. Why would it be doing this?
wows
2008-10-30 20:41:34
@wows - I can't reproduce what you are describing; commenting seems to work the same whether I have one or many lines of CSS selected. The formatting doesn't change at all.
Jeff Hillman
2008-10-31 00:48:55
@Jeff Hillman - I figured it out - it was because VS was set to "Block" indenting instead of "Smart" indenting under CSS specific editor settings. Switching to smart fixes the problem. Cheers!
wows
2008-11-02 22:42:26
@wows - Glad to hear it. It might be possible to modify this for "Block" mode on by inserting '/*' and '*/' directly into the text (using selection.AnchorPoint.AbsoluteCharOffset and selection.ActivePoint.AbsoluteCharOffset) rather than replacing the entire selection. Something to try later.
Jeff Hillman
2008-11-03 13:55:47
A:
here's an even simpler solution:
Sub CommentCSS()
DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.Text = "/*"
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.Text = "*/"
End Sub
you can record it yourself using ctrl+shift+R
- place the cursor on the line you want to comment
- press "Home" on your keyboard
- type /*
- press "End" on your keyboard
- type */
- save your recording
Thanks, but this only really works for one line at a time. It's quite common to comment a whole CSS class or multiple classes/selectors at a time.
wows
2009-07-06 22:32:49