6

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?

1
  • 5
    Use a prefix argument? C-u C-x TAB – rekado Nov 6 '14 at 8:48
9

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
5

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.

1
  • This works really well. C-u does not work in evil mode. C-5 will automatically expand to C-u 5 and even does work with multiple digits when keepin C presses. So I can do C-15 SPC w - to decrease a window size. – sbstnzmr Oct 25 '20 at 7:52
0

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"))))
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.