【问题标题】:Sublime text: Separate comment/uncomment shortcutsSublime text:单独的注释/取消注释快捷方式
【发布时间】:2017-10-29 21:26:33
【问题描述】:

我知道用 Ctrl+/ 切换 cmets。

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

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

【问题讨论】:

  • 据我所知,这是不可能的……

标签: sublimetext3 sublimetext


【解决方案1】:

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

  • 从工具菜单 -> 开发人员 -> 新插件...
  • 全选并替换为以下内容
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 绑定...

【讨论】:

  • 谢谢,效果很好!看起来 Sublime Text 是可深度定制的。也感谢覆盖警告!
  • 关于“删除注释”命令的小问题:如果选择跨越多行,其中一些未注释,其中一些已注释,如果调用命令时注释的行变为未注释,那就太好了.如果不清楚,我可以提供屏幕截图。
  • 好吧,我想我已经破解了。 (如果有人感兴趣:pastebin.com/VigU3HS0
  • 啊,我承认我没有测试那个场景——抱歉!随时编辑我的答案以包括您的改进:)
猜你喜欢
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 2012-07-20
  • 2013-07-18
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多