I am trying to write a function that opens a file under the cursor. I need to get the string between quotes that is surrounding the cursor in my elisp function so that I can use that string to call find file, how can I achieve this?


"~/git/lis<cursor>p.el" should return ~/git/lisp.el


Instead of writing from scratch I found a function that does this


| improve this answer | |
  • Please mark it as answered, if this is the solution you were looking for. – Manuel Uberti Feb 10 '18 at 8:07
  • @ManuelUberti I am not able to right now, says I have to wait for 2 days. – Prathik Rajendran M Feb 10 '18 at 8:58

Something to note: while find-file-at-point is great, it still asks for a confirmation. If you ever want a function that doesn't ask for confirmation Xah, provides some elisp code that automatically jumps to the file:

(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 extention, 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'
Version 2017-09-01"
  (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. space, tabs, brakets. 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))))
           "^file:///" "/"
            ":\\'" "" $inputStr))))
    (if (string-match-p "\\`https?://" $path)
        (if (fboundp 'xahsite-url-to-filepath)
              (find-file (xahsite-url-to-filepath $path)))
          (progn (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 ))))))))))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.