6

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

3
  • 1
    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
    Commented Feb 6, 2018 at 19:02
  • 2
    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
    Commented Feb 7, 2018 at 0:37
  • Many of the answers to stackoverflow.com/questions/3139970/… take a stab at doing something along these lines, too.
    – SamB
    Commented Feb 4, 2022 at 20:22

6 Answers 6

4

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

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.

1
  • 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
    Commented Feb 7, 2018 at 6:20
2

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)

1
  • THANK YOU, it is simple as that I don't have any idea why the emacs help forum makes things this much complicated when the question and expected answer are really simple, Question: how to jump to a line, Answer: use M-g g or M-g M-g Commented Jul 21, 2023 at 16:02
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.

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.

0

In my init files, I create a keymap I call personal-map and I bind it to C-\ (I did this before C-\ was used to select the input mode). I then bind goto-line to C-g in the personal-map (along with other bindings) so I can do C-\ C-g and it prompts me for the line to go to.

If I was to do it now, I would use the other chord keys that are now rather standard on keyboards today such as the right option modifier can be set to alt or the right command key (Mac) can be set to hyper. Then (for example) (global-set-key (kbd "H-g") #'goto-line) to map goto-line to hyper lowercase g.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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