Emacs bulk indent for Python

前端 未结 8 1367
星月不相逢
星月不相逢 2021-01-29 17:37

Working with Python in Emacs if I want to add a try/except to a block of code, I often find that I am having to indent the whole block, line by line. In Emacs, how do you inden

8条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-29 18:23

    I use the following snippet. On tab when the selection is inactive, it indents the current line (as it normally does); when the selection is inactive, it indents the whole region to the right.

    (defun my-python-tab-command (&optional _)
      "If the region is active, shift to the right; otherwise, indent current line."
      (interactive)
      (if (not (region-active-p))
          (indent-for-tab-command)
        (let ((lo (min (region-beginning) (region-end)))
              (hi (max (region-beginning) (region-end))))
          (goto-char lo)
          (beginning-of-line)
          (set-mark (point))
          (goto-char hi)
          (end-of-line)
          (python-indent-shift-right (mark) (point)))))
    (define-key python-mode-map [remap indent-for-tab-command] 'my-python-tab-command)
    

提交回复
热议问题