Sublime text:单独的 comment/uncomment 快捷方式

Sublime text: Separate comment/uncomment shortcuts

我知道用 Ctrl+/.

切换评论

我希望能够使用不同的快捷方式来评论和取消评论行。即:Ctrl+r 进行评论,Ctrl+t 取消注释。

有人知道这样做的方法吗?我在 sublime text 3.

您可以通过创建一个新插件来做到这一点:

  • 从工具菜单 -> 开发人员 -> 新插件...
  • select 全部替换为下面的
import sublime
import sublime_plugin
from Default.comment import *

class AddOrRemoveCommentCommand(ToggleCommentCommand):
    def run(self, edit, **kwargs):
        block = kwargs.get('block', False)
        for region in self.view.sel():
            comment_data = build_comment_data(self.view, region.begin())
            if (region.end() != self.view.size() and
                    build_comment_data(self.view, region.end()) != comment_data):
                # region spans languages, nothing we can do
                continue

            if kwargs['mode'] in ('remove', 'toggle'):
                if self.remove_block_comment(self.view, edit, comment_data, region):
                    continue

                if self.is_entirely_line_commented(self.view, comment_data, region):
                    self.remove_line_comment(self.view, edit, comment_data, region)
                    continue

            if kwargs['mode'] in ('add', 'toggle'):
                has_line_comment = len(comment_data[0]) > 0

                if not has_line_comment and not block and region.empty():
                    # Use block comments to comment out the line
                    line = self.view.line(region.a)
                    line = sublime.Region(
                        advance_to_first_non_white_space_on_line(self.view, line.a), line.b)

                    # Try and remove any existing block comment now
                    if kwargs['mode'] == 'toggle' and self.remove_block_comment(self.view, edit, comment_data, line):
                        continue

                    self.add_comment(self.view, edit, comment_data, block, line)
                    continue

                # Add a comment instead
                self.add_comment(self.view, edit, comment_data, block, region)
  • 将其保存在 ST 推荐 (Packages/User/) 的文件夹中,如 add_or_remove_comment.py(文件扩展名很重要,基本名称不重要)
  • 在您的用户键绑定文件中为 add_or_remove_comment 评论命令创建 2 个键绑定,根据需要将 mode 参数设置为 addremove,即
{ "keys": ["ctrl+r"], "command": "add_or_remove_comment", "args": { "mode": "add" } },
{ "keys": ["ctrl+t"], "command": "add_or_remove_comment", "args": { "mode": "remove" } },

请注意,Ctrl+R 将覆盖默认的 Goto Symbol 绑定,而 Ctrl+T 将覆盖默认的 Transpose Text 绑定。 ..