Generate a file with this:

(echo '#!/bin/bash'
echo 'myfunc() {'
echo "cat <<'z'"
seq 135
echo zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
echo z
echo '}') >out

Open it in Emacs. When pressing C-M-f I get the error:

Scan error: "Containing expression ends prematurely", 515, 515

Then I press M-> to get to end of file, C-x C-x to move back to where I came from, and now I can press C-M-f and move to the next sexp.

It is as if emacs does not really index the full file before I have been to the end of the file.

How can I fix this?

  • Do you see the same thing when you start Emacs using emacs -Q (no init file? If not, bisect your init file to find the culprit. If yes, consider filing a bug report: M-x report-emacs-bug.
    – Drew
    Commented Mar 5, 2018 at 21:54

1 Answer 1


Made a bug report for this: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=30726

Bug-source is in but-last line of the def in lisp.el, where

(goto-char (or (scan-sexps (point) arg) (buffer-end arg)))

scan-sexps sends the error.

Wrapping it into (ignore-errors:

(or (ignore-errors (scan-sexps (point) arg))...

makes it gone.

Error showed up when forward-sexp was called from closing parenthesis at last line of example code. Seen from emitting scan-sexps that error might be justified, however forward-sexp should deal with it. There may be more occasions to fail. IMHO forward-sexp navigates strangely from inside a string - try it at a functions docstring.

To avoid these wrote ar-forward-sexp

which see here: https://github.com/andreas-roehler/thingatpt-utils-core/blob/master/ar-subr.el

  • Does this just ignore the error or does it fix it, so that it now works as if I have done M-> C-x C-x first?
    – Ole Tange
    Commented Mar 6, 2018 at 8:29
  • @OleTange Updated the answer to be more informative. Commented Mar 6, 2018 at 10:33

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.