Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs: Change case of a rectangle

Tags:

emacs

What would be simplest way to change the case of a rectangle?

None of the short-cuts mentioned in the manual talk about this. Do I have to add a custom binding to do that? And while we are at it, how do I search only within a rectangle?

like image 428
calvinkrishy Avatar asked May 27 '11 15:05

calvinkrishy


2 Answers

It's easy with using cua-mode's rectangle selection support:

(setq cua-enable-cua-keys nil)  ; enable only CUA's rectangle selections
(cua-mode t)

You can then select rectangles by pressing C-RET and moving the cursor. To upcase that region, just use the usual upcase-region command, bound to M-U by default.

like image 72
sanityinc Avatar answered Oct 03 '22 21:10

sanityinc


Here's an implementation of upcase-rectangle, which changes the case to all uppercase. Just replace the upcase with downcase or capitalize or whatever custom case transformation you want:

(defun upcase-rectangle (b e)
  "change chars in rectangle to uppercase"
  (interactive "r")
  (apply-on-rectangle 'upcase-rectangle-line b e))

(defun upcase-rectangle-line (startcol endcol)
  (when (= (move-to-column startcol) startcol)
    (upcase-region (point)
                   (progn (move-to-column endcol 'coerce)
                          (point)))))
like image 43
Trey Jackson Avatar answered Oct 03 '22 20:10

Trey Jackson