【问题标题】:emacs equivalent of vim's shift-h and shift-lemacs 相当于 vim 的 shift-h 和 shift-l
【发布时间】:2010-12-03 22:28:01
【问题描述】:

我想知道emacs 是否内置了这些光标移动命令,或者我是否必须编写它们或在某处找到sn-p。我发现它们非常漂亮,并经常在 vim 中使用它们。到目前为止,我还没有在 emacs 的文档中找到它们。

在 vim 中,它们看起来像这样: shift-h -> 将光标移动到屏幕顶部 shift-m -> 将光标移动到屏幕中间 shift-l -> 将光标移动到屏幕底部

澄清一下,我不想将光标移动到文档的顶部/底部,只是移动到文档当前可见部分的顶部/底部,即当前显示在屏幕上的部分。

到目前为止,我找到了其中一个。 alt-r 似乎相当于 vim 的 shift-m。它将光标移动到中间行的第一列。

【问题讨论】:

    标签: vim emacs navigation cursor-position


    【解决方案1】:

    用途:

    • Alt+0 Alt+r - 窗口顶部
    • Alt+- Alt+r - 窗口底部

    严格来说,屏幕顶部应写为M-0 M-r,屏幕底部应写为M-- M-r。其中M 表示Meta 键,通常映射到Alt

    我按如下方式计算出这些击键:

    M-r 运行命令move-to-window-line。我用C-h k M-r 发现了这一点,即。 Ctrl+hkAlt+rThe key sequence C-h k means tell me what the next key sequence does. 它告诉我命令名称,并且您可以将数字参数传递给命令以选择您要转到的行。如果您不传递任何内容,它会将点移动到窗口的中间,如您所见。

    您通过typing a number while holding down Meta 将数字参数传递给命令。减号本身被视为-1。现在,要移动到屏幕顶部,我们要通过第 0 行和屏幕底部的第 -1 行。这给了我们上面的关键序列。

    如果您想将move-to-window-line 绑定到不同的键look at Joe's answer to this question

    【讨论】:

    • 使用: - M-= M-r - 窗口中间
    • smartparens 将 M-r 绑定到 sp-splice-sexp-killing-around,因此您可能会发现将 Emacs 默认值 (move-to-window-line-top-bottom) 更改为 M-R 很有帮助。
    【解决方案2】:

    你要使用的函数是move-to-window-line,其定义是:

    move-to-window-line is an interactive built-in function in `C source
    code'.
    
    It is bound to M-r.
    (move-to-window-line arg)
    
    Position point relative to window.
    With no argument, position point at center of window.
    An argument specifies vertical position within the window;
    zero means top of window, negative means relative to bottom of window.
    

    您可以使用0 调用它以转到页面顶部,使用-1 调用它以转到页面底部。这些可以绑定到具有匿名函数或命名函数的键。两者都有例子。

    匿名函数

    (global-set-key [(f4)] (function
                            (lambda ()
                              "Go to top of page."
                              (interactive)
                              (move-to-window-line 0))))
    
    (global-set-key [(f4)] (function
                            (lambda ()
                              "Go to bottom of page."
                              (interactive)
                              (move-to-window-line -1))))
    

    命名函数

    (defun my-top-of-page ()
      "Go to top of page."
      (interactive)
      (move-to-window-line 0))
    
    (defun my-bottom-of-page ()
      "Go to bottom of page."
      (interactive)
      (move-to-window-line -1))
    
    (global-set-key [(f4)] 'my-top-of-page)
    (global-set-key [(shift f4)] 'my-bottom-of-page)
    

    【讨论】:

      【解决方案3】:

      在 Emacs 23.2 中,M-r 完全符合您的要求。

      此命令的第一次调用将点移动到当前可见窗口的中心,接下来的连续调用将移动到顶部和底部。

      无需额外配置或自定义功能。

      【讨论】:

        【解决方案4】:

        要补充 Joe 和 Dave 的答案,您可以使用以下方法:

        (defun bp-goto-center()
          "move cursor to middle line"
          (interactive)
          (move-to-window-line (/ (window-height) 2)))
        

        (我将bp 添加到我所有函数的前面,以将它们与内置函数或其他人的函数区分开来......随意删除它。)

        【讨论】:

        • 我会 +1 这个答案,但你的代码块没有格式化(是的,我很迂腐!)
        • 我需要执行以下操作才能让中间人工作,(move-to-window-line (/ (window-buffer-height (get-buffer-window)) 2))
        【解决方案5】:

        我发现move-to-window-line 不尊重window-buffer-height。因此,当缓冲区大小小于窗口时,它实际上并没有提供 Vim 的行为。因为我不使用终端的 Emacs,也不关心最小化它的框架,我选择重用它的前缀键,因为 'z' 让我想起 Vim 的 ztzmzb (也因为我打算将以下内容与 Emacs 的C-l 结合使用以达到相同的整体效果)。

        (define-prefix-command 'ctl-z-map)
        (defun move-to-window-line-top ()
          (interactive)
          (move-to-window-line 0))
        (defun move-to-window-line-middle ()
          (interactive)
          (let* ((wb-height (window-buffer-height (get-buffer-window)))
                (actual-height (if (> wb-height (window-height))
                                   (window-height)
                                 wb-height)))
            (move-to-window-line (/ actual-height 2))))
        (defun move-to-window-line-bottom ()
          (interactive)
          (move-to-window-line -1)
          (beginning-of-line))
        (define-key ctl-z-map (kbd "h") 'move-to-window-line-top)
        (define-key ctl-z-map (kbd "m") 'move-to-window-line-middle)
        (define-key ctl-z-map (kbd "l") 'move-to-window-line-bottom)
        (global-set-key (kbd "C-z") 'ctl-z-map)
        

        【讨论】:

          【解决方案6】:

          如果您使用的是 Emacs 23,则只需 C-l。第一次到中心,第二次到顶部,第三次到底部。

          编辑:

          哎呀,我的错,将当前行放在窗口的中心/顶部/底部。不过还是有用的:)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-03-04
            • 1970-01-01
            相关资源
            最近更新 更多