Emacs: Open File Path Under Cursor ๐
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.
(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, or โ:โนnโบ:โนmโบโ with line and column 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://xahlee.info/emacs/emacs/emacs_open_file_path_fast.html' Version 2020-10-17" (interactive) (let* ( ($inputStr (if (use-region-p) (buffer-substring-no-properties (region-beginning) (region-end)) (let ($p0 $p1 $p2 ;; chars that are likely to be delimiters of file path or url, e.g. whitespace, comma. The colon is a problem. cuz it's in url, but not in file name. Don't want to use just space as delimiter because path or url are often in brackets or quotes as in markdown or html ($pathStops "^ ย \t\n\"`'โโโโ|[]{}ใใ<>ใใใใใใใใใใยซยปโนโบโฎโฏโฌโญใใยทใ\\")) (setq $p0 (point)) (skip-chars-backward $pathStops) (setq $p1 (point)) (goto-char $p0) (skip-chars-forward $pathStops) (setq $p2 (point)) (goto-char $p0) (buffer-substring-no-properties $p1 $p2)))) ($path (replace-regexp-in-string "^file:///" "/" (replace-regexp-in-string ":\\'" "" $inputStr)))) (if (string-match-p "\\`https?://" $path) (if (fboundp 'xahsite-url-to-filepath) (let (($x (xahsite-url-to-filepath $path))) (if (string-match "^http" $x ) (browse-url $x) (find-file $x))) (progn (browse-url $path))) (progn ; not starting โhttp://โ (if (string-match "#" $path ) (let ( ( $fpath (substring $path 0 (match-beginning 0))) ( $fractPart (substring $path (1+ (match-beginning 0))))) (if (file-exists-p $fpath) (progn (find-file $fpath) (goto-char (point-min)) (search-forward $fractPart )) (when (y-or-n-p (format "file no exist: ใ%sใ. Create?" $fpath)) (find-file $fpath)))) (if (string-match "^\\`\\(.+?\\):\\([0-9]+\\)\\(:[0-9]+\\)?\\'" $path) (let ( ($fpath (match-string 1 $path)) ($line-num (string-to-number (match-string 2 $path)))) (if (file-exists-p $fpath) (progn (find-file $fpath) (goto-char (point-min)) (forward-line (1- $line-num))) (when (y-or-n-p (format "file no exist: ใ%sใ. Create?" $fpath)) (find-file $fpath)))) (if (file-exists-p $path) (progn ; open f.ts instead of f.js (let (($ext (file-name-extension $path)) ($fnamecore (file-name-sans-extension $path))) (if (and (string-equal $ext "js") (file-exists-p (concat $fnamecore ".ts"))) (find-file (concat $fnamecore ".ts")) (find-file $path)))) (if (file-exists-p (concat $path ".el")) (find-file (concat $path ".el")) (when (y-or-n-p (format "file no exist: ใ%sใ. Create?" $path)) (find-file $path ))))))))))
Be sure to give it a easy key. [see Emacs: How to Define Keybinding]