M-x all-things-emacs

Quick Tip: set-goal-column

March 17th, 2007 by Ryan McGeary · 50 Comments

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 C-x C-n).

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

Step 1: set-goal-column

Now let’s input our first text field and hit C-n to go to the next line.

Step 2: set-goal-column

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 set-goal-column instead.

Step 1: set-goal-column

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-n and C-p. Let’s see what happens when we re-input our first field and hit C-n.

Step 3: set-goal-column

Perfect. We’re right were we need to be. Quickly, we finish our text entry down the column:

Step 4: set-goal-column

To turn off the goal column, pass a non-nil argument to set-goal-column (i.e. 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.

Tags: quick · tips

50 responses so far ↓

  • 1 Jeff Squires // Mar 22, 2007 at 5:46 pm

    Thanks Ryan.

    Like so many aspects of emacs, I never knew this existed until I read your post.

    I usually just compose the text I need to insert, then copy it to a rectangle.

    But, I’m going to start using this now. I wonder why it is disabled by default.

    BTW, slightly on-topic: Does anything know why there isn’t a way (or maybe there is) to copy a rectangle instead of killing it as well. I am aware of the function: copy-rectangle-to-register, but I must admit I can’t figure out how to paste it from the register (and it seems a bit clunky that I have to use a register).

  • 2 Ryan McGeary // Mar 22, 2007 at 7:27 pm

    Jeff, Good question. I think I’ve actually ingrained myself to kill a rectangle and then immediately undo it back to simulate a copy (the rectangle stays as the “last killed” even after the undo).

    It sounds like what you’re asking for is a kill-save-rectangle function to compliment kill-rectangle (just like kill-ring-save compliments kill-region). Since your gripe is my gripe, I took a quick stab at writing such a function:

    1
    2
    3
    4
    5
    6
    7
    
    (defun kill-save-rectangle (start end &optional fill)
      "Save the rectangle as if killed, but don't kill it.  See
    `kill-rectangle' for more information."
      (interactive "r\nP")
      (kill-rectangle start end fill)
      (goto-char start)
      (yank-rectangle))

    This might benefit from some save-excursion action, but it seems to work better for me without. I bound it to C-x r M-k to compliment C-x r k (just like M-w compliments C-w):

    (global-set-key (kbd "C-x r M-k") 'kill-save-rectangle)
  • 3 blalor // Mar 23, 2007 at 5:28 pm

    TextMate has a nifty feature that allows you to type in a column, but with the start of the region relative to the end of the line. For example, consider the following text:

    asdf
    asdffdsa
    asdfqwerty

    You’d select all the lines above, hit a key sequence (which escapes me at the moment) and when you type, the characters all appear at the end of the line. If you moved the cursor left two characters before typing, the text would appear two characters in from the end of the line.

    Is this possible with Emacs? Scratch that, I know it’s possible. How do I do it? :-)

  • 4 Ryan McGeary // Mar 23, 2007 at 6:36 pm

    blalor, Good question. I’m familiar with that TextMate feature:

    ⌥⌘A (Text -> Edit Each Line in Selection) lets you insert text at the end of each line in the current selection.

    I normally perform a quick keyboard macro to accomplish the same task in emacs. The macro typically starts with C-e and ends with C-n, but I must admit, the TextMate feature is more elegant than that for the simpler cases. For the more advanced cases where you might need to insert text before the last word on each line, a keyboard macro (in either editor) probably works best.

    Another alternative is to use query-replace-regexp with transient-mark-mode on. Highlight the region, run query-replace-regexp (C-M-%), and substitute $ (end of line) with the text that you want to append. Hit ! to replace all remaining matches within the region.

    With a bit of time, I’m sure someone could write a more elegant function to simulate the TextMate behavior, but I’m not aware if something is already available.

  • 5 blalor // Mar 24, 2007 at 11:44 am

    Thanks for that, Ryan. I’d forgotten about the regexp way. I figured a macro would be the way to go. Have to get cracking on that.

  • 6 Keyboard Macros in the Wild: The Mundane SQL Fix | M-x all-things-emacs // Apr 5, 2007 at 8:32 pm

    [...] solution was to move point to the start of the unique id 248 in the first row and to set a goal column (C-x C-n) at column 65 (this location). I then created the following macro by calling M-5 M-0 M-0 [...]

  • 7 hrbg // Dec 5, 2010 at 1:27 am

    great post, so that’s C-x C-n was for!!. Many times I typed this by mistake now I see how useful this is

    @Jeff Squires

    you can copy a rectangle of text using cua-mode selecting the text and then use M-w to copy

  • 8 Personal Trainer Seattle // Jul 22, 2015 at 12:45 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 9 Engagement rings // Jul 22, 2015 at 3:23 am

    ... [Trackback]

    [...] There you will find 67594 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 10 Con-Cret Amino Tren // Jul 23, 2015 at 4:18 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 11 amazin gas station video // Jul 24, 2015 at 8:56 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 12 amazin gas station video // Jul 24, 2015 at 10:32 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 13 attorney // Jul 24, 2015 at 9:26 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 14 Limo service atlanta // Jul 25, 2015 at 1:49 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 15 Locksmith Rancho Cordova // Jul 26, 2015 at 6:31 am

    ... [Trackback]

    [...] There you will find 82841 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 16 can you buy propecia over the counter in canada // Jul 27, 2015 at 9:08 am

    ... [Trackback]

    [...] There you will find 41054 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 17 Cartersville Website Design // Jul 27, 2015 at 3:05 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 18 domestic cleaning // Jul 27, 2015 at 6:25 pm

    ... [Trackback]

    [...] There you will find 80131 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 19 surf shop online // Jul 28, 2015 at 6:41 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 20 Insider Binary Bot Review // Jul 31, 2015 at 3:52 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 21 Prodaja staklene ambalaze // Aug 1, 2015 at 4:27 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 22 private security // Aug 1, 2015 at 2:27 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 23 recycletotes.com // Aug 1, 2015 at 7:33 pm

    ... [Trackback]

    [...] There you will find 39409 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 24 http://optimumcontractors.com // Aug 2, 2015 at 8:21 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 25 www.tiptoproofers.net // Aug 2, 2015 at 10:24 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 26 www.tiptopplumbers.com // Aug 3, 2015 at 12:04 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 27 read the full info here // Aug 3, 2015 at 4:18 am

    ... [Trackback]

    [...] There you will find 88914 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 28 more // Aug 3, 2015 at 1:41 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 29 http://tiptophvac.com // Aug 4, 2015 at 1:05 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 30 recommended you read // Aug 4, 2015 at 1:22 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 31 What is home automation and how does it work? // Aug 4, 2015 at 2:59 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 32 http://tiptopelectric.org // Aug 4, 2015 at 12:20 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 33 social signal checker // Aug 4, 2015 at 9:04 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 34 http://www.skillfullcontracting.com // Aug 5, 2015 at 7:13 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 35 http://selectcontractors.net // Aug 5, 2015 at 7:24 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 36 wealthy single mom // Aug 7, 2015 at 3:44 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 37 Your Domain Name // Aug 9, 2015 at 6:30 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 38 imp source // Aug 9, 2015 at 6:51 pm

    ... [Trackback]

    [...] There you will find 92116 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 39 check these guys out // Aug 10, 2015 at 10:13 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 40 visit // Aug 11, 2015 at 12:01 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 41 small business SEO consultant // Aug 12, 2015 at 6:06 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 42 view // Aug 12, 2015 at 10:12 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 43 trending topics // Aug 13, 2015 at 12:19 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 44 outdoor lighting // Aug 13, 2015 at 11:42 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 45 What could be causing the loud, knocking noise in my pipes? // Aug 13, 2015 at 11:57 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 46 http://optimumdentist.com // Aug 14, 2015 at 12:06 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 47 What types of services do you offer? // Aug 14, 2015 at 4:41 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 48 http://primehandymen.com // Aug 14, 2015 at 4:53 am

    ... [Trackback]

    [...] There you will find 68342 more Infos: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 49 patio glass doors // Aug 14, 2015 at 5:12 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]

  • 50 this content // Aug 14, 2015 at 7:36 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/03/17/quick-tip-set-goal-column/ [...]