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
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityYou 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.
– phils
Feb 7 '18 at 6:20
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.
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)
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.
M-g M-g
orM-x goto-line
to jump to a line once the file is open. – jpkotta Feb 6 '18 at 19:02compilation-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. – glucas Feb 7 '18 at 0:37