【问题标题】:Sublime Text 3 - End key toggles end of line and start of commentSublime Text 3 - 结束键切换行尾和评论开始
【发布时间】:2016-04-13 11:38:03
【问题描述】:

如果我没记错的话,在 Eclipse IDE 中,您可以按 End 键并转到行尾,然后再次转到代码行的末尾,在代码行的开头之前评论。下面是一个以| 为光标的示例:

          var str = 'press end to move the cursor here:'| // then here:|

这与您按下 Home 时非常相似,它会转到行首,然后再次按下会将光标移动到代码的开头,如下所示:

|        |var str = 'press home to toggle cursor position';

有人知道如何在 Sublime Text 3 中实现此功能吗?

【问题讨论】:

    标签: sublimetext3 sublimetext


    【解决方案1】:

    Sublime Text 原生的 movemove_to 命令不支持作用域或 cmets 作为参数,因此需要在 Python 中创建一个插件来实现此行为,并绑定 End 关键。

    从 Sublime Text 的 Tools 菜单中,单击 New Plugin。 将内容替换为以下内容:

    import sublime, sublime_plugin
    
    class MoveToEndOfLineOrStartOfCommentCommand(sublime_plugin.TextCommand):
        def run(self, edit):
            new_cursors = []
            for cursor in self.view.sel():
                cursor_end_pos = cursor.end()
                line_end_pos = self.view.line(cursor_end_pos).end()
                if line_end_pos == cursor_end_pos and self.view.match_selector(line_end_pos, 'comment'): # if the cursor is already at the end of the line and there is a comment at the end of the line
                    # move the cursor to the start of the comment
                    new_cursors.append(sublime.Region(self.view.extract_scope(line_end_pos).begin()))
                else:
                    new_cursors.append(sublime.Region(line_end_pos)) # use default end of line behavior
            
            self.view.sel().clear()
            self.view.sel().add_all(new_cursors)
            self.view.show(new_cursors[0]) # scroll to show the first cursor, if it is not already visible
    

    将其保存在ST建议的文件夹中,名称不重要,只要扩展名是.py。 (键绑定引用的命令名基于 Python 代码/类名,而不是文件名。) 转到 Preferences 菜单 -> Key Bindings - User 并插入以下内容:

    { "keys": ["end"], "command": "move_to_end_of_line_or_start_of_comment" }
    

    当按End键时,它会像往常一样移动到行尾,除非它已经在行尾,并且有注释,在这种情况下它会移到评论的开头。

    请注意,这与您的示例略有不同:

    var str = 'press end to move the cursor here:'| // then here:|

    因为它会将光标移动到代码末尾的空格之后,如下所示:

    var str = 'press end to move the cursor here:' |// then here:|

    但它应该为您提供一个工作框架。您可以使用viewsubstr 方法来获取某个区域的字符,因此您可以很容易地使用它来检查空格。

    编辑:请注意,自从编写了此答案以来,我已经为此功能创建了一个包,其中包含一些额外的注意事项、自定义和用例支持,如该问题的另一个答案中所述。

    【讨论】:

    • 不错。空格不是问题。我觉得没有必要在问题中提及它,但我正在考虑这个结果。非常感谢!
    • 您不记得提到如何保存文件以及文件名对键绑定步骤的含义。我建议您编辑以将其放入以完成。我自己也不知道,因为我以前没有做过。
    【解决方案2】:

    多年后,我偶然发现了这个包:https://github.com/SublimeText/GoToEndOfLineOrScope

    Sublime 的作用域文档不是很容易理解,因此需要进行一些挖掘和反复试验,但这里有一个键绑定使它运行良好:

    {
        "keys": ["end"],
        "command": "move_to_end_of_line_or_before_specified_scope",
        "args": {
            "scope": "comment.line",
            "before_whitespace": true,
            "eol_first": false
        }
    },
    

    这使得 End 键在行尾和注释分隔符左侧之间切换,在任何尾随代码的空格之前。它还将首先移动到代码末尾,然后是行尾,将按键保存在预期行为的位置。虽然很明显,但很容易调整。

    comment 范围(如上面 Keith Hall 的回答中所用)也可以,但我不喜欢块/多行 cmets 中的切换行为,所以 comment.line 是一个不错的发现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 2021-07-12
      相关资源
      最近更新 更多