On the surface, it might not sound like editing columns of text is something that happens a lot, but I find myself doing it fairly often. A quick way to enter different values on each line down a column is to use
set-goal-column (bound to
Let’s say we have a block of text like the following comma-separated list. We’d like to enter a different value in the second field of each line. First we position the point on the first line between the two commas. This is at column position 6 (emacs starts counting columns at zero).
Now let’s input our first text field and hit
C-n to go to the next line.
It does what you might expect. Because our first field pushed us out to column position 23, when we went to the next line our point was placed at the end of the second line. To enter a new field on line 2, we’ll have to first jump back to column 6 manually. That’s no fun. Let’s start over and use
Now, before we enter our first field, we’ll first hit
C-x C-n. Setting a goal column sets the current horizontal position as a goal for
C-p. Let’s see what happens when we re-input our first field and hit
Perfect. We’re right were we need to be. Quickly, we finish our text entry down the column:
To turn off the goal column, pass a non-nil argument to
C-u C-x C-n).
Note: When you run
set-goal-column for the first time, you’ll receive a warning that the function is disabled. You can either follow the built-in instructions and customizations to enable the function, or you can add the following to your emacs initialization.
(put 'set-goal-column 'disabled nil)
This exercise provided a nice way to manage a column of text with different values on each line. When the task is more repetitive, emacs rectangles come in handy. We’ll make rectangles a future topic of discussion.