Emacs:滚动缓冲区而不是光标位置

26

在Emacs中,是否可以滚动整个可见的缓冲区,但不改变光标位置。例如:光标位于窗口底部附近,我希望查看一些已经滚动到窗口顶部之外的文本,而不移动光标。

编辑:我想C-l C-l有点类似于我想要的效果。


C- l 清空屏幕 - Billal Begueradj
1
在终端中可以,但在Emacs中不行。 - SabreWolfy
6个回答

18

试试这些。根据您的口味更改 M-nM-p 键绑定。

;;; scrollers
(global-set-key "\M-n" "\C-u1\C-v")
(global-set-key "\M-p" "\C-u1\M-v")

4
我使用类似的东西:(global-set-key "\M-n" (lambda () (interactive) (scroll-up 4)) )(global-set-key "\M-p" (lambda () (interactive) (scroll-down 4)) ) - Ben
11
很遗憾,当光标碰到窗口的上下边缘后,它仍将继续移动。我想知道是否有可能避免这种情况(而不需要重新制作Emacs本身)... - celwell
@celwell:没错,但作为可行的解决方案,我很满意。 - SabreWolfy
@celwell 有一些变量可以控制光标对窗口边界的灵敏度。也许你可以把向下滚动的部分放在一个“let”块里面?另外一个可以尝试的方法是“save-excursion”,不过我不确定它是否适用于滚动命令。 - ksoo

11
;;;_*======================================================================
;;;_* define a function to scroll with the cursor in place, moving the
;;;_* page instead
;; Navigation Functions
(defun scroll-down-in-place (n)
  (interactive "p")
  (previous-line n)
  (unless (eq (window-start) (point-min))
    (scroll-down n)))

(defun scroll-up-in-place (n)
  (interactive "p")
  (next-line n)
  (unless (eq (window-end) (point-max))
    (scroll-up n)))

(global-set-key "\M-n" 'scroll-up-in-place)
(global-set-key "\M-p" 'scroll-down-in-place)

1
这应该是正确的答案,相对于光标/指针滚动到正确的位置。谢谢!! - cevaris
1
为了避免编译器警告,我用“(forward-line (* -1 n))”代替了“(previous-line n)”和“(forward-line n)”代替了“(next-line n)”。 - garyp
这实际上是有效的,但只有当我交换previous-linenext-line时才有效,否则它会跳过两行。 - E. Sambo
2
由于“M-p”和“M-n”通常被保留用于其他操作,因此我将它们绑定到“C-S-y”和“C-S-e”,因为它们与Vim的“CTRL_e”和“CTRL_y”大致相同。 - Micah Elliott

8

这可能会有用。根据EmacsWiki上的滚动页面;

变量scroll-preserve-screen-position可能对某些人有用。 当您向下滚动,然后再向上滚动时,光标应该停留在您开始的相同位置。 该值可以通过内置模式M-x scroll-lock-mode进行切换。


3
我认为这样更好:
(defun gcm-scroll-down ()
      (interactive)
      (scroll-up 1))
    (defun gcm-scroll-up ()
      (interactive)
      (scroll-down 1))
    (global-set-key [(control down)] 'gcm-scroll-down)
    (global-set-key [(control up)]   'gcm-scroll-up)

reference : emacs wiki


1
你为什么认为这样更好?(你的代码做了什么?) - ksoo
它确实更好:它移动视图,而不是点;非常感谢@Bilal。 - yPhil

1

根据Bilal的回答:

(global-set-key [(meta down)] (lambda () (interactive) (scroll-down 1)))
(global-set-key [(meta up)] (lambda () (interactive) (scroll-up 1)))

0
;; Preserve the cursor position relative to the screen when scrolling
(setq scroll-preserve-screen-position 'always)

;; Scroll buffer under the point
;; 'scroll-preserve-screen-position' must be set to a non-nil, non-t value for
;; these to work as intended.
(global-set-key (kbd "M-p") #'scroll-down-line)
(global-set-key (kbd "M-n") #'scroll-up-line)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接