7

With this text:

Play with it a few times you will see my point. The 
command you need is:

$ echo hello

How do I make it into:

> Play with it a few times you will see my point. The
> command you need is:
>
> $ echo hello

Without using the complicated regular expression: M-x replace-regexp RET ^ RET > RET

Note that I couldn't use C-M-% shortcut key to conjure query-replace-regexp since it's not possible to produce that key combination in a terminal.

3 Answers 3

6

You can use string-rectangle (C-x r t).

Set a mark (C-SPC) before the first character of the first line, move the cursor until the first character of the last line, use C-x r t, then type > and ENTER key.

3

If you do this a lot, you can utilize comment-region. Essentially, add a hook to your mode that sets comment-start to >, then add a key binding for comment-region (in c-mode, for example, it's C-c C-c). This has the benefit of being reversible with uncomment-region as well:

(add-hook 'text-mode-hook
          #'(lambda ()
              (require 'newcomment)
              (make-local-variable 'comment-start) ;; Always make buffer-local
              (setq comment-start "> ")
              (local-set-key "\C-c\C-c" #'comment-region)))
1
  • BTW, @peer-gynt thanks for the idea, I just put this in my own .emacs! :) Commented Apr 15, 2019 at 15:28
1

You can mark the region to be quoted using C-SPC if in Text mode and use M-; which is bound to comment-dwim and that will prompt you for a "Comment syntax" which you can then enter > in the prompt.

After using comment-dwim to prefix the lines, you may also find M-q which is bound to fill-paragraph useful for wrapping the lines of the block quote if you're not interested in preserving the original line length.

Alternatively, if you have markdown-mode installed, you can use M-x markdown-mode to switch to it, and then mark the region you care to quote and use C-c C-s q which is bound to markdown-insert-blockquote.

Finally, if you really want to use query-replace-regexp from a terminal which I've found sometimes fails to work due to the combination of Ctrl, Meta, and Shift keys all requiring to be pushed at the same time, you may find it works using the following sequence Esc then C-%. The Esc key is essentially a "meta lock" key which will hold down meta for the duration of the next key sequence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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