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
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)
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.
compilation-error-regexp-alist
which would process the existing syntax, unmodified.
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)
M-g g
or M-g M-g
Commented
Jul 21, 2023 at 16:02
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.
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.
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
.
M-g M-g
orM-x goto-line
to jump to a line once the file is open.compilation-mode
and related commands likeprevious-error
andnext-error
. Emacs has built-in support for handling various kinds of output that includes file names and line numbers, for compilation, grep, etc.