2

After opening a .txt file with Emacs, when the point is at the line

/folder/subfolder/file.xml:435

How can I fast jump to line 435 of the above mentioned file?

What I tried:

dired-at-point and find-file-at-point

  • I'm not sure how to automatically jump there (it shouldn't be super hard to make a command to parse the file:line and automatically jump). Use M-g M-g or M-x goto-line to jump to a line once the file is open. – jpkotta Feb 6 '18 at 19:02
  • 1
    I like @Tobias's answer, but depending on your use case you might want to look at compilation-mode and related commands like previous-error and next-error. Emacs has built-in support for handling various kinds of output that includes file names and line numbers, for compilation, grep, etc. – glucas Feb 7 '18 at 0:37
3

If you added a trailing colon to each line like so:

/folder/subfolder/file.xml:435:

Then enabling M-x compilation-minor-mode will have the desired outcome, such that you can type RET or use mouse-1 to visit the specified file at the specified line.

The trailing colon is needed because an error message is expected. In this case the error message (following the new colon) is an empty string; but the separator is still required.

And as commented by @glucas, you can then also use previous-error and next-error, etc.

  • n.b. If you're comfortable with elisp you could likely write a custom entry for compilation-error-regexp-alist which would process the existing syntax, unmodified. – phils Feb 7 '18 at 6:20
2

You can add this functionality to find-file-at-point with the following elisp snippet in your init file.

Please be aware that this method does not take into account that

/folder/subfolder/file.xml:435

inclusive the sub-string :435 is a valid file name.

(defun find-file-at-point-goto-line (ret)
  "Ignore RET and jump to line number given in `ffap-string-at-point'."
  (when (and
     (stringp ffap-string-at-point)
     (string-match ":\\([0-9]+\\)\\'" ffap-string-at-point))
    (goto-char (point-min))
    (forward-line (string-to-number (match-string 1 ffap-string-at-point))))
  ret)

(advice-add 'find-file-at-point :filter-return #'find-file-at-point-goto-line)
1

If you turn on org-mode, you can open links of the sort

[[file:/folder/subfolder/file.xml::435]] with C-c C-o so with a few changes to the text in your example it's easy to open that link.

Maybe you can't edit the text or don't want to use org-mode so you could implement your own function. I'd still have a look at org mode (specifically org-open-file) to see how line number jumping is implemented.

1

I think you can just use the go-to line function in emacs. below is its brief help doc:

goto-line is an interactive compiled Lisp function in ‘simple.el’.

It is bound to s-l, M-g g, M-g M-g, .

(goto-line LINE &optional BUFFER)

0

Helm's helm-find-files does this out-of-the-box, if you are a helm user, you probably already have bound it to C-x C-f.

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.