Does anyone have an Emacs macro for indenting (and unindenting) blocks of text?

Does anyone have an Emacs macro to indent (and unindenting) blocks of text?

And I mean "indentation" in the common sense, not in Emacspeak. In other words, I want to mark the area, press Cu 2, run this macro and add two spaces before each line in the region.

Or press Cu -2 before running the macro and remove two spaces from the beginning of each line in the region. Bonus points if he complains if the lines do not have enough spaces.

+4
source share
3 answers

indent-rigidly (associated with Cx TAB) does what you want. This is in indent.el, which should be part of the standard emacs distribution.

Also, so that it complains / interrupts when there is not enough space somewhere, you can do something like this: (quick ugly cracking of source code with indentation)

(defun enough-whitespace-to-indent-p (start end arg) (save-excursion (goto-char end) (setq end (point-marker)) (goto-char start) (or (bolp) (forward-line 1)) (while (and (< (point) end) (>= (+ (current-indentation) arg) 0)) (forward-line 1)) (>= (point) end))) (defun indent-rigidly-and-be-picky (start end arg) (interactive "r\np") (if (or (plusp arg) (enough-whitespace-to-indent-p start end arg)) (indent-rigidly start end arg) (message "Not enough whitespace to unindent!"))) 
+12
source

You can also use the world of rectangles . To insert two spaces:

 Cx rt SPC SPC RET 

Delete two spaces

 Cx rd 

provided that you define a rectangle to cover two spaces. Also, in addition to editing rectangles, a nice addition has also been added to the CUA package. The CUA package does not only cover rectangles, so if you just want a part of the rectangle, look at this description (full disclosure, link to my blog).

+3
source

Use indent-rigidly default Cx TAB by default

+2
source

All Articles