Emacs bulk indent for Python

前端 未结 8 1318
星月不相逢
星月不相逢 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:15

    If you are programming Python using Emacs, then you should probably be using python-mode. With python-mode, after marking the block of code,

    C-c > or C-c C-l shifts the region 4 spaces to the right

    C-c < or C-c C-r shifts the region 4 spaces to the left

    If you need to shift code by two levels of indention, or some arbitary amount you can prefix the command with an argument:

    C-u 8 C-c > shifts the region 8 spaces to the right

    C-u 8 C-c < shifts the region 8 spaces to the left

    Another alternative is to use M-x indent-rigidly which is bound to C-x TAB:

    C-u 8 C-x TAB shifts the region 8 spaces to the right

    C-u -8 C-x TAB shifts the region 8 spaces to the left

    Also useful are the rectangle commands that operate on rectangles of text instead of lines of text.

    For example, after marking a rectangular region,

    C-x r o inserts blank space to fill the rectangular region (effectively shifting code to the right)

    C-x r k kills the rectangular region (effectively shifting code to the left)

    C-x r t prompts for a string to replace the rectangle with. Entering C-u 8 will then enter 8 spaces.

    PS. With Ubuntu, to make python-mode the default mode for all .py files, simply install the python-mode package.

提交回复
热议问题