How to make a link to external file line in Emacs org-mode
Asked Answered
B

3

24

I have 2 files:

  1. php file with code
  2. org file with docs

How to create a link to a line in php file from org file? So clicking it will move the cursor to the appropriate line in php file.

Biennial answered 22/8, 2012 at 20:18 Comment(0)
L
30

Use this notation in the org mode to create a link:

[[/path/to/file.php::line][string-to-display]]

If you want the file name to be displayed, use just

[[/path/to/file.php::line]]

See Hyperlinks in the Org mode Info page for details and other ways to specify links.

Lemmons answered 22/8, 2012 at 20:26 Comment(5)
This works, but some files (that don't have .org or .txt extensions) open in TextEdit instead of Emacs. Assuming I don't want to go and setq auto-mode-alist for every dot file in my home directory, how can I force an org-mode file link to open the file in Emacs?Friede
@Friede I've never seen that. What files? What's TextEdit?Lemmons
TextEdit is an external text editor (it's OSX's default system text editor). Example: in org-mode, I have [[~/.hunspell_en_US]] which is my user local dictionary. But when I place point on that file link and hit enter, it opens in TextEdit.Friede
I do use a package called open-with that for e.g. tells .pdf files to open in an external application, but I don't see anything in my config that should affect text files. Guess I'll ask it as a new question. Thanks though!Friede
I need to prefix it with file: for this to work properly. Otherwise it errors with "no match for fuzzy expression: <filename>".Pentstemon
G
21

If I understand the question right you can do it automatically with two org-mode commands:

Move the point to a line in a php file and M-x org-store-link.

Go to an org file and C-c C-l or M-x org-insert-link and follow the instructions in the minibuffer.

You are done, click the link or C-c C-o when the point is on it to open the link.

I have org-store-link on C-c l and org-insert-link on the default C-c C-l. This makes the process very convenient and fast to execute.

Have fun with org-mode.

Godrich answered 30/5, 2018 at 12:31 Comment(3)
Just a comment here in case someone encounters an issue similar to mine: storing and inserting links to headlines in org files won't work correctly if there is a timestamp in the headline.Overwork
The point is to tell emacs with which software to open it. Otherwise it will open keynote files on emacs.Blameful
Years later, I noticed my answer was wrong. Still, it seems helpful to some people, so I'm leaving it as it is.Godrich
V
0

For posterity and some future seeker of this information, which I've been using for a few years in my docs.

Use: [[file+emacs:/path/to/file.php::line]]

The '+emacs' forces Emacs to open the link inside Emacs instead of some other app that handles that file extension.

Vacillatory answered 8/8 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.