2008-09-28 63 views
36

您是否对Emacs中的家庭钥匙具有智能行为?通过智能我的意思是,而不是去到字符数字0,它应该去第一个非空白字符,并在第二次按下去0,并回到第三个非空白的第三个,依此类推。 聪明结束也会很好。Emacs中的智能家居

回答

58
(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) 

我不太确定什么聪明的结局会做。你通常有很多尾随空白吗?

注意:这个函数和RobertVuković's的主要区别在于,即使光标已经在那里,他总是会移动到第一个按键上的第一个非空白字符。在这种情况下,矿井将移动到第0列。

另外,他用(beginning-of-line-text)我用(back-to-indentation)。这些非常相似,但它们之间有一些差异。 (back-to-indentation)总是移动到一行上的第一个非空白字符。 (beginning-of-line-text)有时会移过它认为不重要的非空白字符。例如,在只有评论的行中,它会移至评论文本的第一个字符,而不是评论标记。但是任何一个函数都可以用在我们的答案中,这取决于你喜欢哪种行为。

+1

遗憾的是它不与CUA工作启用,在这种情况下,选择换档不起作用,换回家选择完整的行不起作用。有什么建议么? – 2010-05-27 08:40:28

+0

@亚历山大·斯托尔兹,我从来不使用cua模式,但尝试在'defun'之后添加'(把'聪明开始'的'CUA'动作)'(即在'global-set-key`之前添加'线)。 – cjm 2010-05-27 09:18:14

12

这适用于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) 
4

注意,已经有一个背到缩进功能,你想要做什么第一智能家居功能做的,也就是去上线的第一个非空白字符。它默认绑定到M-m。

6

感谢这个方便的功能。我现在一直都在使用它并喜欢它。我所做的只是其中很小的变化: (互动) 变为: (交互式“^”)

从Emacs帮助: 如果字符串以^' and按住Shift键选择模式”开始为非nil,那么Emacs首先调用函数`handle-shift-select'。

基本上,如果您使用shift-select-mode,则可以从当前位置移动到行首。它在微型缓冲器中特别有用。

0

我适应@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) 
2

现在有一个包,做到了这一点,mwim(移动在哪里我的意思)