I am trying to use gofmt on my go before I save it.

; Run gofmt before save
(setq gofmt-command "goimports")
(setq exec-path (cons "/usr/lib/go/bin" exec-path))
(add-to-list 'exec-path "/home/giodamelio/Projects/go/bin")
(add-hook 'before-save-hook 'gofmt-before-save)

The problem is, whenever I try to move the cursor(either with C-n/C-p, or with evil), the contents of the buffer seem to flicker. It is not the entire buffer(not visual bell), and it is not the same bits of text each time.

When I comment out those four lines it goes away, so I know it has somthing to do with them, but my understanding of the before-save-hook is that is will only run before I save, not whenever I move the cursor"

  • Does the issue persist if you only comment the before-save-hook, but leave the other lines intact?
    – user227
    Nov 20, 2014 at 13:43
  • 1
    Which version of emacs? Does this happen with just these four lines in an otherwise empty emacs config?
    – Squidly
    Nov 20, 2014 at 14:08
  • @lunaryorn No, it is definitely the before hook,
    – giodamelio
    Nov 20, 2014 at 19:43
  • @MrBones No, when they are by themselves(no package init or anything), the problem goes away).
    – giodamelio
    Nov 20, 2014 at 19:52
  • 1
    That makes be think that the hook actually runs, i.e. that the buffer is frequently saved, without you typing C-x C-s. Do you use some auto-saving mode?
    – user227
    Nov 20, 2014 at 20:27

2 Answers 2


Load library simple.el (not simple.elc), then do M-x debug-on-entry RET next-line RET.

Then use C-n and step through the debugger using d (or c to skip through an evaluation) to see what happens (when the flickering occurs etc.). If the flicker happens during evaluation of a function call for a function that is not defined in simple.el, you can load its source file too, to get better debugging about it. And so on.

Update after your comment:

Sounds like an error is occurring during redisplay. Typically that is because of one of these:

  1. An error in post-command-hook (or perhaps pre-command-hook).

  2. An error in something that updates the mode line (e.g., something in mode-line-format).

  3. An error in font-locking.

Check what you are doing in these 3 areas. Remove stuff from post-command-hook to see if it stops the flickering. Likewise, if you do anything to the mode line, stop doing it to see if that stops the flickering. Similarly, turn off font-lock mode to see if that helps.

If you can stop the flickering this way then you'll know which code to investigate. You cannot use the debugger on it (because the debugger is inhibited during redisplay). But you can add calls to message to print program state & other info.

  • I tried that, but the flicker happens whenever I do any input(d or c). It is like it is doing it on every redraw.
    – giodamelio
    Nov 23, 2014 at 5:09
  • Thanks, it turned out to be git-gutter conflicting with linenum mode. I switched to diff-hl and it is working fine. Still not sure why it was only happening in go-mode(I tried it in other modes inside git repos), I can only guess that it was the filesize(the go file was much larger then any of the other files I tested).
    – giodamelio
    Nov 26, 2014 at 4:17

My guess is that some other part of your config causes constant saves, tho I wouldn't know why nor of what.

You might like to try

(add-hook 'go-mode-hook
          (lambda ()
            (add-hook 'before-save-hook 'gofmt-before-save nil t)))

so the gofmt-before-save only happens in go-mode buffers, rather than everywhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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