移动到代码行的开头:Emacs

移动到代码行的开头:Emacs,emacs,Emacs,我使用emacs进行开发,并且经常需要移动到行的开头(C-a)。但是,如果行缩进,我想移到代码开始的点 因此,当浏览xy中x的代码:()时,z:。在键入C-a时,我们得到以下结果:|(),对于xyz中的x:。但是,我希望这样:()|对于xyz中的x: 此处|表示光标,()表示空格或制表符 我怎样才能做到这一点呢?Meta-m 我最喜欢的处理方法是在行首和代码开头之间进行C-A切换。您可以使用此功能执行此操作: (defun beginning-of-line-or-indentation ()

我使用emacs进行开发,并且经常需要移动到行的开头(C-a)。但是,如果行缩进,我想移到代码开始的点

因此,当浏览xy中x的代码:
()时,z:
。在键入C-a时,我们得到以下结果:
|(),对于xyz中的x:
。但是,我希望这样:
()|对于xyz中的x:

此处|表示光标,()表示空格或制表符

我怎样才能做到这一点呢?

Meta-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,并让它经常执行您最想执行的操作,同时仍然能够轻松地获得内置行为。

默认情况下,Meta-m将根据“移动指向此行的第一个非空白字符”运行

现代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)

这段代码首先移动到实际的开始/结束,新的行为在随后的按键上显示。因此,任何旧键盘宏都将按预期工作

默认情况下,Meta-m运行
返回缩进
。这是我见过的最短(也是最正确)的答案!幸运的是你写了
Meta
,而不是
M-M
太棒了。此代码将颠倒顺序:先到代码的开头,然后到行的开头<代码>(defun my--smart start of line()“将点移动到`行的开头'。如果重复命令,则在`返回缩进'和`行的开头'之间循环位置。”(交互式“^”)(如果(eq last command'my--smart start of line)(如果(=(行的开头位置)(点))(返回缩进)(行首))(返回缩进))