【问题标题】:Moving to the start of a code line: Emacs移动到代码行的开头:Emacs
【发布时间】: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


    【解决方案1】:

    -m

    【讨论】:

    • 默认情况下,Meta-m 运行back-to-indentation
    • 我见过的最短(也是最正确)的答案!幸运的是你写的是Meta 而不是M-m
    【解决方案2】:

    我最喜欢的处理方法是让 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))
    

    【讨论】:

      【解决方案3】:

      我使用与 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 并让它做你最想做的事情,同时仍然能够轻松地获得内置行为.

      【讨论】:

      • 在您的解决方案中,我在注释掉该行时遇到问题,它会在注释字符内跳转 2 个字符。请看:emacs.stackexchange.com/q/66590/18414
      • @alper 我喜欢这种行为。从注释行的开头我可能会做的两件事是:用 C-k 删除整行;编辑评论的文本。我想不出任何理由我想在评论字符之前但在缩进之后定位点。另外,如果你想回到那个地方,已经有M-m
      【解决方案4】:

      默认情况下,Meta-m 运行back-to-indentation,根据the documentation 将“移动指向这一行的第一个非空白字符。”

      【讨论】:

        【解决方案5】:

        现代 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)))
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 2013-10-11
        • 1970-01-01
        • 2021-11-27
        相关资源
        最近更新 更多