goto-file in Emacs
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
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.
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" )) ))