For posting code to stackexchange I repeatedly have to do indentation in emacs. I mark a piece of text and then do M-X indent-rigidly four times. Is there an elegant way to do this indentation of text by four blanks in one go?
4 Answers
The documentation for indent-rigidly
says:
If called from a program, or interactively with prefix ARG, indent all lines starting in the region forward by ARG columns. If called from a program, START and END specify the beginning and end of the text to act on, in place of the region.
To indent by four columns, just pass the prefix argument with C-u
:
C-u C-x TAB
Besides the obvious answer of using a prefix arg to specify the number of columns to indent (i.e., C-4 C-x TAB
), if you use Emacs 24.4 or later then you can simply use arrow keys to repeat indenting, i.e., to incrementally indent. E.g.:
C-x TAB <right> <right> <right>...
If you use Shift with <right>
or <left>
then each repetition indents by a tab stop. Without Shift, each repetition indents by one column.
-
This works really well.
C-u
does not work in evil mode.C-5
will automatically expand toC-u 5
and even does work with multiple digits when keepinC
presses. So I can doC-15 SPC w -
to decrease a window size.– sbstnzmrOct 25, 2020 at 7:52
I've written this snippet when Stack Overflow for some reason stopped recognizing lisp syntax in the code blocks. Might be useful:
(defun stackoverflow-block (beg end)
(interactive "r")
(let ((str (buffer-substring-no-properties beg end)))
(delete-region beg end)
(insert
"<!-- language: lang-lisp -->\n\n"
(mapconcat
(lambda (x) (format " %s" x))
(split-string str "\n")
"\n"))))
I've always used M-[number] to do things. This also works if you just want to type out 80/132 characters to measure the width of your screen or something. There doesn't seem to be a limit on the number of numbers you can put in the prefix.
M-80 #
;;Prints out 80 #
M-4 M-x indent-rigidly
;; Indents 4 times
;; Need to separate M-# with C-u if your command is a number
M-80 C-u 0
;; Prints out eighty 0 characters
C-u C-x TAB
C-u <n> <comment>