2

I'm using Firefox extention It's all text, with it I can edit stackoverflow form text in Emacs.

Therefore I often want to add four spaces before selected text to markdown codes. indent-region only "indent" the region. Is there already function like this or should I write it myself?

I already installed markdown-mode.

4

I think indent-rigidly (C-x TAB or C-x C-i, see footnote) can help you.

indent-rigidly is an interactive compiled Lisp function in `indent.el'.

It is bound to C-x TAB.

(indent-rigidly START END ARG &optional INTERACTIVE)

Indent all lines starting in the region. If called interactively with no prefix argument, activate a transient mode in which the indentation can be adjusted interactively by typing left, right, S-left, or S-right. Typing any other key deactivates the transient mode.

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.

Negative values of ARG indent backward, so you can remove all indentation by specifying a large negative ARG.

Footnote: C-x TAB is equivalent to C-x C-i. This was noted by nanny in the comment, and I think it's worth mentioning because C-x C-i is so much easier to hit.

  • Thanks! But my code (define-key markdown-mode-map (kbd "TAB") '(lambda () (interactive) (indent-rigidly 4))) fails with error Wrong number of arguments: (3 . 4), 1 Could you tell me what am I doing wrong? – ironsand Dec 9 '15 at 13:25
  • the function takes 3 mandatory arguments, you gave it one. START and END would be the beginning and end of the region. – Ehvince Dec 9 '15 at 15:14
  • 1
    I use this function all the time to do exactly what OP is asking. I use the keystrokes: C-u C-x C-i to call it after marking a region (since C-i is the same as TAB and C-u gives it a numerical argument of 4). – nanny Dec 9 '15 at 15:21
  • (sorry, duplicate) As for the doc, this function requires 3 mandatory arguments. You gave it one. START and END would be the beginning and end of the region. See my answer for an example. – Ehvince Dec 9 '15 at 15:27
  • Thanks! I could do with (define-key markdown-mode-map (kbd "TAB") '(lambda () (interactive) (indent-rigidly (region-beginning) (region-end) 4))). – ironsand Dec 11 '15 at 9:14
2

Try rectangle-mark-mode, down N lines, forward 4 spaces, then open-rectangle.

  • When I type spaces 4 times, the last line is already with spaces, and then open-rectangle make it 8 times spaces. Other lines works fine, though. – ironsand Dec 9 '15 at 7:21
  • 1
    I don't think he meant to insert 4 space but just to move forward by 4 steps. – Stefan Dec 9 '15 at 13:09
0

This function inserts 4 spaces at the beginning of each line of the region:

(defun my-indent (reg-beg reg-end)
     (interactive "r") ;; r: take the active region and give its boundaries as arguments
     (save-excursion
       (replace-regexp "^" "    " nil reg-beg reg-end)))

evil mode's > does it too.

  • As mentioned in its docstring, replace-regexp is for interactive use only. In particular, it sets the mark, which can be annoying. – YoungFrog Dec 10 '15 at 10:42
  • thanks. Quoting the doc: This function is usually the wrong thing to use in a Lisp program. What you probably want is a loop like this: (while (re-search-forward REGEXP nil t) (replace-match TO-STRING nil nil)) which will run faster and will not set the mark or print anything. – Ehvince Dec 10 '15 at 18:16

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