Find file under cursor in emacs

#TIL you can use gf to open a file under the current cursor position in VIM, I was reading this post I found in Hacker News and I was wondering if evil-mode has this functionality as well

evil-mode has this key binding defined and it uses (find-file-at-point), it works well for absolute paths but not for relative paths and I wanted to use it for a path like this templates/home.html so I wrote some elisp code to be able to make what I want:

(defun my/find-file-under-cursor ()
  "Check it the filepath under cursor is an absolute path otherwise open helm and insert the filepath."
  (let ((file-path (thing-at-point 'filename t)))
    (if (file-name-absolute-p file-path)
	(find-file-at-point file-path)
      (minibuffer-with-setup-hook (lambda ()
				    (insert file-path))

If the path under the cursor is an absolute path it will use the regular find-file-at-point function, otherwise it will open helm-ls-git-ls and insert that value into the minibuffer. I use minibuffer-with-setup-hook to setup a temporal function attached to minibuffer-setup-hook to be able to insert the file path in the minibuffer after helm-ls-git-ls is opened.

With all of this done we just need to replace the regular behaviour of gf with our custom function:

(define-key evil-normal-state-map "gf" 'my/find-file-under-cursor)



And that's it. Enjoy! 🎉