【问题标题】:Shortcut to move to a specific part of a text file移动到文本文件特定部分的快捷方式
【发布时间】:2019-08-11 05:44:30
【问题描述】:

我想创建一个键盘快捷键(例如 CTRL+T),在出现固定文本后自动将光标移动到该行,例如作为&todo

例子:

foo 
bar
&todo
fix bug #783
blah
blah2

CTRL+T 会自动将光标移动到以 fix ... 开头的行。

目前我正在这样做:

  • CTRL F
  • 输入&todo输入
  • ESCAPE(关闭Search底部面板)
  • 首页
  • 向下箭头(移动到下一行)

但这需要太多的操作。

如何在一个快捷键中做到这一点?

【问题讨论】:

    标签: keyboard-shortcuts sublimetext2 sublimetext sublime-text-plugin


    【解决方案1】:

    最好的解决方案是使用插件来做到这一点。

    下面的插件可以满足您的需求。它将在当前光标位置下方找到下一个出现的pattern(即&todo 标记),将光标移动到其下方的行,并在窗口中居中该位置。如果在当前光标位置下方找不到pattern,它将从缓冲区顶部再次搜索,提供环绕功能。

    将以下 Python 代码复制并粘贴到缓冲区中,并将其保存在 Sublime Text 配置 User 文件夹中为 GoToPattern.py

    import sublime
    import sublime_plugin
    
    class GotoPatternCommand(sublime_plugin.TextCommand):
    
        def run(self, edit, pattern):
    
            sels = self.view.sel()
            # Optional flags; see API.
            flags = sublime.LITERAL | sublime.IGNORECASE
            start_pos = sels[0].end() if len(sels) > 0 else 0
            find_pos = self.view.find(pattern, start_pos, flags)
    
            if not find_pos and start_pos > 0:
                # Begin search again at the top of the buffer; wrap around
                # feature, i.e. do not stop the search at the buffer's end.
                find_pos = self.view.find(pattern, 0, flags)
    
            if not find_pos:
                sublime.status_message("'{}' not found".format(pattern))
                return
    
            sels.clear()
            sels.add(find_pos.begin())
            self.view.show_at_center(find_pos.begin())
            row, col = self.view.rowcol(find_pos.begin())
            self.view.run_command("goto_line", {"line": row + 2})
            # Uncomment for: cursor to the end of the line.
            # self.view.run_command("move_to", {"to": "eol"})
    

    添加键绑定:

    // The pattern arg, i.e. "&todo", can be changed to anything you want
    // and other key bindings can also be added to use different patterns.
    {"keys": ["???"], "command": "goto_pattern", "args": {"pattern": "&todo"}}
    

    如果需要,可以将命令面板条目添加到 Default.sublime-commands

    {"caption": "GoToPattern: &todo", "command": "goto_pattern", "args": {"pattern": "&todo"}},
    

    这些链接可能对您有用 ST v. 2 APIST v. 3 API

    附:你知道 Sublime Text 有书签吗? [以防万一你没有。]

    【讨论】:

      【解决方案2】:

      accepted answer 真的更好,我终于用上了。

      作为参考,这里我使用了一个旧解决方案:首先在"C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\" 中创建一个gototodo.py 文件,其中包含:

      import sublime, sublime_plugin
      
      class GototodoCommand(sublime_plugin.TextCommand):
          def run(self, edit):
              contents = self.view.substr(sublime.Region(0, self.view.size()))  # https://stackoverflow.com/questions/20182008/sublime-text-3-api-get-all-text-from-a-file
              a = contents.find('&todo')
              cursors = self.view.sel()
              cursors.clear()
              location = sublime.Region(a, a)
              cursors.add(location)
              self.view.show_at_center(location)
      
              (row, col) = self.view.rowcol(self.view.sel()[0].begin())  # go to the next line
              self.view.run_command("goto_line", {"line": row+2})
      

      然后在"C:\Users\User\AppData\Roaming\Sublime Text 2\Packages\User\Default (Windows).sublime-keymap"中添加这个:

      { "keys": ["ctrl+t"], "command": "gototodo" }
      

      完成!

      【讨论】:

      • 不需要import subprocess。您的插件只会将光标移动到“&todo”的第一次出现处。
      • Sublime Text 版本 3 已经稳定了 5 年多,几乎可以肯定比 2013 年最后一次更新的非常旧的版本 2 更好的选择。版本 3 已经退出测试版好几年了这是SublimeText.com上的默认下载。
      • @mattst 你是对的subprocess,我第一次包含它是因为完全不同的东西(几乎是另一个插件),但你是对的,它应该被删除。我现在就编辑。
      • @mattst 关于 ST 版本:这里的代码会在 ST3 上完美运行还是需要在这里进行一些修改?
      • 不需要修改,它工作正常,我测试了它以确保。请注意,在您从版本 2 转换到 3 时,ST2 和 ST3 可以同时安装在同一台计算机上。
      猜你喜欢
      • 2013-01-20
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多