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

Solution 1:

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)

Solution 2:

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).

Solution 3:

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.