Does anybody know if there is a way in Visual Studio 2010 to highlight and comment out lines in CSS files like you can with all other files (by clicking a button)? Perhaps a Visual Studio extension? Commenting them manually is cumbersome.
-
ctrl-k-ctrl-c doesn't work? (I haven't used it for CSS files specifically, so don't know if it works there) – jalf Jun 24 '10 at 13:39
-
+! Good point - I never noticed this, CTRL+K+C doesn't work and there is no menu option to comment-out. – Fenton Jun 24 '10 at 13:40
2 Answers
Unfortunately the regular commands for commenting and uncommenting (Ctrl+K+C and Ctrl+K+U) don't work for CSS. Instead, you'll need to record or write a macro that does this and attach it to your own shortcut.
To comment the selected text (note, this is quick and dirty and therefore comments it as a single block):
Sub CssComment()
DTE.ActiveDocument.Selection.Text = "/*" + DTE.ActiveDocument.Selection.Text + "*/"
End Sub
Update
This new one below works more like the regular comment command and comments on a line-by-line basis. It means you don't have to select the text before hand. This also does all the changes as a single undoable operation and checks the file extension so that you can assign this to the regular shortcut and it will work for all files.
Sub CommentCss()
Dim ts1 As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
Dim fileName = DTE.ActiveDocument.FullName
' We should default to regular commenting if we're not editing CSS.
' This allows this macro to be attached to the Ctrl+K+C shortcut
' without breaking existing file format commenting.
If Not fileName.EndsWith(".css") Then
DTE.ExecuteCommand("Edit.CommentSelection")
Return
End If
Dim weOpenedUndo As Boolean = False
If Not DTE.UndoContext.IsOpen Then
DTE.UndoContext.Open("CommentCSS")
weOpenedUndo = True
End If
ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)
Dim ep1 As EditPoint2 = ts1.TopPoint.CreateEditPoint()
Dim ep2 As EditPoint2 = ts1.BottomPoint.CreateEditPoint()
While ep1.Line <= ep2.Line
Dim text As String = ep1.GetLines(ep1.Line, ep1.Line + 1)
text = text.Trim()
If Not text.StartsWith("/*") Or Not text.EndsWith("*/") Then
ep1.StartOfLine()
ep1.Insert("/*")
ep1.EndOfLine()
ep1.Insert("*/")
End If
Dim lineBeforeDown As Integer = ep1.Line
ep1.LineDown()
If ep1.Line = lineBeforeDown Then
Exit While
End If
End While
ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)
If weOpenedUndo Then
DTE.UndoContext.Close()
End If
End Sub
Update for Uncommenting
This macro performs the reverse task. Again, it's implemented so that it will work for all documents if required by checking the file extension and deferring to the standard Edit.UncommentSelection
command for non-CSS files.
Sub UncommentCss()
Dim ts1 As TextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
Dim ep1 As EditPoint2 = ts1.TopPoint.CreateEditPoint()
Dim ep2 As EditPoint2 = ts1.BottomPoint.CreateEditPoint()
Dim fileName = DTE.ActiveDocument.FullName
' We should default to regular commenting if we're not editing CSS.
' This allows this macro to be attached to the Ctrl+K+C shortcut
' without breaking existing file format commenting.
If Not fileName.EndsWith(".css") Then
DTE.ExecuteCommand("Edit.UncommentSelection")
Return
End If
Dim weOpenedUndo As Boolean = False
If Not DTE.UndoContext.IsOpen Then
DTE.UndoContext.Open("UncommentCSS")
weOpenedUndo = True
End If
While ep1.Line <= ep2.Line
ep1.StartOfLine()
Dim text As String = ep1.GetLines(ep1.Line, ep1.Line + 1)
text = text.Trim()
If text.StartsWith("/*") And text.EndsWith("*/") Then
Dim epEndOfLine As EditPoint2 = ep1.CreateEditPoint()
epEndOfLine.EndOfLine()
text = text.Substring(2, text.Length - 4)
ep1.ReplaceText(epEndOfLine, text, vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers Or vsEPReplaceTextOptions.vsEPReplaceTextAutoformat)
End If
Dim lineBeforeDown As Integer = ep1.Line
ep1.LineDown()
If ep1.Line = lineBeforeDown Then
Exit While
End If
End While
ts1.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn, True)
If weOpenedUndo Then
DTE.UndoContext.Close()
End If
End Sub
Update 18Oct2012
As per dirq's answer, there is an extension, Web Essentials that provides CSS commenting and uncommenting. I would recommend using this over the macros above as it provides other great support besides just the CSS commenting shortcuts.

- 1
- 1

- 61,417
- 20
- 137
- 189
-
CTRL+K+C and CTRL+K+U doesn't work. Could you elaborate on the macro? – Ralf de Kleine Jun 24 '10 at 13:47
-
@Jeff Sweet! Seems like it would be pretty easy, but I'll let you prove it. :-D – jeremcc Jun 24 '10 at 13:50
-
@jeremcc: No problem. I'll update my answer with a better macro when I've worked it out. – Jeff Yates Jun 24 '10 at 14:08
-
@jeremcc: Updated to include better comment macro and to include an uncomment counterpart. – Jeff Yates Jun 24 '10 at 16:39
-
@jeremcc: You're welcome. It was an interesting exercise to work on. I learned quite a bit about the Visual Studio object model. – Jeff Yates Jun 25 '10 at 13:15
-
Great answer, makes all the difference that you check the file type so you can use the same shortcut :) and no problems with the format staying the same when you uncomment either, nice job +1 – Pricey Aug 17 '11 at 23:02
There's an extension available that works better than the macro: Web Essentials. Check it out. http://visualstudiogallery.msdn.microsoft.com/6ed4c78f-a23e-49ad-b5fd-369af0c2107f

- 968
- 2
- 11
- 26