goto-file in Emacs

Emacs

Emacs Problem Overview


Is there a substitute in emacs for the vi "gf" command? meaning try to open the file which is under the cursor right now if a real file name is in fact there.

Thanks

Emacs Solutions


Solution 1 - Emacs

You want the find-file-at-point function (which is also aliased to ffap). It's not bound to a key by default, but you can use

M-x ffap

Or, you can put in your .emacs file:

(ffap-bindings)

This will replace many of the normal find-file key bindings (like C-x C-f) with ffap-based versions. See the commentary in ffap.el for details.

Solution 2 - Emacs

Thanks, it works quite well but somehow the vi (gf) version is still somewhat smarter. I think it looks at some path variable for search paths.

I made something which is needlessly complicated but works for me (only in linux). It uses the "locate" command to search for the path under the cursor. I guess it could be made smarter by searching the relative path to the current file first. sorry for my bad elisp skills...It can probably be achieved in a much nicer way.

put in your .emacs, then use with M-x goto-file

(defun shell-command-to-string (command)
  "Execute shell command COMMAND and return its output as a string."
  (with-output-to-string
    (with-current-buffer standard-output
      (call-process shell-file-name nil t nil shell-command-switch command))))

(defun goto-file ()
  "open file under cursor"
  (interactive)
  (find-file (shell-command-to-string (concat "locate " (current-word) "|head -c -1" )) ))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNirView Question on Stackoverflow
Solution 1 - EmacscjmView Answer on Stackoverflow
Solution 2 - EmacsNirView Answer on Stackoverflow