Using Emacs to indent (shift 4) code

微笑、不失礼 提交于 2019-11-29 01:36:28
kmkaplan

C-x TAB runs indent-rigidly. Given a numerical argument of four it will do what you want. Alternatively use <pre><code> to introduce your code (see the first paragraph of Markdown Editing Help).

Edit: your interactive declaration would better be written:

(interactive "r
p")

Another easy way to do is to use emacs's powerful rectangular editing ability: set your region starting from the beginning of the first line and ending at the beginning of the last line you want to indent (note: the it has to be in the beginning of the line since you don't want to replace your existent text!), then do

C-x r t (string-rectangle)

Then just enter 4 spaces as prompted. Voila! No extra lisp hacking needed. This additionally gives you flexibility to insert other stuff beside spaces to the beginning or anywhere middle of a bunch of lines.

Using C-x TAB to indent-rigidly (as mentioned in another answer) is the easiest way. Simply mark the region you want to indent and press C-u C-x TAB. As the default prefix for C-u is 4, this should do exactly what you want.

Your code looks fine to me. I think the re-setting of end in text-shift-region is not necessary, but other than that, it looks fine.

In python-mode, you can mark a region (C-space, move cursor) and hit C-c > to indent 4 spaces.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!