emacs 相当于 vim 的 shift-h 和 shift-l

发布于 2024-08-06 14:35:17 字数 340 浏览 2 评论 0原文

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

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

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

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

i'm wondering if emacs has these cursor movement commands built in or if i'm going to have to write them or find a snippet somewhere. i find them pretty spiffy and use them in vim regularly. i haven't found them in emacs' documentation so far.

in vim, they look like this:
shift-h -> move cursor to the top of the screen
shift-m -> move cursor to the middle of the screen
shift-l -> move cursor to the bottom of the screen

just to clarify, i'm not looking to move the cursor to the top/bottom of the document, just to the top/bottom of the currently visible part of the document, i.e. the part that's currently being displayed on screen.

i found one of them so far. alt-r seems to be the equivalent of vim's shift-m. it moves the cursor to the first column of the middle line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

御守 2024-08-13 14:35:17

使用:

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

严格来说,这些应该写为 M-0 Mr< /code> 表示屏幕顶部,M-- Mr 表示屏幕底部。其中 M 表示 Meta 键,通常映射到 Alt

我计算出这些击键如下:

Mr 运行命令move-to-window-line。我通过 Ch k Mr 发现了这一点,即。 Ctrl+hkAlt+r按键序列 Ch k 意味着告诉我下一个按键序列的作用。 它告诉我命令名称,并且您可以将数字参数传递给命令以选择您想要转到的行。如果您没有通过任何操作,它会将点移动到窗口的中间,如您所见。

您可以通过在按住 Meta 的同时键入数字来将数字参数传递给命令。减号本身表示-1。现在,要移动到屏幕顶部,我们要传递第 0 行,而移动到屏幕底部则传递 -1 行。这给了我们上面的按键序列。

如果要将 move-to-window-line 绑定到不同的键 看看乔对此问题的回答

Use:

  • Alt+0 Alt+r - top of Window
  • Alt+- Alt+r - bottom of Window

Strictly, these should be written as M-0 M-r for the top of the screen and M-- M-r for the bottom of the screen. Where M means the Meta key which is usually mapped to Alt.

I worked out these keystrokes as follows:

M-r runs the command move-to-window-line. I found this out with C-h k M-r, ie. Ctrl+h, k, Alt+r. The key sequence C-h k means tell me what the next key sequence does. It told me the command name and also that you can pass numeric arguments to the command to pick the line you want to go to. If you don't pass any it moves the point to the middle of the window as you have seen.

You pass numeric arguments to commands by typing a number while holding down Meta. A minus sign on its own is taken to mean -1. Now, to move to the top of the screen we want to pass line 0 and for the bottom of the screen line -1. This gives us the key sequences above.

If you want to bind move-to-window-line to a different key look at Joe's answer to this question.

失与倦" 2024-08-13 14:35:17

您希望使用的函数是 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)

The function you wish to use is move-to-window-line, whose definition is:

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.

You would call it with a 0 to go to the top of the page and a -1 to go to the bottom of the page. These can be bound to a key with an anonymous function or a named function. Examples of both are given.

Anonymous Functions

(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))))

Named Functions

(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)
肩上的翅膀 2024-08-13 14:35:17

在 Emacs 23.2 中,Mr 完全按照您的意愿行事。

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

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

In Emacs 23.2, M-r does exactly what you want.

The first invocation of this command moves the point to center of the currently visible window, the next successive invocations move to top and bottom.

No additional configuration or custom functions needed.

悲喜皆因你 2024-08-13 14:35:17

要添加到 Joe 和 Dave 的答案中,您可以使用以下内容:(

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

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

To add to Joe's and Dave's answers, you can get middle with:

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

(I add bp to the front of all my functions to distinguish them from built-ins, or other people's... feel free to remove that.)

窗影残 2024-08-13 14:35:17

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

(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)

I've found that move-to-window-line doesn't respect window-buffer-height. Thus, it doesn't actually provide the behavior Vim has when the buffer size is smaller than the window. Since I don't use Emacs from a terminal, nor do I care about minimizing its frame, I've chosen to reuse its prefix key because the 'z' reminds me of Vim's zt,zm, and zb (and also because I'm going to use the following in combination with Emacs' C-l to achieve the same overall effect).

(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)
少女情怀诗 2024-08-13 14:35:17

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

编辑:

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

If you are using Emacs 23, it's simply C-l. The first time it will go to the center, the second time it will go to the top, the third time it will go to the bottom.

EDIT:

Oops, my bad, that puts the current line in the center/top/bottom of the window. Still useful though :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文