sublimetext3 - Sublime Text 2 : How to Page Up/Down without moving the cursor

标签 sublimetext3 sublimetext2 text-cursor

我在使用 ST2 的 OS X 10.8.4 上。当我使用 Home 和 End 键时,视口(viewport)会移动,而光标会被单独留下。这是标准的 Mac 行为,也是我所期望的。

但是,当我使用 Page Up (pageup/pgup) 和 Page Down (pagedown/pgdn) 时,光标会随着视口(viewport)移动。这不是其他 Mac 应用程序的行为方式,我也希望将光标单独留在这些键上。

通过将其添加到我的键绑定(bind)中,我已经能够完成这个工作:

[
   { "keys": ["pageup"], "command": "scroll_lines", "args" : {"amount": 30.0} },
   { "keys": ["pagedown"], "command": "scroll_lines", "args" : {"amount": -30.0} }
]

但是,那里的金额是硬编码的。看起来 viewport_extent 会让我得到视口(viewport)的高度,但我怎样才能在键绑定(bind)文件中使用它呢?这甚至是正确的解决方案吗?我觉得要获得这种行为是一项非常艰巨的工作。

提前致谢。

最佳答案

为此需要一个文本插件。感谢 ST 论坛上的用户 bizoo,您不必自己编写以下代码:

http://www.sublimetext.com/forum/viewtopic.php?f=3&t=12793

这完全符合我的预期。

Sublime Text 3 更新:您可以按照以下说明进行操作,稍作更改,文件应以 .py 结尾(例如 scroll_lines_fixed.py )并且在 ~/Library/Application Support/Sublime Text 3/Packages/User/ 中应该是松散的文件夹。

Sublime Text 2 更新:这还不清楚,而且还使用了一个裸露的 URL,可以想象将来会死掉。所以这里有一个关于你需要做什么的更完整的解释。

  • 将这四行添加到 Sublime Text 2 > Preferences > Key Bindings - User,在文件中已经存在的任何方括号内:
    [
        { "keys": ["ctrl+up"], "command": "scroll_lines_fixed", "args": {"amount": 1.0 } },
        { "keys": ["ctrl+down"], "command": "scroll_lines_fixed", "args": {"amount": -1.0 } },
        { "keys": ["pageup"], "command": "scroll_lines_fixed", "args" : {"by": "pages", "amount": 1.0 } },
        { "keys": ["pagedown"], "command": "scroll_lines_fixed", "args" : {"by": "pages", "amount": -1.0 } }
    ]
    
  • 在 Sublime Text 中,从菜单栏中选择 Tools > New Plugin... 选项。
  • 用这个替换新文件的内容:
    import sublime, sublime_plugin
    
    class ScrollLinesFixedCommand(sublime_plugin.TextCommand):
       """Must work exactly as builtin scroll_lines command, but without moving the cursor when it goes out of the visible area."""
       def run(self, edit, amount, by="lines"):
          # only needed if one empty selection
          if by != "lines" or (len(self.view.sel()) == 1 and self.view.sel()[0].empty()):
             maxy = self.view.layout_extent()[1] - self.view.line_height()
             curx, cury = self.view.viewport_position()
             if by == "pages":
                delta = self.view.viewport_extent()[1]
             else:
                delta = self.view.line_height()
             nexty = min(max(cury - delta * amount, 0), maxy)
             self.view.set_viewport_position((curx, nexty))
          else:
             self.view.run_command("scroll_lines", {"amount": amount})
    
  • 将文件保存到 ~/Library/Application Support/Sublime Text 2/Packages/ScrollLinesFixed/。您将需要创建 ScrollLinesFixed 文件夹。
  • 没有第 5 步。
  • 关于sublimetext3 - Sublime Text 2 : How to Page Up/Down without moving the cursor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215334/

    相关文章:

    python - 使用 SublimeREPL 和 Python 3 进行 Sublime 构建

    macos - 在 Sublime Text 3 中执行安全转储信任设置时出错

    sublimetext - 颠倒Sublime Text中的所有文本行

    jquery - 如何将 Jquery 添加到 Sublime Text 2?

    android - 在 Android 3.0 中更改 EditText 的光标颜色

    sublimetext3 - 带有 Sublime Text 编辑器的阿拉伯语

    lisp - 普通口齿不清 : Hunchentoot and the REPL - When I start the server I get log output in the REPL and can't use the REPL any more

    java - Sublime Text 2 无法构建

    javascript - 如何在 Ckeditor 的光标位置放置一个 html 元素?

    PhpStorm 插入和覆盖光标样式反转