How to do select column then do editing in GNU Emacs?

15

8

I've been using ViM, TextMate, and GNU Emacs for years.

For example here is the text I want to edit

foo
foo
foo

And here is the text result I want to have

bar foo
bar foo
bar foo

When I'm on Vim, I can do "Ctrl v" on the very first line and first column, then press "2 j", then press "i", then type "bar", done.

When I'm on Textmate, I can press "Apple Command Option" both while selecting down (by my mouse), then type "bar", done.

But when I'm on GNU Emacs 23.1, I don't know how to do it. :((

I searched EmacsWiki and googling around but didn't get the solution. Please guide me if you know my solution. Would be grateful for that.

Arie

Posted 2009-12-01T11:16:02.000

Reputation: 333

@Trey Jackson and @humble coffee, solved, thanks a lot!! – Arie – 2009-12-02T01:13:02.093

Answers

16

Two options come to mind. The first is rectangles (as mentioned in another answer). The explicit directions for that are:

  1. goto first line, first column
  2. C-SPC
  3. goto last line (first column)
  4. C-x r t bar SPC RET

Another option, which provides very nice rectangle/column editing commands is CUA mode. Here's a blog post (disclosure: my blog) that describes how to use it. To see the power of CUA mode it's totally worth watching this three minute video.

I integrate CUA mode with the following (because I prefer not to have transient mark mode):

(setq cua-enable-cua-keys nil)
(setq cua-highlight-region-shift-only t) ;; no transient mark mode
(setq cua-toggle-set-mark nil) ;; original set-mark behavior, i.e. no transient-mark-mode
(cua-mode)

Trey Jackson

Posted 2009-12-01T11:16:02.000

Reputation: 3 731

11

In Emacs-24.4, the base support for rectangles has been improved a bit, so instead of using C-SPC followed by C-x r t, you can do:

C-x SPC
down down
C-t bar RET

One of the nice thing about it compared to the the C-SPC method is that you'll get visual feedback about the rectangle you're selecting. Of course the cua-mode method works as well (and works similarly).

Stefan

Posted 2009-12-01T11:16:02.000

Reputation: 1 052

sadly, it seems to me the improvements are not enough at all: cua-mode had it more right... Just to mention a thing: in cua-mode, while a rectangle is highlighted, I can type and add text. In current built-in emacs "base support", highlight disappears (the selection is "cancelled") and the input is inserted at the cursor point. This is counter-intuitive and not what I usually want (and get used to how cua-mode does it) – ShinTakezou – 2015-04-16T13:20:27.563

@ShinTakezou: while you find rectangle-mark-mode counter intuitive, I find cua's behavior counter intuitive. Luckily, you get to choose. If you prefer cua's support, you can either enable cua-mode or (if you only want its rectangle support) (global-set-key [?\C-x ?\s] 'cua-rectangle-mark-mode). – Stefan – 2015-04-16T13:48:22.573

in fact is what I do: sticking to cua-mode. It works like: highlight a column, type text, done. Otherwise: hightlight a column, say you want add text, type text in minibuffer (you won't see it appearing in the buffer as you type...), ... done. You'll see later a typo in the last char... in cua-mode, I can see it as I type, delete it (no undo the whole insert), rethink, insert another char... and the rectangle keeps highlighted as I need, btw, and I can shrink it, enlarge it, type some more text, or "cut" that new rectangle... so, to me it's more intuitive, and comfortable. – ShinTakezou – 2015-04-16T14:01:17.463

I'm glad you like it ;-) – Stefan – 2015-04-16T14:04:37.203

BTW, in Emacs-25, the non-CUA rectange C-t operation (aka C-x r t) also shows you a preview of the newly inserted text as you type it. – Stefan – 2016-05-16T00:18:20.297

2

In emacs these kind of columns are referred to as 'rectangles'. So this is the relevant documentation page.

All these commands require that the region contains the the rectangle you are operating on. So you need to set the mark on the top left character in the rectangle and extend the region to the bottom right character in the rectangle. The command you're after is M-x string-insert-rectangle which then prompts you for the string to insert.

nedned

Posted 2009-12-01T11:16:02.000

Reputation: 2 502

1

Shortest answer: Enable CUA rectangle mode via C-RET.

So, for your case, it would be

  1. Go to first line, first column and hit C-RET to enter CUA mode
  2. Go to last line, first column and simply type barSPC
  3. Exit CUA mode via ESC or C-G

See @Trey 's answer for a more elaborate solution and links to CUA.

Martin

Posted 2009-12-01T11:16:02.000

Reputation: 11

2Yes, I have too little reputation to comment. I don't want to repeat his answer, but I will add some more info from the CUA doc. – Martin – 2015-01-21T22:02:12.973

0

You can use replace-regexp for this:

  1. set mark to the last line --> C SPC and go to the last line
  2. type M-x replace-regexp RET in minibuffer (i have a key binding for this M-r)
  3. Give ^ RET
  4. type bar and hit RET

This will add bar to the beginning of the line.

Neelesh

Posted 2009-12-01T11:16:02.000

Reputation: 1