光标半屏时自动移到顶部行首

请教一个问题

当光标位于半屏任意位置,怎么让光标自动跳转到顶部行首?

补充一下,在C-x 2之后的窗口也实现上述功能

就是说在分割了多个窗口的情况下,当光标位于当前窗口一半的任意位置时,自动调整到该窗口的顶部行首,请问该如何做

不是 M-<M-> 嘛?

M-xbeginning-of-buffer 当然,乃也可搜搜 beginning-xxx 的那些

想做成自动跳转

能否给个例子?

scroll-margin 调整到一半?

你好,刚看到

我还是没有描述清楚

具体情况是这样

在写org文档的时候,当输入光标位于当前窗口一半的那一行行首的时候,能够自动把光标所在位置移动到当前窗口的第一行行首

这个我没有试过,晚上有时间去试试看,感谢推荐

说的准确一些,你是要goto-char到当前窗口的第一行,还是你只是想把当前行放到窗口的第一行(光标下的char不变)

是把当前行放到当前窗口第一行,光标下的char位置不变

  (setq scroll-conservatively 0)

描述问题要清楚一点

恩,知道了,有时候不知道用什么专业术语,谢谢你的指点,马上去试试看

没有起作用,加入代码

当光标位于窗口半屏的那一行,并没有自动跳转到第一行的位置

这不是我要的功能

我的需求是

在Org mode

当文字输入到Org窗口高度的一半,换行的同时光标输入位置自动跳转到该窗口的第一行首

重点一是要把换行输入的位置变到第一行首,另一个点是自动执行这个操作,不需要手动按快捷键

不知道这个功能可不可以实现

再次感谢上面给我建议的朋友们

1 个赞

试试 alway center mode(名字大约是这个,记不清了)

好的,感谢你的建议

(defun my/newline-maybe-display-cursor-at-top ()
  "Newline, if going past half of window, display cursor at top of window (without moving point)."
  (interactive)
  (if (eq major-mode 'org-mode)
      (org-return)
    (newline))
  (let ((distance-to-top-of-window (- (line-number-at-pos)
                                      (save-excursion
                                        (move-to-window-line 0)
                                        (line-number-at-pos))))
        (scroll-preserve-screen-position nil))
    (when (> (1+ distance-to-top-of-window)
             (/ (window-height) 2))
      (scroll-up distance-to-top-of-window))))

参考了以下代码:

(evil-define-motion evil-window-top (count)
  "Move the cursor to line COUNT from the top of the window
on the first non-blank character."
  :jump t
  :type line
  (move-to-window-line (max (or count 0)
                            (if (= (point-min) (window-start))
                                0
                              scroll-margin)))
  (back-to-indentation))

(evil-define-motion evil-window-middle ()
  "Move the cursor to the middle line in the window
on the first non-blank character."
  :jump t
  :type line
  (move-to-window-line
   (/ (1+ (save-excursion (move-to-window-line -1))) 2))
  (back-to-indentation))

(evil-define-motion evil-window-bottom (count)
  "Move the cursor to line COUNT from the bottom of the window
on the first non-blank character."
  :jump t
  :type line
  (move-to-window-line (- (max (or count 1) (1+ scroll-margin))))
  (back-to-indentation))

(evil-define-command evil-scroll-line-down (count)
  "Scrolls the window COUNT lines downwards."
  :repeat nil
  :keep-visual t
  (interactive "p")
  (let ((scroll-preserve-screen-position nil))
    (scroll-up count)))

感谢你的回复,我刚测试了,手动执行是完全满足我的需求的,不过能不能在org模式里面自动执行,我测试打到半屏之后,并不会自动跳到第一行首

(define-key org-mode-map (kbd "return") my/newline-maybe-display-cursor-at-top)

这句代码报错

重现过程,新建.Emacs,添加你的两段代码,执行emacs --devug-init,,提示信息如下

Void variable org-mode-map

因为org还没加载,要用

(with-eval-after-load 'org
  ...)

包起来

或者用general,自动defer到map有了才bind (强烈推荐,大部分时候只要用general-define-key):

For example, the following are all equivalent:

(general-define-key
 :keymaps 'org-mode-map
 "M-n" 'org-next-visible-heading
 "M-p" 'org-previous-visible-heading)

(general-emacs-define-key org-mode-map
  "M-n" 'org-next-visible-heading
  "M-p" 'org-previous-visible-heading)

;; rough equivalent with define-key
(with-eval-after-load 'org-mode
  (define-key org-mode-map (kbd "M-n") 'org-next-visible-heading)
  (define-key org-mode-map (kbd "M-p") 'org-previous-visible-heading))