open README under git root directory in Emacs

  |   Source

I often need update my README files at the root directory of my project which is always managed by git.

As a lazy guy, I' rather use some hotkey to open that README.

So if you add below elisp code into your ~/ .emacs, you can use hot key `C-c C-f` to open the README:

(defun open-readme-in-git-root-directory ()
  (let (filename
        (root-dir (locate-dominating-file (file-name-as-directory (file-name-directory buffer-file-name)) ".git"))
    ;; (message "root-dir=%s" root-dir)
    (and root-dir (file-name-as-directory root-dir))
    (setq filename (concat root-dir ""))
    (if (not (file-exists-p filename))
        (setq filename (concat root-dir ""))
    ;; (message "filename=%s" filename)
    (if (file-exists-p filename)
        (switch-to-buffer (find-file-noselect filename nil nil))
      (message "NO or found!"))
(global-set-key (kbd "C-c C-f") 'open-readme-in-git-root-directory)


Comments powered by Disqus