Moving to the start of a code line: Emacs

后端 未结 5 1909
自闭症患者
自闭症患者 2021-01-31 15:18

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 st

5条回答
  •  Happy的楠姐
    2021-01-31 15:33

    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!

提交回复
热议问题