Xcode: Sometimes a return is not a return (emacs brain damage)

ExpectedExpression
Indention and Insertion Prefs

Every now and then, I’ll be coding along merrily in Xcode and I’ll get an error much like the one at left. Or “expected identifier or ‘(‘” is another variant.

Huh? That code is fine. Maybe it is an invisible character? Nope. Nothing shown.

Took a bit, but I figured out the cause; 25 years of using emacs as my command line editor of choice, along with the folks at NeXT that implemented the AppKit’s text editor.

In emacs, you quite commonly navigate about by holding down the ctrl- key and banging on various keys to go to the beginning/end of lines, etc. Many of these control sequences are honored by Cocoa’s text editing system and quite a few more are supported in Xcode’s editor.

Seemingly unrelated, ctrl-return is mapped to Insert Line Break.

Thus, if you are an emacs head and you commonly hit ctrl-e<return> to start a new line of code and you happen to hold down the return key just a tad too long, it causes the error shown (or a variant depending on where the insert happens).

The easiest way to tell if this is the case is to go to the line of code after the line reporting the error and hit ctrl-a. If the cursor ends up at the beginning of the previous line, that line is ended by a line break and not a true newline. (ctrl-n – backspace – return to quickly fix).

While it is easy enough to fix once you know the ctrl-a trick, a better fix is one that makes it such that it’ll never happen again.

To do that, go to Xcode’s Key Bindings Preferences, click on “Text”, and scroll down to Insertions and Indetions. On Insert Line Break, delete the ctrl-return (hat + u-turn arrow) key sequence. For convenience add the same to Insert Newline.

Problem solved.



6 Responses to “Xcode: Sometimes a return is not a return (emacs brain damage)”

  1. ssp says:

    That’s a helpful hint.

    I find that making Xcode display line numbers (my impression is that my Xcode used to display line numbers but the application ‘reconsidered’ its defaults after some update) helps reduce the mystery as well.

  2. pmb says:

    Thank you, thank you, thank you. I do this all the time! Now I have an easy fix.

  3. Christopher says:

    What’s the usefulness of Line Break? (other than inserting non-printable characters from 1970 into my 2012 code, I guess. Also, seems like clang should be way more chatty about them.)

  4. stephane says:

    Why not just display an invisible character for a line break?

    It’s the clever solution that was put in place to remove the burden of finding non-breakable space (alt + space) in your source code in Xcode. And it’s also super useful in .strings files.

  5. jlnr says:

    Wow! I’ve never used emacs before and I’ve still run into exactly this problem two or three times in Xcode. Not sure why my finger was even on the ctrl key. Impossible to google this stuff…

    FWIW, there is another bizarre control character that can be inserted in all of OS X, namely the backspace character (ASCII 8), with shift+leftarrow/rightarrow+backspace:

    http://lbrandy.com/blog/2010/07/me-vs-backspace/

    I’ve noticed that in Safari’s unibar, Spotlight and Xcode 4, this combo inserts a literal “^H” nowadays. In Mail, Colloquy and this Safari text area, the character is still invisible and waiting to cause havoc (or in the case of IRC, public ridicule).

  6. Tim Griffiths says:

    You have made me so happy! I wish I would have found this sooner! Stupid line break was killing me!

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>