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

时间:2013-06-20 13:47:44

标签: cursor sublimetext3 sublimetext2

我使用ST2在OS X 10.8.4上。当我使用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} }
]

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

提前致谢。

3 个答案:

答案 0 :(得分:13)

Just using Fn+up to pageup and Fn+down to pagedown.

答案 1 :(得分:6)

要做到这一点,需要一个文本插件。感谢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更新:这个目前还不清楚,并且还使用了一个可以想象将来会死的裸网址。所以这里有一个更完整的解释,你需要做什么。

  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 } }
    ]
    
  2. 在Sublime Text中,选择工具>菜单栏中的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})
    
  4. 将文件保存到〜/ Library / Application Support / Sublime Text 2 / Packages / ScrollLinesFixed /。您需要创建ScrollLinesFixed文件夹。
  5. 没有第5步。

答案 2 :(得分:3)

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

{ "keys": ["super+up"], "command": "scroll_lines", "args": {"amount": 1.0} },
{ "keys": ["super+down"], "command": "scroll_lines", "args": {"amount": -1.0} }

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

相关问题