Emacs: Open File Path Under Cursor

By Xah Lee. Date: . Last updated: .

Alt+x find-file-at-point. It'll open the file path under cursor. (also works for URL).

This is very useful in shell output or in HTML file.

If the path under cursor is URL, it'll launch browser. You can tell emacs which browser to use. 〔►see Emacs: Set Default Browser

One thing annoying is that it'll prompt to confirm.

The following command will open the file path under cursor without confirmation. It'll jump to line number if the path ends with :number.

(defun xah-open-file-at-cursor ()
  "Open the file path under cursor.
If there is text selection, uses the text selection for path.
If the path starts with “http://”, open the URL in browser.
Input path can be {relative, full path, URL}.
Path may have a trailing “:‹n›” that indicates line number. If so, jump to that line number.
If path does not have a file extension, automatically try with “.el” for elisp files.
This command is similar to `find-file-at-point' but without prompting for confirmation.

URL `http://ergoemacs.org/emacs/emacs_open_file_path_fast.html'"
  (let (($path (if (use-region-p)
                   (buffer-substring-no-properties (region-beginning) (region-end))
                 (let (p0 p1 p2)
                   (setq p0 (point))
                   ;; chars that are likely to be delimiters of full path, e.g. space, tabs, brakets.
                   (skip-chars-backward "^  \"\t\n`'|()[]{}<>〔〕“”〈〉《》【】〖〗«»‹›·。\\`")
                   (setq p1 (point))
                   (goto-char p0)
                   (skip-chars-forward "^  \"\t\n`'|()[]{}<>〔〕“”〈〉《》【】〖〗«»‹›·。\\'")
                   (setq p2 (point))
                   (goto-char p0)
                   (buffer-substring-no-properties p1 p2)))))
    (if (string-match-p "\\`https?://" $path)
        (browse-url $path)
      (progn ; not starting “http://”
        (if (string-match "^\\`\\(.+?\\):\\([0-9]+\\)\\'" $path)
              (let (
                    ($fpath (match-string 1 $path))
                    ($line-num (string-to-number (match-string 2 $path))))
                (if (file-exists-p $fpath)
                      (find-file $fpath)
                      (goto-char 1)
                      (forward-line (1- $line-num)))
                    (when (y-or-n-p (format "file doesn't exist: 「%s」. Create?" $fpath))
                      (find-file $fpath))))))
            (if (file-exists-p $path)
                (find-file $path)
              (if (file-exists-p (concat $path ".el"))
                  (find-file (concat $path ".el"))
                (when (y-or-n-p (format "file doesn't exist: 「%s」. Create?" $path))
                  (find-file $path ))))))))))

Be sure to give it a easy key. 〔►see Emacs: How to Define Keys

Current File Commands

  1. Emacs: Open File Path Under Cursor
  2. Emacs: Copy File Path of Current Buffer
  3. Emacs: Run Current File
  4. Emacs: Delete Current File
  5. Emacs: Backup Current File
  6. Emacs: Batch Convert Line Ending in Dired
  7. Emacs: Open File in External App

Open File topics

  1. Emacs: File Manager, dired
  2. Emacs: Bookmark Tutorial
  3. Emacs: Open Recently Opened File
  4. Emacs: Open File Fast
  5. Emacs: Open Last Closed File
  6. Emacs: Open File Path Under Cursor

Buffer topics

  1. Emacs: List Buffers
  2. Emacs: Switch Buffer
  3. Emacs: New Empty Buffer
  4. Emacs: Next/Previous User Buffer
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.