Sublimetext3 升华文本3-结束键切换行的结束和注释的开始

Sublimetext3 升华文本3-结束键切换行的结束和注释的开始,sublimetext3,sublimetext,Sublimetext3,Sublimetext,如果我没记错的话,在EclipseIDE中,您可以按End键转到一行的末尾,然后再次转到代码行的末尾,在注释开始之前。下面是一个以|为光标的示例: var str = 'press end to move the cursor here:'| // then here:| 这与按Home键时光标移到行首,然后再按一次键将光标移到代码的开头非常相似,如下所示: | |var str = 'press home to toggle cursor position'

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

          var str = 'press end to move the cursor here:'| // then here:|
这与按Home键时光标移到行首,然后再按一次键将光标移到代码的开头非常相似,如下所示:

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

有人知道如何在升华文本3中实现此功能吗?

升华文本的原生
move
move\u to
命令不支持作用域或注释作为参数,因此有必要在Python中创建插件来实现此行为,并将结束键绑定到它

从Sublime文本中的
工具
菜单中,单击
新建插件
。 将内容替换为以下内容:

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
{ "keys": ["end"], "command": "move_to_end_of_line_or_start_of_comment" }
将其保存在文件夹中,只要扩展名为
.py
,名称就不重要。(keybinding引用的命令名基于Python代码/类名,而不是文件名。) 转到
首选项
菜单->
键绑定-用户
并插入以下内容:

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
{ "keys": ["end"], "command": "move_to_end_of_line_or_start_of_comment" }
按结束键时,它将像往常一样移动到行的末尾,除非它已经在行的末尾,并且有注释,在这种情况下,它将移动到注释的开头

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

var str='按end键将光标移到此处:'|//然后移到此处:|

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

|        |var str = 'press home to toggle cursor position';
var str='按end键将光标移到此处:'|//然后移到此处:|

但它应该为您提供一个工作框架。您可以使用
view
substr
方法来获取特定区域中的字符,因此可以非常轻松地使用该方法检查空格


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

几年后,我偶然发现了此软件包:

Sublime的范围文档不是非常容易理解,因此需要进行一些挖掘和尝试,但这里有一个关键绑定,使其工作得非常好:

{
    "keys": ["end"],
    "command": "move_to_end_of_line_or_before_specified_scope",
    "args": {
        "scope": "comment.line",
        "before_whitespace": true,
        "eol_first": false
    }
},
这使得结束键在行的末尾和注释分隔符的左侧之间切换,在跟踪代码的任何空白之前切换。它还将首先移动到代码的末尾,然后移动到行的末尾,并将按键保存在预期行为的位置。很明显,这很容易调整


comment
作用域(正如上面在Keith Hall的回答中所使用的)也可以工作,但我不喜欢块/多行注释中的切换行为,因此
comment.line
是一个很好的发现。

很好。空格不是问题。我觉得没有必要在问题中提到它,但我正在考虑这个结果。非常感谢!您不记得提到如何保存文件,可能还没有提到文件名对密钥绑定步骤的含义。我建议你编辑一下,把它放进去,以便完成。我自己也不知道,因为我以前从未做过。@MichaelIyke谢谢你的反馈,答案更新:)