I have also posted this question here on Reddit

I'm not a version control or Git expert and it is very cumbersome for me to do all the steps manually. I want to have a text editor which uplon closing the file, if any changes were applied, applies these commands:

  • checks if the file has been edited
  • git add <filename>
  • opens a popup or tab to edit the commit message
  • git commit -m "the commit message entered in the popup"

I would appreciate if you could help me know if there is such an editor to have this feature built in or via a plugin/addon. Or if it is possible to write such a plugin.

P.S.1. It doesn't really have to be git, mercurial or other versions control systems are also fine.

P.S.2. I think this should be doable in Vim script and/or emacs lisp, If I knew those languages.

P.S.3. here and here different ideas have been discussed. It is apparently possible to git commit upon closing, but I want to write the message manually.

  • 1
    vim-fugitive – phd Jul 5 at 15:24
  • 1
    If Michal's answer helps you solve the problem, you can mark the answer by clicking √ symbol on the left of the answer? And it will also benefit other members who meet similar questions. – Marina Liu - MSFT Jul 16 at 9:02

It should definitely be doable in any reasonably programmable editor.

Here, for example, is a definitely very buggy and incomplete first stab at a hook for Emacs + magit:

(defun my/auto-commit-on-kill ()
  (when buffer-file-name
    (let* ((current (magit-file-relative-name))
           (choices (nconc (magit-modified-files) (magit-untracked-files)))
           (to-stage (car (member current choices))))
      (when to-stage
        (magit-stage-file to-stage)
        (magit-commit)))))

(add-hook 'kill-buffer-hook 'my/auto-commit-on-kill)

But actually if you were to go the Emacs/magit route I would strongly urge you to just use magit "manually". It's really not that complicated and yet it would teach you way more about git than relying on a even more simplified interface.

  • I haven't used emacs for a long long time now. would you be so kind to explain where this code should go? – Foad Jul 5 at 10:55
  • 1
    If you really wanted to test it, your Emacs initialization file - probably ~/.emacs.d/init.el - is the place for setting this up. You would also need to install magit. But I bet the current version of my example is not good enough for real use. I'm something of an Emacs user, but definitely not an elisp expert. – Michał Politowski Jul 5 at 11:00

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.