如何跳转到行、列指定的位置?

如何跳转到行、列指定的位置?

打开文件时可以使用

$ emacs +2:9 practice.b

这将打开文件“practice.b”的第 2 行和该行的第 9 个字符。如何在已经运行的 Emacs 中像这样跳转?我知道,M-x goto-line但它不识别分号。

答案1

M-x goto-line(M-g gM-g M-g) 让您到达目标行的开头。然后您可以使用M-g | 8 RET( M-x move-to-column 8 RET) 移动到目标列(C-u 8 right如果没有宽字符也可以)。请注意,这会将您置于第 8 列,因为 Emacs 从 0 开始对列进行编号,但命令行选项+LINE:COLUMN从 1 开始对列进行编号。

如果您想要一个可以键入 的 Emacs 命令2:9,您可以将以下一些代码粘贴到您的文件中,.emacs以便将其作为参数写入goto-line。请注意,该代码仅在 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))
                      (prog1
                        (match-string 2 line)
                        (setq line (match-string 1 line)))
                    nil))))
    (if (stringp column)
        (setq column (if (= (length column) 0)
                         (current-column)
                       (string-to-int column))))
    (if (stringp line)
        (setq line (if (= (length line) 0)
                       (if buffer
                         (save-excursion
                           (set-buffer buffer)
                           (line-number-at-pos))
                         nil)
                     (string-to-int line))))
    (if line
        ad-do-it)
    (if column
        (let ((limit (- (save-excursion (forward-line 1) (point))
                        (point))))
          (when (< column limit)
            (move-to-column column))))))

答案2

对于 Emacs 24+,我编写了另一个版本函数,因为 Gilles 函数不适合我(不知道为什么)。

(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)))
    (save-match-data
      (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)

答案3

尝试set-goal-columnC-x C-n

相关内容