【发布时间】:2011-05-17 19:11:31
【问题描述】:
我使用 emacs 进行开发,并且经常需要移动到行首 (C-a)。但是,如果该行是缩进的,我想移动到代码开始的位置。
所以在浏览代码时:( ) for x in xy|z:。在输入 C-a 时,我们得到:|( ) for x in xyz:。但相反,我想要这个:( ) |for x in xyz:
这里 |表示光标,() 表示空格或制表符。
我怎样才能做到这一点?
【问题讨论】:
标签: emacs
我使用 emacs 进行开发,并且经常需要移动到行首 (C-a)。但是,如果该行是缩进的,我想移动到代码开始的位置。
所以在浏览代码时:( ) for x in xy|z:。在输入 C-a 时,我们得到:|( ) for x in xyz:。但相反,我想要这个:( ) |for x in xyz:
这里 |表示光标,() 表示空格或制表符。
我怎样才能做到这一点?
【问题讨论】:
标签: emacs
元-m
【讨论】:
back-to-indentation。
Meta 而不是M-m
我最喜欢的处理方法是让 C-a 在行首和代码开头之间切换。你可以用这个函数来做到这一点:
(defun beginning-of-line-or-indentation ()
"move to beginning of line, or indentation"
(interactive)
(if (bolp)
(back-to-indentation)
(beginning-of-line)))
并将适当的绑定添加到您最喜欢的模式地图:
(eval-after-load "cc-mode"
'(define-key c-mode-base-map (kbd "C-a") 'beginning-of-line-or-indentation))
【讨论】:
我使用与 Trey 相同的切换技巧,但默认为缩进而不是行首。它需要更多的代码,因为我知道没有“缩进开始”功能。
(defun smart-line-beginning ()
"Move point to the beginning of text on the current line; if that is already
the current position of point, then move it to the beginning of the line."
(interactive)
(let ((pt (point)))
(beginning-of-line-text)
(when (eq pt (point))
(beginning-of-line))))
这可能会让你继续使用 Ctrl-a 并让它做你最想做的事情,同时仍然能够轻松地获得内置行为.
【讨论】:
M-m。
默认情况下,Meta-m 运行back-to-indentation,根据the documentation 将“移动指向这一行的第一个非空白字符。”
【讨论】:
现代 IDE 中的常见习语是在第二次按下时移动到第一个/最后一个非空格:
(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)))
(defun my--smart-end-of-line ()
"Move point to `end-of-line'. If repeat command it cycle
position between last non-whitespace and `end-of-line'."
(interactive "^")
(if (and (eq last-command 'my--smart-end-of-line)
(= (line-end-position) (point)))
(skip-syntax-backward " " (line-beginning-position))
(end-of-line)))
(global-set-key [home] 'my--smart-beginning-of-line)
(global-set-key [end] 'my--smart-end-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 (eq last-command 'my--smart-beginning-of-line) (if (= (line-beginning-position) (point)) (back-to-indentation) (beginning-of-line)) (back-to-indentation)))