当前接受的答案满足规定的要求,但有两个主要限制:
- 它仅在使用 进入插入模式时触发i。I使用、A、或任何自定义函数/键绑定跳转到最后一行a需要额外的脚本。
- 如果
point
已经在最后一行(例如,在编辑当前命令时),则无法在当前位置进入插入模式;
i有效反弹至A。
第一个限制可以通过先上钩eshell-mode
然后再上钩来消除evil-insert-state-entry
。
第二个限制可以通过设置point
位置来解决,首先基于行号,其次基于只读文本属性:
- 如果
point
不在最后一行,则将其移至point-max
(始终为读写)。
- 否则,如果文本 at
point
是只读的,point
则移动到当前行中只读文本属性更改的位置。
以下代码否定了已接受答案的限制。
(defun move-point-to-writeable-last-line ()
"Move the point to a non-read-only part of the last line.
If point is not on the last line, move point to the maximum position
in the buffer. Otherwise if the point is in read-only text, move the
point forward out of the read-only sections."
(interactive)
(let* ((curline (line-number-at-pos))
(endline (line-number-at-pos (point-max))))
(if (= curline endline)
(if (not (eobp))
(let (
;; Get text-properties at the current location
(plist (text-properties-at (point)))
;; Record next change in text-properties
(next-change
(or (next-property-change (point) (current-buffer))
(point-max))))
;; If current text is read-only, go to where that property changes
(if (plist-get plist 'read-only)
(goto-char next-change))))
(goto-char (point-max)))))
(defun move-point-on-insert-to-writeable-last-line ()
"Only edit the current command in insert mode."
(add-hook 'evil-insert-state-entry-hook
'move-point-to-writeable-last-line
nil
t))
(add-hook 'eshell-mode-hook
'move-point-on-insert-to-writeable-last-line)