I am using Sublime Text 3. I have encountered a problem. I don't know how to toggle XML line comment.
I know there is a Toggle Comment
function in Sublime Text 3 and I tried. However, the result is not the same as what I envisioned.
For example, I want to toggle comment the following XML code:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
I want to make it like this(Just like Eclipse's line comment):
<!-- <profile> -->
<!-- <id>jdk-1.8</id> -->
<!-- <activation> -->
<!-- <activeByDefault>true</activeByDefault> -->
<!-- <jdk>1.8</jdk> -->
<!-- </activation> -->
<!-- <properties> -->
<!-- <maven.compiler.source>1.8</maven.compiler.source> -->
<!-- <maven.compiler.target>1.8</maven.compiler.target> -->
<!-- <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> -->
<!-- </properties> -->
<!-- </profile> -->
However by using Toggle Comment
in Sublime, I can only get the following code:
<!-- <profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile> -->
I don't know how to achieve this goal. I search this problem on Google but I can not find any useful information. Can you give me some suggestions?
In general, Sublime can be configured to know the difference between a line and block comment and act accordingly. However as far as I can tell, this can't be done for XML because it needs to wrap the content with comment characters.
More specifically, the configuration options for comments specify either a TM_COMMENT_START for a pure line comment or TM_COMMENT_START and TM_COMMENT_END for a block comment. If both are present, the toggle command selects the correct one based on content and context.
For XML, it uses a pair due to how comments in XML works, which means that only block comments are possible. However, when you invoke the command with no selection, it assumes that the selection wraps the entire line. If you have a selection, that's what gets wrapped.
One way around this problem is to split your selection into lines before you toggle the comment. You can do that via
Selection > Split into Lines
from the menu (this will also show you what your key binding is for this command).It is possible to group these commands into a macro so that you don't have to take multiple steps on your own.
Such a macro could look like the following (saved in your
User
package asXML_Line_Comment.sublime-macro
):This would split the selection, toggle the comment, and then return to a single selection (and jump to the start of the line). You can modify this as appropriate (e.g. if you don't want to revert to single selection afterwards).
You can run this macro from the menu bar (
Tools > Macros > User > XML_Line_Comment
), but a better way might be to set up a key binding. An example of that would be:This will cause the key that normally toggles comments to run your macro for the specific case of there being a selection while you're in an XML file.