【发布时间】:2010-09-13 19:16:32
【问题描述】:
你能在 Emacs 中为 home 键设置智能行为吗?智能我的意思是,它应该转到第一个非空白字符,然后在第二次按下时转到 0,然后在第三次按下时返回到第一个非空白字符,而不是转到第 0 个字符,以此类推。 拥有智能端也不错。
【问题讨论】:
标签: emacs
你能在 Emacs 中为 home 键设置智能行为吗?智能我的意思是,它应该转到第一个非空白字符,然后在第二次按下时转到 0,然后在第三次按下时返回到第一个非空白字符,而不是转到第 0 个字符,以此类推。 拥有智能端也不错。
【问题讨论】:
标签: emacs
我的版本:移到可视行的开头、第一个非空白或行首。
(defun smart-beginning-of-line ()
"Move point to beginning-of-line or first non-whitespace character"
(interactive "^")
(let ((p (point)))
(beginning-of-visual-line)
(if (= p (point)) (back-to-indentation))
(if (= p (point)) (beginning-of-line))))
(global-set-key [home] 'smart-beginning-of-line)
(global-set-key "\C-a" 'smart-beginning-of-line)
[home] 和 "\C-a"(control+a)键:
interactive "^")。这取自@cjm 和@thomas;然后我添加了视觉线的东西。 (抱歉我的英语不好)。
【讨论】:
(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)
我不太确定 smart end 会做什么。您通常有很多尾随空格吗?
注意:此功能与 Robert Vuković 的主要区别在于,即使光标已经存在,他总是在第一次按键时移动到第一个非空白字符。在这种情况下,我的会移动到第 0 列。
另外,他使用了(beginning-of-line-text),而我使用了(back-to-indentation)。它们非常相似,但它们之间存在一些差异。 (back-to-indentation) 总是移动到一行的第一个非空白字符。 (beginning-of-line-text) 有时会跳过它认为无关紧要的非空白字符。例如,在仅注释行上,它移动到注释文本的第一个字符,而不是注释标记。但是这两个函数都可以在我们的任何一个答案中使用,具体取决于您喜欢哪种行为。
【讨论】:
defun 之后添加 (put 'smart-beginning-of-line 'CUA 'move)(即,就在 global-set-key 行之前)。
(interactive "^") 并添加 ; Use (interactive) with Emacs 22 or older。 Gorlum0(上面的评论)可能不是唯一一个努力让示例代码与班次选择一起工作的人。
(if (version< "22" emacs-version) (interactive "^") (interactive))。但这给了我错误:Wrong type argument: commandp, smart-beginning-of-line。有人介意解释为什么 with 不起作用吗?
现在有一个包可以做到这一点,mwim(移动我的意思)
【讨论】:
我改编@Vucovic代码先跳转到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)
【讨论】:
感谢这个方便的功能。我现在一直使用它并且喜欢它。我只做了一个小改动: (交互的) 变成: (互动“^”)
来自 emacs 帮助:
如果以^' andshift-select-mode' 开头的字符串不为nil,则Emacs 首先调用函数`handle-shift-select'。
如果您使用 shift-select-mode,这基本上会使 shift-home 选择从当前位置到行首。它在 minibuffer 中特别有用。
【讨论】:
请注意,已经有一个返回缩进功能,它可以执行您希望第一个智能家居功能执行的操作,即转到行上的第一个非空白字符。它默认绑定到 M-m。
【讨论】:
这适用于 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)
【讨论】: