移动到代码行的开头:Emacs

发布于 2024-11-08 05:08:02 字数 274 浏览 0 评论 0原文

我使用 emacs 进行开发,经常需要移动到行首 (​​Ca)。但是,如果该行缩进,我想移动到代码开始的位置。

因此,在浏览代码时:( ) for x in xy|z:。输入 Ca 后,我们得到:|( ) for x in xyz:。但相反,我想要这样:( ) |for x in xyz:

Here |表示光标,() 表示空格或制表符。

我怎样才能做到这一点?

I use emacs for development and very often need to move to the start of a line (C-a). However if the line is indented, I'd like to move to the point at which code starts.

So while browsing code: ( ) for x in xy|z:. On typing C-a we get this: |( ) for x in xyz:. But instead, I would like this:( ) |for x in xyz:

Here | indicates cursor and () indicate spaces or tabs.

How can I make make this happen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再见回来 2024-11-15 05:08:03

-

Meta-m

悸初 2024-11-15 05:08:03

我最喜欢的处理这个问题的方法是让 Ca 在行的开头和代码的开头之间切换。您可以使用此函数来执行此操作:

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

A favorite way for me to handle this is to have C-a toggle between the beginning of the line and the beginning of the code. You can do so with this function:

(defun beginning-of-line-or-indentation ()
  "move to beginning of line, or indentation"
  (interactive)
  (if (bolp)
      (back-to-indentation)
    (beginning-of-line)))

And add the appropriate binding to your favorite mode map:

(eval-after-load "cc-mode" 
     '(define-key c-mode-base-map (kbd "C-a") 'beginning-of-line-or-indentation))
睡美人的小仙女 2024-11-15 05:08:03

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

I do the same toggling trick as Trey, but defaulting to indentation instead of to beginning of line. It takes slightly more code because there's no "at-beginning-of-indentation" function that I know of.

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

This will probably let you continue to use Ctrl-a and have it do what you want most often, while still being able to get the built-in behavior easily.

我的奇迹 2024-11-15 05:08:03

默认情况下,Meta-m 运行 < code>back-to-indentation 根据 文档将“将指向移动到该行的第一个非空白字符。”

By default, Meta-m runs back-to-indentation which according to the documentation will "Move point to the first non-whitespace character on this line."

只为守护你 2024-11-15 05:08:03

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

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

Common idiom among modern IDE is to move at the first/last non-whitespace on second press:

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

This code firstly move to actual begin/end, new behavior show up on subsequent presses. So any old keyboard macros will work as expected!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文