I'm a beginner in Emacs and Elisp, but do have programming experience.
My question is quite straightforward, I want to have a command open-git
that when in a git directory, it opens the url inside a browser.
I have the following code
(defun open-git ()
"Opens the git repository in a browser."
(interactive)
(browse-url (replace-regexp-in-string
"git@\\(.*\\):\\(.*\\)/\\(.*\\).git", ; Regex
"https://\\1/\\2/\\3.git/", ; Replacement
(shell-command-to-string "git remote get-url origin")) ; String
)
)
The git repository url is in a ssh format, meaning for browse-url
to work correctly, I have to regex replace it.
I have used M-x regexp-builder
to confirm it matches correctly. However, when trying to execute the command from a file buffer, it gives me the error Symbol's function definition is void: \,
.
Reading up on it, I saw that \,
is used to run regex interactively for the replacement. But I'm not using \,
, is it being used internally?
On a git url that is for example [email protected]:foo/bar.git
the matches are:
- github.com
- foo
- bar
Obviously I can just set the git url to the https version and be done with it. But I also just want to understand Emacs and Elisp better, so figured this was a nice easy exercise.