In Visual Studio 2010, How to Easily Comment Out Lines in CSS

In Visual Studio 2010, is there a way to easily comment out lines in CSS?

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.

Visual Studio 2010 comment / uncomment buttons for css files

You can record your own macros in VS.

This article might help you (it's for VS2008 but it's same in VS2010):

http://www.brianschmitt.com/2009/09/how-to-comment-uncomment-code-selection.html

Visual Studio 2010: highlight CSS text and comment

Comment Selection command is not available for css files.

However, you can create a simple macro and assign a keyboard shortcut to it...

Sub CommentCSS()
Dim selection As String
selection = DTE.ActiveDocument.Selection.Text
selection = "/*" + selection + " */"
DTE.ActiveDocument.Selection.Text = selection
End Sub

To 'uncomment' the selection use another simple macro:

Sub UncommentCSS()
Dim selection As String
selection = DTE.ActiveDocument.Selection.Text
selection = selection.Remove(0, 2)
selection = selection.Remove(selection.Length - 2, 2)
DTE.ActiveDocument.Selection.Text = selection
End Sub

comment css doesnt work on aspx page under vs2010 ?

There is no default method for commenting css lines in aspx page. So it just add the asp comments in lines.

But you can record your own macro to perform the action. Refer to this link :

Make Comments for CSS

Is there any way to get Visual Studio 2010 to put comments in left-most column?

I use the following macro. It's pretty slow if you've got a lot of lines selected for commenting, and I'm not too familiar with writing macros, so it could probably be improved a lot, but it works for me.

Public Module Module1
Sub CodeBlocksComment()
Dim start_line, end_line, temp As Integer
Dim selection As EnvDTE.TextSelection
selection = DTE.ActiveDocument.Selection

start_line = selection.TopLine
end_line = selection.BottomLine
If end_line < start_line Then
temp = start_line
start_line = end_line
end_line = temp
End If

If Not start_line = end_line And selection.BottomPoint.AtStartOfLine Then
end_line -= 1
End If

DTE.UndoContext.Open("Comment Region")
Try
For i = start_line To end_line
selection.GotoLine(i)
selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
selection.Text = "//"
Next
selection.GotoLine(start_line)
selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstColumn)
selection.LineDown(True, end_line - start_line + 1)
Finally
DTE.UndoContext.Close()
End Try
End Sub
End Module

You can then set whatever keyboard shortcut you want. The command will be listed as Macros.MyMacros.Module1.CodeBlocksComment

Quickly commenting /* selected C# code */ in Visual Studio (not the // whole line)

Resharper does that. It costs money - but it is worth it.

It's called "Block Comment".

And the default keyboard shortcut is Ctrl+Shift+/

How do i comment marked text in Visual Studio 2012 C#? (Not the line)

I dont think there is any shortcut for inline comments. The best I have known is CTRL K + CTRL C but that too comments the whole line.

I think if you have a comment you can instead shift it to the right and then comment it using the usual syntax //

Something like this:-

 int a=500;  //200+

EDIT:-

You can use TEXT MACROS FOR VISUAL STUDIO 2012

You can use it to automate repetitive text editing tasks. It is
inspired by the macro feature of Notepad++, so if you have used it
then you already know how to use this extension.

Shortcut for commenting CSS in VS 2008

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.

The comment out selected lines toolbar button is disabled in Visual Studio 2010

I didnt want to do this, but uninstalling it and reinstalling it fixed it. Still have no idea what caused it and I have no intention of trying to replicate it as it took me a while to reinstall my packages.

Thanks everyone for your help.

Comment Opening and Closing Brackets (with ReSharper?)

This isn't entirely what you're after, but it's pretty close:

Highlight the code inside the if statement by placing the cursor at one brace and hitting Ctrl + Shift + ].

Now hit Ctrl + Shift + Alt + Left Arrow. This will move the code 'left', i.e. outside of the if statement.

You don't need to comment the if statement out after this because it's empty.

Note that you can also move code 'right' to put it back in the if statement later.



Related Topics



Leave a reply



Submit