Sublime Text 2:如何在不移动光标的情况下向上/向下翻页

Nic*_* K9 15 cursor sublimetext2 sublimetext3

我在OS X 10.8.4上使用ST2.当我使用Home和End键时,视口移动并且光标保持不变.这是标准的Mac行为,也是我所期待的.

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

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

[
   { "keys": ["pageup"], "command": "scroll_lines", "args" : {"amount": 30.0} },
   { "keys": ["pagedown"], "command": "scroll_lines", "args" : {"amount": -30.0} }
]
Run Code Online (Sandbox Code Playgroud)

但是,金额是硬编码的.看起来viewport_extent会让我获得视口的高度,但是如何在键绑定文件中使用它呢?这甚至是正确的解决方案吗?我觉得要获得这种行为是一项非常艰巨的工作.

提前致谢.

Eri*_*Guo 16

只是Fn+up用于翻页和Fn+down分页.


Nic*_* K9 7

为此,需要一个文本插件。感谢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,可以想象将来会消失。因此,这里是您需要做什么的更完整的说明。

  1. 将这四行添加到Sublime Text 2>首选项>键绑定-用户中,该文件已在任何方括号内:

    [
        { "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 } }
    ]
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在Sublime Text中,从菜单栏中选择Tools> New Plugin…选项。
  3. 以此替换新文件的内容:

    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})
    
    Run Code Online (Sandbox Code Playgroud)
  4. 将文件保存到〜/ Library / Application Support / Sublime Text 2 / Packages / ScrollLinesFixed /。您将需要创建ScrollLinesFixed文件夹。
  5. 没有步骤5。


and*_*gle 5

只是我的 2 美分,但我的设置可以使用以下内容向上或向下滚动:

{ "keys": ["super+up"], "command": "scroll_lines", "args": {"amount": 1.0} },
{ "keys": ["super+down"], "command": "scroll_lines", "args": {"amount": -1.0} }
Run Code Online (Sandbox Code Playgroud)

我使用的是 Mac,所以“超级”键是命令键,它是空格键左侧(或右侧)的第一个键。不确定 Windoze 上的等价物是什么;也许它是“开始”键或其他东西。无论如何,就像一个魅力。

  • 将 "amount": 1.0 更改为 "amount": 30.0 使其一次滚动 30 行。足以满足我的需求。谢谢! (2认同)