这适用于GNU Emacs,我没有尝试使用XEmacs。
(defun My-smart-home () "Odd home to beginning of line, even home to beginning of text/code." (interactive) (if (and (eq last-command 'My-smart-home) (/= (line-beginning-position) (point))) (beginning-of-line) (beginning-of-line-text)) ) (global-set-key [home] 'My-smart-home)
现在有一个包可以做到这一点, mwim (移动我的意思)
mwim
感谢这个方便的功能。我现在一直使用它并喜欢它。我做了一个小改动: (互动) 变为: (互动“^”)
来自emacs帮助: 如果字符串以。开头 ^' and shift-select-mode'是非零的,Emacs首先调用函数`handle-shift-select'。
^' and
基本上,如果使用shift-select-mode,这会使shift-home从当前位置选择到行的开头。它在迷你缓冲器中特别有用。
(defun smart-beginning-of-line () "Move point to first non-whitespace character or beginning-of-line. Move point to the first non-whitespace character on this line. If point was already at that position, move point to beginning of line." (interactive "^") ; Use (interactive) in Emacs 22 or older (let ((oldpos (point))) (back-to-indentation) (and (= oldpos (point)) (beginning-of-line)))) (global-set-key [home] 'smart-beginning-of-line)
我不太确定聪明的结局会做什么。你通常有很多尾随空格吗?
的 注意: 强> 这个函数和Robert Vukovi之间的主要区别在于,即使光标已经存在,他总是会移动到第一个按键上的第一个非空白字符。在这种情况下,我将移至第0列。
而且,他用过 (beginning-of-line-text) 我用过的地方 (back-to-indentation) 。这些非常相似,但它们之间存在一些差异。 (back-to-indentation) 始终移动到一行上的第一个非空白字符。 (beginning-of-line-text) 有时会移动它认为无关紧要的非空白字符。例如,在仅注释行上,它移动到注释文本的第一个字符,而不是注释标记。但是,根据您喜欢的行为,可以在我们的任何一个答案中使用任一函数。
(beginning-of-line-text)
(back-to-indentation)
请注意,已经有一个返回缩进功能,可以执行您希望第一个智能家庭功能执行的操作,即转到该行上的第一个非空白字符。它默认绑定到M-m。
我改编@Vucovic代码跳转到 beggining-of-line 第一:
beggining-of-line
(defun my-smart-beginning-of-line () "Move point to beginning-of-line. If repeat command it cycle position between `back-to-indentation' and `beginning-of-line'." (interactive "^") (if (and (eq last-command 'my-smart-beginning-of-line) (= (line-beginning-position) (point))) (back-to-indentation) (beginning-of-line))) (global-set-key [home] 'my-smart-beginning-of-line)