Sublime Forum

User Pref - Toggle comment not working

#1

Hello everyone. I am new to the forums and Sublime Text. Love the program and the features. I have begun using the program but have started running into situations where things don’t work. My first one I will elaborate on will be the toggle comment. I read some posts from other users on other sites and I got the feeling that what I needed to change was an entry regarding ‘user preferences’. I added a user preference on the right side of the screen, change the ‘/’ to ‘keypad_divide’ (or something near that). But that didn’t work either.

Can anyone shed some additional light on what I might try in order to allow me to comment using the shortcut?

Thanks :slight_smile:

0 Likes

#2

Your question is a little unclear. Can you provide more details on what’s not working for you or what it is you’re trying to change so that we can better assist?

For example, are you trying to alter the default key binding of Ctrl+/ for toggling comments so that it also works with the slash key on the numeric keypad? If so, the key bindings would look like the following, which is just a copy of the default bindings with the / character swapped for keypad_divide:

{ "keys": ["ctrl+keypad_divide"], "command": "toggle_comment", "args": { "block": false } },
{ "keys": ["ctrl+shift+keypad_divide"], "command": "toggle_comment", "args": { "block": true } },
1 Like

#3

Sorry for the late response, work has kept me away. No, I don’t want to change anything at all. My problem arose when I tried to comment out a line of code with the “Ctrl-/” key combination, where nothing happened. I did some research and I found where some users suggested that Sublime didn’t recognize this combination and a change was recommended to the keypad_divide. Trust me, I would rather not change anything… I’m about defaults as much as possible, but I was trying to figure out why the comment shortcut wasn’t working.

Hopefully I answered your question OdatNurd.

0 Likes

#4

Ahh, gotcha.

Possible reasons for the toggle comment command not doing what you want include Sublime not knowing how to comment the kind of file that you’re editing or, if you’re using a non-US keyboard layout, Sublime may not properly recognize the key that you’re pressing.

To double check, open up a file that you want to comment, position the cursor where you’d normally hit the key sequence, and then view the Sublime console by selecting View > Show Console from the menu.

In the console, input the following commands:

sublime.log_commands(True)
sublime.log_input(True)

With that done, switch back to the file that you’re commenting on and press the keyboard shortcut for toggling a comment. You will see something similar to the following in the console (you may see other key events and commands displayed as well; that’s OK, just focus on the entries that appear when you take the action).

>>> sublime.log_commands(True)
>>> sublime.log_input(True)
key evt: control+/
command: toggle_comment {"block": false}

Command says toggle_comment

If you see the command toggle_comment trigger as it did here, then Sublime knows you want to toggle a comment, but it can’t because it doesn’t know how to comment that particular kind of file. You will see this if you do this in a plain text file, for example.

If you see this, make sure that the bottom right of the status line agrees with what sort of file you think you’re editing. If it doesn’t, then you need to change the syntax of the file you’re using, change the extension to one that Sublime recognizes for that file, or possibly save your file if you just created it but haven’t saved it yet.

Key event doesn’t match the key you pressed

If you don’t see a key event that matches the key you pressed, then the problem is that your keyboard layout doesn’t match what Sublime is using, so it doesn’t think that you’re pressing the key to toggle a comment and thus doesn’t know what you’re asking.

In that case, aside of changing your keyboard layout the fix is to do something as outlined above and add a custom binding that maps the command to the key the way that Sublime sees it. You should do something similar to what I outlined above, but replace the key with what Sublime reports as the key evt value.

No key event at all

It’s also possible that perhaps pressing the key doesn’t display any key evt information at all. If that’s the case, then Sublime isn’t reacting because it doesn’t know you pressed a key at all.

The most likely culprit in this case is that the key is being “stolen” by some other application or the OS itself, which is seeing the input and grabbing it before Sublime gets to see it. This can also happen if the key you’re pressing is a “dead” key, such as one of the keys in a key sequence that you would use in a non-english language to add diacritical marks and accents to another character.

In this case your recourse is to remap the key; either in Sublime to be something different or at the point where it’s being stolen, depending on which is easier or possible (the OS may not let you change a global shortcut, for example).

In the case of it being a dead key you generally have to either change your keyboard layout entirely or remap the key in Sublime to some other key that’s not dead.

0 Likes

Can't type pipe | symbol in ST3
#5

Awesome OdatNurd. Thanks for all that helpful info. You were 100% spot on. The file I was editing was in fact a text file. I changed it to a html file and I was able to comment both lines and blocks of text.

I appreciate your help.

Have a great day!!

Best,
Mike

1 Like

#6

Any input on how to simply do this:

I launch a new / unsaved text file in ST
I type / copy/paste stuff
I want to add comments as I go

Why the #@#@()_$&^^%#^ can’t ST just throw a %$#@%&^$ comment block at the front of the line?

Why should it care what format it is if the file isn’t even saved yet?
This should be a look-ahead / look-behind kind of function that analyzes a few dozen lines as entered, makes a judgment call of the format, and if nothing matches (select…case… HTML… nope, XML… nope…etc., ) then say ‘Fine, it’s plain text until proven innocent’ and throw a # or a // in front of it.

I’ve spent 4 hours or so researching, digging, trying everyone’s 'this fixed it for me… ’ lawn darts, just trying to figure this crap out. What a waste.

Help… anyone? Or it’s back to pen and paper I think… or just a scotch… neat… on the rocks

0 Likes

#7

sounds like you want to use https://packagecontrol.io/packages/AutoSetSyntax

0 Likes

#8

Why not:

I launch a new / unsaved text file in ST
I select syntax of the file
I type / copy/paste stuff
I add comments as I go

Because lot of things depend of the syntax, including comments.
And don’t see why if the file is saved or not change anything.

And your idea is to create a feature to detect the syntax of a chunk of text ? Doable with a plugin, good luck to manage all the syntax supported by ST.

If you want to add comments in Plain Text syntax, create a file CommentsPlainText.tmPreferences in your Packages\User folder containing:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
	<key>name</key>
	<string>Comments</string>
	<key>scope</key>
	<string>text.plain</string>
	<key>settings</key>
	<dict>
		<key>shellVariables</key>
		<array>
			<dict>
				<key>name</key>
				<string>TM_COMMENT_START</string>
				<key>value</key>
				<string># </string>
			</dict>
			<dict>
				<key>name</key>
				<string>TM_COMMENT_START_2</string>
				<key>value</key>
				<string>// </string>
			</dict>
		</array>
	</dict>
</dict>
</plist>
0 Likes