When opening a file one can use

$ emacs +2:9 practice.b

Which will open file ‘practice.b’ on line 2 and character 9 on that line. How do I jump like this in an already running Emacs? I know about M-x goto-line but it doesn’t recognize semicolon.


M-x goto-line (M-g g or M-g M-g) gets you to the beginning of the target line. Then you can use C-u 8 right to move to the target column. Note that this puts you in column 8, because Emacs numbers columns from 0, except that the command line option +LINE:COLUMN numbers columns from 1.

If you want an Emacs command where you can type 2:9, here's some code you can paste in your .emacs that allows you to write it as an argument to goto-line. Note that the code is only minimally tested in Emacs 23.

(defadvice goto-line (around goto-column activate)
  "Allow a specification of LINE:COLUMN instead of just COLUMN.
Just :COLUMN moves to the specified column on the current line.
Just LINE: moves to the current column on the specified line.
LINE alone still moves to the beginning of the specified line (like LINE:0)."
  (if (symbolp line) (setq line (symbol-name line)))
  (let ((column (save-match-data
                  (if (and (stringp line)
                           (string-match "\\`\\([0-9]*\\):\\([0-9]*\\)\\'" line))
                        (match-string 2 line)
                        (setq line (match-string 1 line)))
    (if (stringp column)
        (setq column (if (= (length column) 0)
                       (string-to-int column))))
    (if (stringp line)
        (setq line (if (= (length line) 0)
                       (if buffer
                           (set-buffer buffer)
                     (string-to-int line))))
    (if line
    (if column
        (let ((limit (- (save-excursion (forward-line 1) (point))
          (when (< column limit)
            (forward-char column))))))

For Emacs 24+ I wrote another version function, because Gilles function not working for me (don't know why).

(defun go-to-line-and-column-cond (lc-cond)
  "Allow a specification of LINE:COLUMN or LINE,COLUMN instead of just COLUMN.                                                                                                              
Just :COLUMN or ,COLUMN moves to the specified column on the current line.                                                                                                                  
LINE alone still moves to the beginning of the specified line (like LINE:0 or LINE,0).                                                                                                      
By Default I'm bind it to M-g M-l.                                                                                                                                                          
The default value of the COLUMN is decrement by -1                                                                                                                                          
because all compilers consider the number of COLUMN from 1 (just for copy-past)"
  (interactive "sLine:Column:: ")
  (let (line delim column max-lines)
    (setq max-lines (count-lines (point-min) (point-max)))
      (string-match "^\\([0-9]*\\)\\([,:]?\\)\\([0-9]*\\)$" lc-cond)
      (setq line (string-to-number (match-string 1 lc-cond)))
      (setq delim (match-string 2 lc-cond))
      (setq column (string-to-number (match-string 3 lc-cond)))
      (if (not (equal delim "")) (if (> column 0) (setq column (1- column))))
      (if (= 0 line) (setq line (line-number-at-pos)))
      (if (> line max-lines) (setq line max-lines))
      (goto-line line)
      (move-to-column column)
      (message "Marker set to line %d column %s" (line-number-at-pos) (current-column))

(global-set-key (kbd "M-g M-l") 'go-to-line-and-column-cond)

Try set-goal-column (C-x C-n)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.